summaryrefslogtreecommitdiff
path: root/docs/handbook/ofborg/amqp-infrastructure.md
blob: 4575da966a1be2cb6608cf397527d170ea6c6964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# Tickborg — AMQP Infrastructure

## Overview

Tickborg uses **AMQP 0-9-1** (RabbitMQ) as the message bus connecting all
services. The Rust crate `lapin` (v4.3.0) provides the low-level protocol
client. Two abstraction layers — `easyamqp` and `easylapin` — provide
higher-level APIs for declaring exchanges, binding queues, and running worker
consumers.

---

## Key Source Files

| File | Purpose |
|------|---------|
| `tickborg/src/easyamqp.rs` | Config types, traits, exchange/queue declarations |
| `tickborg/src/easylapin.rs` | `lapin`-based implementations of the traits |
| `tickborg/src/worker.rs` | `SimpleWorker` trait, `Action` enum |
| `tickborg/src/notifyworker.rs` | `SimpleNotifyWorker`, `NotificationReceiver` |
| `tickborg/src/config.rs` | `RabbitMqConfig` |

---

## Connection Configuration

### `RabbitMqConfig`

```rust
// config.rs
#[derive(Deserialize, Debug)]
pub struct RabbitMqConfig {
    pub ssl: bool,
    pub host: String,
    pub vhost: Option<String>,
    pub username: String,
    pub password_file: PathBuf,
}
```

### Connection URI Construction

```rust
// easylapin.rs
pub async fn from_config(cfg: &RabbitMqConfig) -> Result<lapin::Connection, lapin::Error> {
    let password = std::fs::read_to_string(&cfg.password_file)
        .expect("Failed to read RabbitMQ password file")
        .trim()
        .to_owned();

    let vhost = cfg.vhost
        .as_deref()
        .unwrap_or("/")
        .to_owned();

    let scheme = if cfg.ssl { "amqps" } else { "amqp" };
    let uri = format!(
        "{scheme}://{user}:{pass}@{host}/{vhost}",
        user = urlencoding::encode(&cfg.username),
        pass = urlencoding::encode(&password),
        host = cfg.host,
        vhost = urlencoding::encode(&vhost),
    );

    lapin::Connection::connect(
        &uri,
        lapin::ConnectionProperties::default()
            .with_tokio()
            .with_default_executor(8),
    ).await
}
```

---

## Exchange and Queue Configuration Types

### `ExchangeType`

```rust
#[derive(Clone, Debug)]
pub enum ExchangeType {
    Topic,
    Fanout,
    Headers,
    Direct,
    Custom(String),
}

impl ExchangeType {
    fn as_str(&self) -> &str {
        match self {
            ExchangeType::Topic => "topic",
            ExchangeType::Fanout => "fanout",
            ExchangeType::Headers => "headers",
            ExchangeType::Direct => "direct",
            ExchangeType::Custom(s) => s.as_ref(),
        }
    }
}
```

### `ExchangeConfig`

```rust
#[derive(Clone, Debug)]
pub struct ExchangeConfig {
    pub exchange_name: String,
    pub exchange_type: ExchangeType,
    pub passive: bool,
    pub durable: bool,
    pub exclusive: bool,
    pub auto_delete: bool,
    pub no_wait: bool,
    pub internal: bool,
}

impl Default for ExchangeConfig {
    fn default() -> Self {
        ExchangeConfig {
            exchange_name: String::new(),
            exchange_type: ExchangeType::Topic,
            passive: false,
            durable: true,
            exclusive: false,
            auto_delete: false,
            no_wait: false,
            internal: false,
        }
    }
}
```

### `QueueConfig`

```rust
#[derive(Clone, Debug)]
pub struct QueueConfig {
    pub queue_name: String,
    pub passive: bool,
    pub durable: bool,
    pub exclusive: bool,
    pub auto_delete: bool,
    pub no_wait: bool,
}

impl Default for QueueConfig {
    fn default() -> Self {
        QueueConfig {
            queue_name: String::new(),
            passive: false,
            durable: true,
            exclusive: false,
            auto_delete: false,
            no_wait: false,
        }
    }
}
```

### `BindQueueConfig`

```rust
#[derive(Clone, Debug)]
pub struct BindQueueConfig {
    pub queue_name: String,
    pub exchange_name: String,
    pub routing_key: Option<String>,
    pub no_wait: bool,
    pub headers: Option<Vec<(String, String)>>,
}
```

### `ConsumeConfig`

```rust
#[derive(Clone, Debug)]
pub struct ConsumeConfig {
    pub queue: String,
    pub consumer_tag: String,
    pub no_local: bool,
    pub no_ack: bool,
    pub no_wait: bool,
    pub exclusive: bool,
}
```

---

## The `ChannelExt` Trait

```rust
// easyamqp.rs
pub trait ChannelExt {
    fn declare_exchange(
        &mut self,
        config: ExchangeConfig,
    ) -> impl Future<Output = Result<(), String>>;

    fn declare_queue(
        &mut self,
        config: QueueConfig,
    ) -> impl Future<Output = Result<(), String>>;

    fn bind_queue(
        &mut self,
        config: BindQueueConfig,
    ) -> impl Future<Output = Result<(), String>>;
}
```

### `lapin` Implementation

```rust
// easylapin.rs
impl ChannelExt for lapin::Channel {
    async fn declare_exchange(&mut self, config: ExchangeConfig) -> Result<(), String> {
        let opts = ExchangeDeclareOptions {
            passive: config.passive,
            durable: config.durable,
            auto_delete: config.auto_delete,
            internal: config.internal,
            nowait: config.no_wait,
        };
        self.exchange_declare(
            &config.exchange_name,
            lapin::ExchangeKind::Custom(
                config.exchange_type.as_str().to_owned()
            ),
            opts,
            FieldTable::default(),
        ).await
        .map_err(|e| format!("Failed to declare exchange: {e}"))?;
        Ok(())
    }

    async fn declare_queue(&mut self, config: QueueConfig) -> Result<(), String> {
        let opts = QueueDeclareOptions {
            passive: config.passive,
            durable: config.durable,
            exclusive: config.exclusive,
            auto_delete: config.auto_delete,
            nowait: config.no_wait,
        };
        self.queue_declare(
            &config.queue_name,
            opts,
            FieldTable::default(),
        ).await
        .map_err(|e| format!("Failed to declare queue: {e}"))?;
        Ok(())
    }

    async fn bind_queue(&mut self, config: BindQueueConfig) -> Result<(), String> {
        let routing_key = config.routing_key
            .as_deref()
            .unwrap_or("#");

        let mut headers = FieldTable::default();
        if let Some(hdr_vec) = &config.headers {
            for (k, v) in hdr_vec {
                headers.insert(
                    k.clone().into(),
                    AMQPValue::LongString(v.clone().into()),
                );
            }
        }

        self.queue_bind(
            &config.queue_name,
            &config.exchange_name,
            routing_key,
            QueueBindOptions { nowait: config.no_wait },
            headers,
        ).await
        .map_err(|e| format!("Failed to bind queue: {e}"))?;
        Ok(())
    }
}
```

---

## The `ConsumerExt` Trait

```rust
// easyamqp.rs
pub trait ConsumerExt {
    fn consume<W: worker::SimpleWorker + 'static>(
        &mut self,
        worker: W,
        config: ConsumeConfig,
    ) -> impl Future<Output = Result<(), String>>;
}
```

Three implementations exist in `easylapin.rs`:

### 1. `Channel` — Simple Workers

```rust
impl ConsumerExt for lapin::Channel {
    async fn consume<W: worker::SimpleWorker + 'static>(
        &mut self,
        mut worker: W,
        config: ConsumeConfig,
    ) -> Result<(), String> {
        let consumer = self.basic_consume(
            &config.queue,
            &config.consumer_tag,
            BasicConsumeOptions {
                no_local: config.no_local,
                no_ack: config.no_ack,
                exclusive: config.exclusive,
                nowait: config.no_wait,
            },
            FieldTable::default(),
        ).await
        .map_err(|e| format!("Failed to start consumer: {e}"))?;

        // Message processing loop
        while let Some(delivery) = consumer.next().await {
            let delivery = delivery
                .map_err(|e| format!("Consumer error: {e}"))?;

            // Decode the message
            let job = match worker.msg_to_job(
                &delivery.routing_key,
                &delivery.exchange,
                &delivery.data,
            ).await {
                Ok(job) => job,
                Err(err) => {
                    tracing::error!("Failed to decode message: {}", err);
                    delivery.ack(BasicAckOptions::default()).await?;
                    continue;
                }
            };

            // Process the job
            let actions = worker.consumer(&job).await;

            // Execute resulting actions
            for action in actions {
                action_deliver(&self, &delivery, action).await?;
            }
        }
        Ok(())
    }
}
```

### 2. `WorkerChannel` — Workers on a Dedicated Channel

```rust
pub struct WorkerChannel {
    pub channel: lapin::Channel,
    pub prefetch_count: u16,
}

impl ConsumerExt for WorkerChannel {
    async fn consume<W: worker::SimpleWorker + 'static>(
        &mut self,
        worker: W,
        config: ConsumeConfig,
    ) -> Result<(), String> {
        // Set QoS (prefetch count)
        self.channel.basic_qos(
            self.prefetch_count,
            BasicQosOptions::default(),
        ).await?;

        // Delegate to Channel implementation
        self.channel.consume(worker, config).await
    }
}
```

### 3. `NotifyChannel` — Notify Workers

```rust
pub struct NotifyChannel {
    pub channel: lapin::Channel,
}

impl NotifyChannel {
    pub async fn consume<W: notifyworker::SimpleNotifyWorker + 'static>(
        &mut self,
        mut worker: W,
        config: ConsumeConfig,
    ) -> Result<(), String> {
        // Similar to Channel but creates a ChannelNotificationReceiver
        // that allows the worker to report progress back to AMQP
        let consumer = self.channel.basic_consume(/* ... */).await?;

        while let Some(delivery) = consumer.next().await {
            let delivery = delivery?;
            let receiver = ChannelNotificationReceiver {
                channel: self.channel.clone(),
                delivery: &delivery,
            };

            let job = worker.msg_to_job(/* ... */).await?;
            let actions = worker.consumer(&job, &receiver).await;

            for action in actions {
                action_deliver(&self.channel, &delivery, action).await?;
            }
        }
        Ok(())
    }
}
```

---

## Action Delivery

```rust
// easylapin.rs
async fn action_deliver(
    channel: &lapin::Channel,
    delivery: &lapin::message::Delivery,
    action: worker::Action,
) -> Result<(), String> {
    match action {
        worker::Action::Ack => {
            delivery.ack(BasicAckOptions::default()).await
                .map_err(|e| format!("Failed to ack: {e}"))?;
        }
        worker::Action::NackRequeue => {
            delivery.nack(BasicNackOptions {
                requeue: true,
                ..Default::default()
            }).await
                .map_err(|e| format!("Failed to nack: {e}"))?;
        }
        worker::Action::NackDump => {
            delivery.nack(BasicNackOptions {
                requeue: false,
                ..Default::default()
            }).await
                .map_err(|e| format!("Failed to nack-dump: {e}"))?;
        }
        worker::Action::Publish(msg) => {
            channel.basic_publish(
                msg.exchange.as_deref().unwrap_or(""),
                msg.routing_key.as_deref().unwrap_or(""),
                BasicPublishOptions::default(),
                &msg.content,
                BasicProperties::default()
                    .with_delivery_mode(2),  // persistent
            ).await
                .map_err(|e| format!("Failed to publish: {e}"))?;
        }
    }
    Ok(())
}
```

---

## Notification Receiver

```rust
// easylapin.rs
pub struct ChannelNotificationReceiver<'a> {
    channel: lapin::Channel,
    delivery: &'a lapin::message::Delivery,
}

impl<'a> notifyworker::NotificationReceiver for ChannelNotificationReceiver<'a> {
    async fn tell(&mut self, action: worker::Action) {
        if let Err(e) = action_deliver(&self.channel, self.delivery, action).await {
            tracing::error!("Failed to deliver notification action: {}", e);
        }
    }
}
```

Used by `BuildWorker` (which implements `SimpleNotifyWorker`) to publish
incremental log messages while a build is in progress, without waiting for the
build to complete.

---

## Exchange Topology

### Declarations

Every binary declares its own required exchanges/queues at startup.
Here is the complete topology used across the system:

| Exchange | Type | Purpose |
|----------|------|---------|
| `github-events` | Topic | GitHub webhooks → routing by event type |
| `build-jobs` | Fanout | Evaluation → builders |
| `build-results` | Fanout | Builder results → poster + stats |
| `logs` | Topic | Build log lines → collector |
| `stats` | Fanout | Metrics events → stats collector |

### Queue Bindings

| Queue | Exchange | Routing Key | Consumer |
|-------|----------|-------------|----------|
| `mass-rebuild-check-inputs` | `github-events` | `pull_request.*` | EvaluationFilterWorker |
| `mass-rebuild-check-jobs` | _(direct publish)_ | — | EvaluationWorker |
| `build-inputs-{identity}` | `build-jobs` | — | BuildWorker |
| `build-results` | `build-results` | — | GitHubCommentPoster |
| `build-logs` | `logs` | `logs.*` | LogMessageCollector |
| `comment-jobs` | `github-events` | `issue_comment.*` | GitHubCommentWorker |
| `push-jobs` | `github-events` | `push.*` | PushFilterWorker |
| `stats-events` | `stats` | — | StatCollectorWorker |

### Topic Routing Keys

For the `github-events` exchange, the routing key follows the pattern:

```
{event_type}.{action}
```

Examples:
- `pull_request.opened`
- `pull_request.synchronize`
- `issue_comment.created`
- `push.push`

For the `logs` exchange:
- `logs.{build_id}` — Each build's log lines are tagged with the build ID

---

## Message Persistence

All published messages use `delivery_mode = 2` (persistent), which means
messages survive RabbitMQ restarts:

```rust
BasicProperties::default()
    .with_delivery_mode(2)  // persistent
```

---

## Prefetch / QoS

Worker binaries configure `basic_qos` (prefetch count) to control how many
messages are delivered to a consumer before it must acknowledge them:

```rust
let mut chan = WorkerChannel {
    channel,
    prefetch_count: 1,  // Process one job at a time
};
```

Setting `prefetch_count = 1` ensures fair dispatching across multiple worker
instances and prevents a single slow worker from hoarding messages.

---

## Error Recovery

### Message Processing Failures

| Scenario | Action | Effect |
|----------|--------|--------|
| Decode error | `Ack` | Message discarded |
| Processing error (retryable) | `NackRequeue` | Message requeued |
| Processing error (permanent) | `NackDump` | Message dead-lettered |
| Processing success | `Ack` | Message removed |
| Worker publish | `Publish` | New message to exchange |

### Connection Recovery

`lapin` supports automatic connection recovery. If the TCP connection drops,
the library will attempt to reconnect. However, tickborg binaries are designed
to be restarted by their process supervisor (systemd) if the connection
cannot be re-established.

---

## Usage Example: Declaring a Full Stack

A typical binary does:

```rust
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tickborg::setup_log();
    let cfg = tickborg::config::load();

    // 1. Connect to RabbitMQ
    let conn = easylapin::from_config(&cfg.rabbitmq).await?;
    let mut chan = conn.create_channel().await?;

    // 2. Declare exchange
    chan.declare_exchange(ExchangeConfig {
        exchange_name: "github-events".to_owned(),
        exchange_type: ExchangeType::Topic,
        durable: true,
        ..Default::default()
    }).await?;

    // 3. Declare queue
    chan.declare_queue(QueueConfig {
        queue_name: "mass-rebuild-check-inputs".to_owned(),
        durable: true,
        ..Default::default()
    }).await?;

    // 4. Bind queue to exchange
    chan.bind_queue(BindQueueConfig {
        queue_name: "mass-rebuild-check-inputs".to_owned(),
        exchange_name: "github-events".to_owned(),
        routing_key: Some("pull_request.*".to_owned()),
        ..Default::default()
    }).await?;

    // 5. Start consume loop
    let worker = EvaluationFilterWorker::new(cfg.acl());
    chan.consume(worker, ConsumeConfig {
        queue: "mass-rebuild-check-inputs".to_owned(),
        consumer_tag: format!("evaluation-filter-{}", cfg.identity),
        ..Default::default()
    }).await?;

    Ok(())
}
```