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
//! Public `ConsumerGroup<B, Ctx>` -- specialist harness for coordinated
//! consumer groups. Gated on `B: HasCoordinatedGroups`.
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use crate::autoscaler::AutoscalerConfig;
use crate::backend::RegistryImpl;
use crate::backend::capability::HasCoordinatedGroups;
use crate::consumer_supervisor::SupervisorOutcome;
use crate::error::{Result, ShoveError};
use crate::handler::MessageHandler;
#[cfg(feature = "kafka")]
use crate::markers::Kafka;
use crate::topic::{SequencedTopic, Topic};
pub struct ConsumerGroup<B: HasCoordinatedGroups, Ctx: Clone + Send + Sync + 'static = ()> {
pub(crate) inner: B::RegistryImpl,
client: B::Client,
autoscaler: Option<AutoscalerConfig>,
ctx: Ctx,
}
pub struct ConsumerGroupConfig<B: HasCoordinatedGroups> {
pub(crate) inner: B::ConsumerGroupConfig,
}
impl<B: HasCoordinatedGroups> ConsumerGroupConfig<B> {
pub fn new(inner: B::ConsumerGroupConfig) -> Self {
Self { inner }
}
}
impl<B: HasCoordinatedGroups> ConsumerGroup<B, ()> {
pub(crate) fn new(inner: B::RegistryImpl, client: B::Client) -> Self {
Self {
inner,
client,
autoscaler: None,
ctx: (),
}
}
pub fn with_context<Ctx: Clone + Send + Sync + 'static>(
self,
ctx: Ctx,
) -> ConsumerGroup<B, Ctx> {
ConsumerGroup {
inner: self.inner,
client: self.client,
autoscaler: self.autoscaler,
ctx,
}
}
}
impl<B: HasCoordinatedGroups, Ctx: Clone + Send + Sync + 'static> ConsumerGroup<B, Ctx> {
/// Register a concurrent (unsequenced) topic handler.
///
/// The backend **automatically declares** the required broker topology
/// (queues, streams, topics, consumer groups — whatever the backend
/// needs) before returning. Callers do **not** need to call
/// `broker.topology().declare::<T>()` first; doing so is harmless but
/// redundant. This guarantee holds across all backends (Redis, InMemory,
/// RabbitMQ, NATS, Kafka, SQS).
///
/// Returns an error if:
/// - `T` has a sequencing config — use [`register_fifo`] instead.
/// - The topic is already registered in this registry.
/// - Topology declaration fails (e.g. broker unreachable).
///
/// [`register_fifo`]: Self::register_fifo
pub async fn register<T, H>(
&mut self,
config: ConsumerGroupConfig<B>,
factory: impl Fn() -> H + Send + Sync + 'static,
) -> Result<()>
where
T: Topic,
H: MessageHandler<T, Context = Ctx>,
{
if T::topology().sequencing().is_some() {
return Err(ShoveError::Topology(format!(
"topic '{}' has a sequencing config; `ConsumerGroup::register` \
would silently drop FIFO ordering. Use `register_fifo` instead.",
T::topology().queue(),
)));
}
self.inner
.register::<T, H>(config.inner, factory, self.ctx.clone())
.await
}
/// Register a FIFO (sequenced) topic handler.
///
/// The backend **automatically declares** the required broker topology
/// (queues, streams, shard queues, consumer groups — whatever the backend
/// needs) before returning. Callers do **not** need to call
/// `broker.topology().declare::<T>()` first; doing so is harmless but
/// redundant. This guarantee holds across all backends (Redis, InMemory,
/// RabbitMQ, NATS, Kafka, SQS).
///
/// Returns an error if:
/// - `T`'s topology has no sequencing config — use [`register`] instead.
/// - The topic is already registered in this registry.
/// - Topology declaration fails (e.g. broker unreachable).
///
/// [`register`]: Self::register
pub async fn register_fifo<T, H>(
&mut self,
config: ConsumerGroupConfig<B>,
factory: impl Fn() -> H + Send + Sync + 'static,
) -> Result<()>
where
T: SequencedTopic,
H: MessageHandler<T, Context = Ctx>,
{
if T::topology().sequencing().is_none() {
return Err(ShoveError::Topology(format!(
"topic '{}' implements `SequencedTopic` but its topology has no \
sequencing config; `ConsumerGroup::register_fifo` would attach to \
FIFO shard queues that were never declared. Use `register` for \
unsequenced topics, or add `.sequenced(...)` to the topology.",
T::topology().queue(),
)));
}
self.inner
.register_fifo::<T, H>(config.inner, factory, self.ctx.clone())
.await
}
/// Set a default handler timeout applied to every group registered
/// through this `ConsumerGroup` whose per-group config did not call
/// `with_handler_timeout`. Per-group explicit settings always win.
///
/// Must be called **before** [`register`] / [`register_fifo`]. Each
/// backend resolves the effective handler timeout at registration
/// time, so a call after the first registration silently has no
/// effect on the already-registered group.
///
/// `broker.consumer_group()` also constructs a fresh registry on
/// every call, so a default set on one returned handle does not
/// propagate to subsequent `broker.consumer_group()` results.
///
/// [`register`]: Self::register
/// [`register_fifo`]: Self::register_fifo
pub fn with_default_handler_timeout(mut self, timeout: std::time::Duration) -> Self {
assert!(
!timeout.is_zero(),
"default_handler_timeout must be positive"
);
self.inner.set_default_handler_timeout(timeout);
self
}
pub fn cancellation_token(&self) -> CancellationToken {
self.inner.cancellation_token()
}
/// Enable autoscaling for this group. The autoscaler polls queue depth on
/// `config.poll_interval` and scales consumers within the per-group
/// `ConsumerGroupConfig` min/max range using `Stabilized<ThresholdStrategy>`.
/// Its lifecycle is owned by [`run_until_timeout`](Self::run_until_timeout):
/// it is stopped and joined before consumers drain. A panic in the
/// autoscaler task (observed before the stop deadline) surfaces in the
/// returned [`SupervisorOutcome`] as a panic.
pub fn enable_autoscaling(mut self, config: AutoscalerConfig) -> Self {
self.autoscaler = Some(config);
self
}
pub async fn run_until_timeout<S>(self, signal: S, drain_timeout: Duration) -> SupervisorOutcome
where
S: Future<Output = ()> + Send + 'static,
{
let Some(config) = self.autoscaler else {
return self.inner.run_until_timeout(signal, drain_timeout).await;
};
let mut inner = self.inner;
// Token we race the external signal against; cancelling it cascades to
// consumers exactly as the non-autoscaling path's broker token does.
let consumer_token = inner.cancellation_token();
inner.start_all();
let registry = Arc::new(Mutex::new(inner));
// Dedicated token so we can stop the autoscaler *before* draining
// consumers (ordering required by the shutdown contract).
let auto_token = CancellationToken::new();
let handle =
B::spawn_autoscaler(&self.client, registry.clone(), config, auto_token.clone());
// Wait for the external signal or an externally-triggered cancel.
let mut signal_task = tokio::spawn(signal);
tokio::select! {
_ = consumer_token.cancelled() => { signal_task.abort(); }
res = &mut signal_task => { let _ = res; }
}
// Stop the autoscaler first; bounded-join so a stuck metrics poll can't
// consume the drain budget. Count a panic observed before the deadline.
auto_token.cancel();
let mut autoscaler_panics = 0usize;
let grace = Duration::from_secs(1);
let mut handle = handle;
tokio::select! {
res = &mut handle => {
if matches!(&res, Err(e) if e.is_panic()) {
autoscaler_panics += 1;
}
}
_ = tokio::time::sleep(grace) => {
handle.abort();
if let Err(e) = handle.await
&& e.is_panic()
{
autoscaler_panics += 1;
}
}
}
// Autoscaler task (and its registry Arc clone) is now gone: sole owner.
let inner = Arc::try_unwrap(registry)
.unwrap_or_else(|_| unreachable!("autoscaler joined; registry Arc must be sole-owned"))
.into_inner();
// Broadcast shutdown to every registered group at once before draining,
// matching the non-autoscaling lifecycle: the non-autoscaling path calls
// broker_token.cancel() up-front so all group tokens are cancelled
// concurrently before any sequential per-group drain begins.
// Without this, drain_all_into cancels groups one at a time as it
// iterates, so groups 2..N keep consuming until the sequential drain
// reaches them.
consumer_token.cancel();
let mut outcome = inner.drain_until_timeout(drain_timeout).await;
outcome.panics += autoscaler_panics;
outcome
}
}
#[cfg(feature = "kafka")]
#[cfg_attr(docsrs, doc(cfg(feature = "kafka")))]
impl<Ctx: Clone + Send + Sync + 'static> ConsumerGroup<Kafka, Ctx> {
/// Replication factor applied to every topic auto-declared when a group is
/// registered via [`register`](Self::register) / [`register_fifo`](Self::register_fifo).
/// Defaults to `1` (single-broker dev) when unset; production clusters
/// should set `>= 3`. Mirrors [`with_default_handler_timeout`](Self::with_default_handler_timeout):
/// a registry-level default reached directly from `broker.consumer_group()`
/// instead of only via the low-level `KafkaConsumerGroupRegistry`.
///
/// # Panics
///
/// Panics if `n < 1`.
pub fn with_default_replication_factor(mut self, n: i32) -> Self {
self.inner = self.inner.with_default_replication_factor(n);
self
}
}
#[cfg(all(test, feature = "inmemory"))]
mod tests {
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::autoscaler::AutoscalerConfig;
use crate::define_sequenced_topic;
use crate::define_topic;
use crate::inmemory::{InMemoryConfig, InMemoryConsumerGroupConfig};
use crate::topic::SequencedTopic;
use crate::topology::{SequenceFailure, TopologyBuilder};
use crate::{
Broker, ConsumerGroupConfig, InMemory, MessageHandler, MessageMetadata, Outcome, ShoveError,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct LedgerEntry {
account_id: String,
}
define_sequenced_topic!(
Ledger,
LedgerEntry,
|msg| msg.account_id.clone(),
TopologyBuilder::new("ledger-test")
.sequenced(SequenceFailure::FailAll)
.hold_queue(Duration::from_millis(50))
.dlq()
.build()
);
struct NoopHandler;
impl MessageHandler<Ledger> for NoopHandler {
type Context = ();
async fn handle(&self, _: LedgerEntry, _: MessageMetadata, _: &()) -> Outcome {
Outcome::Ack
}
}
#[tokio::test]
async fn register_rejects_sequenced_topic() {
let broker = Broker::<InMemory>::new(InMemoryConfig::default())
.await
.expect("broker");
broker
.topology()
.declare::<Ledger>()
.await
.expect("declare");
let mut group = broker.consumer_group();
let result = group
.register::<Ledger, _>(
ConsumerGroupConfig::new(InMemoryConsumerGroupConfig::new(1..=1)),
|| NoopHandler,
)
.await;
match result {
Err(ShoveError::Topology(msg)) => {
assert!(
msg.contains("sequencing config") && msg.contains("register_fifo"),
"unexpected error message: {msg}"
);
}
other => panic!("expected Topology error, got {other:?}"),
}
// Drain shouldn't hang on a group that never registered anything.
let outcome = group
.run_until_timeout(std::future::ready(()), Duration::from_millis(100))
.await;
assert_eq!(outcome.exit_code(), 0);
}
#[tokio::test]
async fn register_fifo_drains_through_run_until_timeout() {
let broker = Broker::<InMemory>::new(InMemoryConfig::default())
.await
.expect("broker");
broker
.topology()
.declare::<Ledger>()
.await
.expect("declare");
let mut group = broker.consumer_group();
group
.register_fifo::<Ledger, _>(
ConsumerGroupConfig::new(InMemoryConsumerGroupConfig::default()),
|| NoopHandler,
)
.await
.expect("register_fifo");
let outcome = group
.run_until_timeout(std::future::ready(()), Duration::from_millis(500))
.await;
assert_eq!(outcome.exit_code(), 0);
}
#[tokio::test]
async fn with_default_handler_timeout_chains_and_runs_clean() {
let broker = Broker::<InMemory>::new(InMemoryConfig::default())
.await
.expect("broker");
broker
.topology()
.declare::<Ledger>()
.await
.expect("declare");
let mut group = broker
.consumer_group()
.with_default_handler_timeout(Duration::from_secs(5));
group
.register_fifo::<Ledger, _>(
ConsumerGroupConfig::new(InMemoryConsumerGroupConfig::default()),
|| NoopHandler,
)
.await
.expect("register_fifo");
let outcome = group
.run_until_timeout(std::future::ready(()), Duration::from_millis(500))
.await;
assert_eq!(outcome.exit_code(), 0);
}
#[tokio::test]
#[should_panic(expected = "default_handler_timeout must be positive")]
async fn with_default_handler_timeout_zero_panics() {
let broker = Broker::<InMemory>::new(InMemoryConfig::default())
.await
.expect("broker");
let _ = broker
.consumer_group()
.with_default_handler_timeout(Duration::ZERO);
}
// Two unsequenced topics used by the two-group autoscaling shutdown test.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct AlphaEvent {
id: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct BetaEvent {
id: u32,
}
define_topic!(
AlphaTopic,
AlphaEvent,
TopologyBuilder::new("alpha-autoscale-test").build()
);
define_topic!(
BetaTopic,
BetaEvent,
TopologyBuilder::new("beta-autoscale-test").build()
);
struct AlphaHandler;
impl MessageHandler<AlphaTopic> for AlphaHandler {
type Context = ();
async fn handle(&self, _: AlphaEvent, _: MessageMetadata, _: &()) -> Outcome {
Outcome::Ack
}
}
struct BetaHandler;
impl MessageHandler<BetaTopic> for BetaHandler {
type Context = ();
async fn handle(&self, _: BetaEvent, _: MessageMetadata, _: &()) -> Outcome {
Outcome::Ack
}
}
/// Regression test for the autoscaling broadcast-cancel fix.
///
/// Two groups are registered, autoscaling is enabled, and an immediate
/// shutdown signal is sent. With the fix, `consumer_token.cancel()` is
/// called before `drain_until_timeout`, broadcasting shutdown to BOTH
/// group tokens at once. Without the fix, groups are cancelled sequentially
/// inside `drain_all_into`, which means group 2's token is not cancelled
/// until after group 1 fully drains.
///
/// This test verifies a clean outcome (exit_code 0, not timed out) within
/// a drain_timeout that is sufficient for concurrent cancellation but would
/// be tight for sequential cancellation if consumers were doing slow work.
/// With an empty queue the consumers are idle, so the timing difference is
/// not observable here; what the test guarantees is that the two-group
/// autoscaling shutdown path completes correctly and reports a clean outcome.
#[tokio::test]
async fn autoscaling_two_groups_broadcast_cancel_before_drain() {
let broker = Broker::<InMemory>::new(InMemoryConfig::default())
.await
.expect("broker");
let mut group = broker.consumer_group();
group
.register::<AlphaTopic, _>(
ConsumerGroupConfig::new(InMemoryConsumerGroupConfig::new(1..=2)),
|| AlphaHandler,
)
.await
.expect("register AlphaTopic");
group
.register::<BetaTopic, _>(
ConsumerGroupConfig::new(InMemoryConsumerGroupConfig::new(1..=2)),
|| BetaHandler,
)
.await
.expect("register BetaTopic");
// Immediate signal: shutdown fires before any polling cycle.
let outcome = group
.enable_autoscaling(AutoscalerConfig {
poll_interval: Duration::from_millis(50),
..AutoscalerConfig::default()
})
.run_until_timeout(std::future::ready(()), Duration::from_millis(500))
.await;
assert_eq!(
outcome.exit_code(),
0,
"two-group autoscaling shutdown must be clean: {outcome:?}"
);
assert!(
!outcome.timed_out,
"drain must not time out with broadcast cancel: {outcome:?}"
);
}
#[tokio::test]
async fn enable_autoscaling_runs_and_drains_clean() {
let broker = Broker::<InMemory>::new(InMemoryConfig::default())
.await
.expect("broker");
broker
.topology()
.declare::<Ledger>()
.await
.expect("declare");
let mut group = broker.consumer_group();
group
.register_fifo::<Ledger, _>(
ConsumerGroupConfig::new(InMemoryConsumerGroupConfig::default()),
|| NoopHandler,
)
.await
.expect("register_fifo");
let outcome = group
.enable_autoscaling(AutoscalerConfig {
poll_interval: Duration::from_millis(50),
..AutoscalerConfig::default()
})
.run_until_timeout(
async { tokio::time::sleep(Duration::from_millis(150)).await },
Duration::from_millis(500),
)
.await;
assert_eq!(outcome.exit_code(), 0);
}
}