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
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use dashmap::DashMap;
use tokio::sync::{Notify, Semaphore};
use tokio_util::sync::CancellationToken;
use super::builder::QueueBuilder;
use super::core::Outbox;
use super::prioritizer::SharedPrioritizer;
use super::stats::{StatsListener, StatsRegistry, StatsReporter};
use super::taskward::{
BackoffConfig, Bulkhead, BulkheadConfig, ConcurrencyLimit, PanicPolicy, TaskSet,
TracingListener, WorkerBuilder,
};
use super::types::{OutboxConfig, OutboxError, Partitions, QueueConfig, SequencerConfig};
use super::workers::sequencer::{Sequencer, SequencerReport};
use super::workers::vacuum::{VacuumReport, VacuumTask};
use crate::Db;
/// Deferred queue declaration — config + factory, resolved at `start()`.
pub struct QueueDeclaration {
pub(crate) name: String,
pub(crate) partitions: Partitions,
pub(crate) config: QueueConfig,
pub(crate) factory: Box<dyn super::builder::ProcessorFactory>,
}
/// Fluent builder for the outbox pipeline.
///
/// Entry point: [`Outbox::builder(db)`](Outbox::builder). Configure global
/// settings and register queues with handlers, then call
/// [`start()`](Self::start) to spawn background tasks.
///
/// ```ignore
/// let handle = Outbox::builder(db)
/// .poll_interval(Duration::from_millis(100))
/// .queue("orders", Partitions::of(4))
/// .decoupled(my_handler)
/// .start().await?;
/// // enqueue via handle.outbox()
/// handle.stop().await;
/// ```
pub struct OutboxBuilder {
db: Db,
sequencer_batch_size: u32,
poll_interval: Duration,
poker_interval: Duration,
partition_batch_limit: u32,
max_inner_iterations: u32,
processors: Option<usize>,
maintenance_guaranteed: Option<usize>,
maintenance_shared: Option<usize>,
vacuum_cooldown: Duration,
stats_interval: Option<Duration>,
steady_pace: Duration,
pub(crate) queue_declarations: Vec<QueueDeclaration>,
}
impl OutboxBuilder {
pub(crate) fn new(db: Db) -> Self {
Self {
db,
sequencer_batch_size: super::types::DEFAULT_SEQUENCER_BATCH_SIZE,
poll_interval: super::types::DEFAULT_POLL_INTERVAL,
poker_interval: super::types::DEFAULT_POKER_INTERVAL,
partition_batch_limit: super::types::DEFAULT_PARTITION_BATCH_LIMIT,
max_inner_iterations: super::types::DEFAULT_MAX_INNER_ITERATIONS,
processors: None,
maintenance_guaranteed: None,
maintenance_shared: None,
vacuum_cooldown: super::types::DEFAULT_VACUUM_COOLDOWN,
stats_interval: Some(Duration::from_secs(10)),
steady_pace: Duration::from_millis(10),
queue_declarations: Vec::new(),
}
}
/// Sequencer batch size (rows per cycle). Default: 100.
#[must_use]
pub fn sequencer_batch_size(mut self, n: u32) -> Self {
self.sequencer_batch_size = n;
self
}
/// Safety net fallback poll interval for both the sequencer and
/// per-partition processors. Default: 1s.
#[must_use]
pub fn poll_interval(mut self, d: Duration) -> Self {
self.poll_interval = d;
self
}
/// Maximum number of concurrent partition processors across all queues.
///
/// Controls the global processor semaphore. Each partition processor
/// acquires one permit before executing. Default: unlimited.
///
/// # Panics
///
/// Panics if `n` is 0.
///
/// # Connection budget
///
/// `max_connections >= processors + guaranteed + shared + producer_headroom`
#[must_use]
pub fn processors(mut self, n: usize) -> Self {
assert!(n > 0, "processors must be > 0");
self.processors = Some(n);
self
}
/// Two-tier maintenance connection budget.
///
/// - `guaranteed`: permits reserved exclusively for sequencer workers.
/// - `shared`: permits available to both sequencer and maintenance tasks
/// (vacuum). Sequencers steal shared permits when maintenance is idle.
///
/// Total sequencer workers = `guaranteed + shared`.
/// Vacuum workers = `shared`.
///
/// # Panics
///
/// Panics if either `guaranteed` or `shared` is 0.
///
/// # Connection budget
///
/// `max_connections >= processors + guaranteed + shared + producer_headroom`
///
/// Default: `guaranteed = 2, shared = 1`.
#[must_use]
pub fn maintenance(mut self, guaranteed: usize, shared: usize) -> Self {
assert!(guaranteed > 0, "maintenance guaranteed must be > 0");
assert!(shared > 0, "maintenance shared must be > 0");
self.maintenance_guaranteed = Some(guaranteed);
self.maintenance_shared = Some(shared);
self
}
/// Cold reconciler (poker) interval. Default: 60s.
/// The poker is a safety net that discovers partitions with pending
/// incoming rows by querying the DB. Normally, the hot path (enqueue)
/// populates the dirty set directly.
///
/// # Panics
///
/// Panics if `d` is zero.
#[must_use]
pub fn poker_interval(mut self, d: Duration) -> Self {
assert!(!d.is_zero(), "poker_interval must be > 0");
self.poker_interval = d;
self
}
/// Max partitions the sequencer processes per cycle. Default: 128.
///
/// # Panics
///
/// Panics if `n` is 0.
#[must_use]
pub fn partition_batch_limit(mut self, n: u32) -> Self {
assert!(n > 0, "partition_batch_limit must be > 0");
self.partition_batch_limit = n;
self
}
/// Max inner drain iterations per partition before yielding. Default: 8.
///
/// # Panics
///
/// Panics if `n` is 0.
#[must_use]
pub fn max_inner_iterations(mut self, n: u32) -> Self {
assert!(n > 0, "max_inner_iterations must be > 0");
self.max_inner_iterations = n;
self
}
/// Minimum interval between full vacuum sweeps. Default: 1h.
#[must_use]
pub fn vacuum_cooldown(mut self, d: Duration) -> Self {
self.vacuum_cooldown = d;
self
}
/// Periodic stats reporting interval. Default: 10s.
/// `Duration::ZERO` disables stats collection entirely.
#[must_use]
pub fn stats_interval(mut self, d: Duration) -> Self {
self.stats_interval = if d.is_zero() { None } else { Some(d) };
self
}
/// Steady-state pace between worker executions. Default: 100ms.
/// Enforces a minimum interval between consecutive executions even
/// when healthy, preventing tight-loop spinning on the database.
/// `Duration::ZERO` disables pacing (workers spin as fast as possible).
#[must_use]
pub fn steady_pace(mut self, d: Duration) -> Self {
self.steady_pace = d;
self
}
/// Begin building a queue registration.
pub fn queue(self, name: &str, partitions: Partitions) -> QueueBuilder {
QueueBuilder::new(self, name.to_owned(), partitions)
}
/// Spawn background tasks and return a handle to the running pipeline.
///
/// Registers all queues in the database, creates the sequencer and
/// per-partition processors, then starts them as background tasks.
///
/// # Errors
///
/// Returns an error if queue registration fails (DB operation).
///
/// # Panics
///
/// Panics if the internal stats registry mutex is poisoned.
pub async fn start(mut self) -> Result<OutboxHandle, OutboxError> {
// Build shared prioritizer first — Outbox and sequencer workers
// both subscribe to its internal Notify for wakeups.
let shared_prioritizer = Arc::new(SharedPrioritizer::new());
let config = OutboxConfig {
sequencer: SequencerConfig {
batch_size: self.sequencer_batch_size,
poll_interval: self.poll_interval,
partition_batch_limit: self.partition_batch_limit,
max_inner_iterations: self.max_inner_iterations,
},
};
let outbox = Outbox::new(config);
let outbox = Arc::new(outbox);
let cancel = CancellationToken::new();
let mut task_set = TaskSet::new(cancel.clone());
let start_notify = Arc::new(Notify::new());
let partition_notify: DashMap<i64, Arc<Notify>> = DashMap::new();
// Global processor semaphore — caps total concurrent partition processors
let processor_sem = Arc::new(Semaphore::new(
self.processors
.unwrap_or(Semaphore::MAX_PERMITS)
.min(Semaphore::MAX_PERMITS),
));
// Shared stats registry (wrapped in Mutex for processor factory access)
let stats_registry_shared = if self.stats_interval.is_some() {
Some(Arc::new(std::sync::Mutex::new(StatsRegistry::new())))
} else {
None
};
// Register queues and spawn processor workers via factories
for decl in &mut self.queue_declarations {
// Apply global poll_interval to each queue
decl.config.poll_interval = self.poll_interval;
outbox
.register_queue(&self.db, &decl.name, decl.partitions.count())
.await?;
let partition_ids = outbox.partition_ids_for_queue(&decl.name);
for &pid in &partition_ids {
let notify = Arc::new(Notify::new());
partition_notify.insert(pid, Arc::clone(¬ify));
let ctx = super::builder::SpawnContext {
pid,
db: self.db.clone(),
cancel: cancel.clone(),
partition_notify: notify,
processor_sem: Arc::clone(&processor_sem),
start_notify: Arc::clone(&start_notify),
outbox: Arc::clone(&outbox),
stats_registry: stats_registry_shared.clone(),
};
let (name, future) = decl.factory.spawn(ctx);
task_set.spawn(name, future);
}
}
// Two-tier maintenance semaphores
let guaranteed = self.maintenance_guaranteed.unwrap_or(2);
let shared = self.maintenance_shared.unwrap_or(1);
let guaranteed_sem = Arc::new(Semaphore::new(guaranteed.min(Semaphore::MAX_PERMITS)));
let shared_sem = Arc::new(Semaphore::new(shared.min(Semaphore::MAX_PERMITS)));
// Collect per-partition notify map for the sequencer
let mut notify_map: HashMap<i64, Arc<Notify>> = HashMap::new();
for entry in &partition_notify {
notify_map.insert(*entry.key(), Arc::clone(entry.value()));
}
let notify_map = Arc::new(notify_map);
outbox.set_partition_notify(notify_map).await;
outbox
.set_prioritizer(Arc::clone(&shared_prioritizer))
.await;
// Eager reconciliation at startup: discover pending partitions
// from the incoming table before spawning the sequencer.
super::workers::reconciler::reconcile_dirty(&outbox, &self.db, &shared_prioritizer).await;
// Spawn parallel sequencer workers (guaranteed + shared)
let sequencer_count = guaranteed + shared;
for i in 0..sequencer_count {
#[allow(unused_mut)]
let mut sequencer = Sequencer::new(
outbox.config().sequencer.clone(),
Arc::clone(&outbox),
self.db.clone(),
Arc::clone(&shared_prioritizer),
);
let name = format!("sequencer-{i}");
let mut builder = WorkerBuilder::<SequencerReport>::new(&name, cancel.clone())
.notifier(shared_prioritizer.notifier())
.notifier(Arc::clone(&start_notify))
.bulkhead(Bulkhead::new(
&name,
BulkheadConfig {
semaphore: ConcurrencyLimit::Tiered {
guaranteed: Arc::clone(&guaranteed_sem),
shared: Arc::clone(&shared_sem),
},
backoff: BackoffConfig::default(),
steady_pace: self.steady_pace,
},
))
.listener(TracingListener)
.on_panic(PanicPolicy::CatchAndRetry);
if let Some(ref registry) = stats_registry_shared {
let stats = StatsListener::new(Box::new(|any| {
any.downcast_ref::<SequencerReport>()
.map_or(0, |r| u64::from(r.rows_claimed))
}));
if let Ok(mut reg) = registry.lock() {
reg.register("sequencer".to_owned(), stats.clone());
}
builder = builder.listener(stats);
}
let worker = builder.build(sequencer);
task_set.spawn(&name, worker.run());
}
// Spawn cold reconciler as a WorkerAction (ungated, poker-driven)
{
let reconciler = super::workers::reconciler::ColdReconciler {
outbox: Arc::clone(&outbox),
db: self.db.clone(),
prioritizer: Arc::clone(&shared_prioritizer),
};
let name = "cold-reconciler";
let worker = WorkerBuilder::new(name, cancel.clone())
.with_poker(self.poker_interval)
.notifier(Arc::clone(&start_notify))
.listener(TracingListener)
.on_panic(PanicPolicy::CatchAndRetry)
.build(reconciler);
task_set.spawn(name, worker.run());
}
// Spawn parallel vacuum workers (one per shared permit)
for i in 0..shared {
#[allow(unused_mut)]
let vacuum = VacuumTask::new(self.db.clone(), self.vacuum_cooldown);
let name = format!("vacuum-{i}");
let mut builder = WorkerBuilder::<VacuumReport>::new(&name, cancel.clone())
.with_poker(self.vacuum_cooldown)
.notifier(Arc::clone(&start_notify))
.bulkhead(Bulkhead::new(
&name,
BulkheadConfig {
semaphore: ConcurrencyLimit::Fixed(Arc::clone(&shared_sem)),
backoff: BackoffConfig {
initial: Duration::from_millis(500),
max: Duration::from_secs(60),
..Default::default()
},
steady_pace: Duration::ZERO,
},
))
.listener(TracingListener)
.on_panic(PanicPolicy::CatchAndRetry);
if let Some(ref registry) = stats_registry_shared {
let stats = StatsListener::new(Box::new(|any| {
any.downcast_ref::<VacuumReport>()
.map_or(0, |r| r.rows_deleted)
}));
if let Ok(mut reg) = registry.lock() {
reg.register("vacuum".to_owned(), stats.clone());
}
builder = builder.listener(stats);
}
let worker = builder.build(vacuum);
task_set.spawn(&name, worker.run());
}
// Spawn stats reporter (if enabled)
if let Some(interval) = self.stats_interval {
// Extract the registry — all workers have registered by now.
#[allow(clippy::expect_used)]
let registry = stats_registry_shared
.expect("stats_registry_shared is Some when stats_interval is Some")
.lock()
.ok()
.map(|mut guard| std::mem::replace(&mut *guard, StatsRegistry::new()))
.expect("stats registry mutex not poisoned");
let reporter = StatsReporter::new(Arc::new(registry));
let name = "stats-reporter";
let worker = WorkerBuilder::new(name, cancel.clone())
.with_poker(interval)
.on_panic(PanicPolicy::CatchAndRetry)
.build(reporter);
task_set.spawn(name, worker.run());
}
start_notify.notify_waiters();
Ok(OutboxHandle {
outbox,
tasks: task_set,
})
}
}
/// A running outbox pipeline. Obtained by calling [`OutboxBuilder::start()`].
///
/// Provides access to the [`Outbox`] for enqueue operations and a
/// [`stop()`](Self::stop) method for graceful shutdown.
///
/// Drop safety: if `stop()` is never called, `TaskSet::Drop` cancels the
/// cancellation token, signaling all workers to exit.
pub struct OutboxHandle {
outbox: Arc<Outbox>,
tasks: TaskSet,
}
impl OutboxHandle {
/// Returns the outbox for enqueue operations.
#[must_use]
pub fn outbox(&self) -> &Arc<Outbox> {
&self.outbox
}
/// Cancel background tasks and join all handles. Consumes self.
pub async fn stop(self) {
self.tasks.shutdown().await;
}
}