allsource-core 0.19.1

High-performance event store core built in Rust
Documentation
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
use crate::{
    domain::value_objects::system_stream::{SystemDomain, consumer_events, system_entity_id_value},
    infrastructure::persistence::SystemMetadataStore,
};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::Arc;

/// A durable consumer tracks a cursor (last-acknowledged position) so it can
/// resume from where it left off after disconnection or restart.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Consumer {
    pub consumer_id: String,
    /// Event type prefix filters (e.g. `["scheduler.*", "index.*"]`).
    /// Empty means "all events".
    pub event_type_filters: Vec<String>,
    /// Global event offset of the last acknowledged event.
    /// Events after this offset are "unprocessed" for this consumer.
    /// `None` means the consumer hasn't acked anything yet (start from beginning).
    pub cursor_position: Option<u64>,
}

/// Registry that manages durable consumers with persistent cursor positions.
///
/// Consumer state is stored as system events in the WAL pipeline so it
/// survives Core restarts. In-memory state is held in a DashMap for O(1) access.
///
/// Two modes:
/// - **In-memory** (`new()`): no persistence, for tests and backward compatibility
/// - **Durable** (`new_durable()`): backed by `SystemMetadataStore`, survives restarts
pub struct ConsumerRegistry {
    consumers: Arc<DashMap<String, Consumer>>,
    system_store: Option<Arc<SystemMetadataStore>>,
}

impl Default for ConsumerRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl ConsumerRegistry {
    /// Create an in-memory-only consumer registry (no persistence).
    pub fn new() -> Self {
        Self {
            consumers: Arc::new(DashMap::new()),
            system_store: None,
        }
    }

    /// Create a durable consumer registry backed by `SystemMetadataStore`.
    ///
    /// On construction, replays all `_system.consumer.*` events to rebuild
    /// the in-memory cache from WAL.
    pub fn new_durable(system_store: Arc<SystemMetadataStore>) -> Self {
        let registry = Self {
            consumers: Arc::new(DashMap::new()),
            system_store: Some(system_store),
        };
        registry.rebuild_cache();
        registry
    }

    /// Register a consumer (or update its filters if it already exists).
    pub fn register(&self, consumer_id: &str, event_type_filters: &[String]) -> Consumer {
        let consumer = self
            .consumers
            .entry(consumer_id.to_string())
            .or_insert_with(|| Consumer {
                consumer_id: consumer_id.to_string(),
                event_type_filters: event_type_filters.to_vec(),
                cursor_position: None,
            });

        // Update filters if consumer already existed
        let mut c = consumer.clone();
        if c.event_type_filters != event_type_filters {
            drop(consumer);
            self.consumers.alter(consumer_id, |_, mut existing| {
                existing.event_type_filters = event_type_filters.to_vec();
                c = existing.clone();
                existing
            });
        }

        // Persist registration event
        if let Some(ref store) = self.system_store {
            let entity_id = system_entity_id_value(SystemDomain::Consumer, consumer_id);
            let payload = json!({
                "consumer_id": consumer_id,
                "event_type_filters": event_type_filters,
            });
            if let Err(e) =
                store.append_system_event(consumer_events::REGISTERED, entity_id, payload, None)
            {
                tracing::warn!("Failed to persist consumer registration: {}", e);
            }
        }

        c
    }

    /// Get a consumer by ID. Returns None if not registered.
    pub fn get(&self, consumer_id: &str) -> Option<Consumer> {
        self.consumers.get(consumer_id).map(|c| c.clone())
    }

    /// Get or implicitly create a consumer.
    pub fn get_or_create(&self, consumer_id: &str) -> Consumer {
        self.consumers
            .entry(consumer_id.to_string())
            .or_insert_with(|| Consumer {
                consumer_id: consumer_id.to_string(),
                event_type_filters: vec![],
                cursor_position: None,
            })
            .clone()
    }

    /// Acknowledge events up to a given global offset.
    /// Returns Ok(()) on success, Err if the position is beyond the max offset.
    pub fn ack(&self, consumer_id: &str, position: u64, max_offset: u64) -> Result<(), String> {
        if position > max_offset {
            return Err(format!(
                "Position {position} is beyond the latest event offset {max_offset}"
            ));
        }

        let mut entry = self
            .consumers
            .entry(consumer_id.to_string())
            .or_insert_with(|| Consumer {
                consumer_id: consumer_id.to_string(),
                event_type_filters: vec![],
                cursor_position: None,
            });

        // Only advance the cursor (idempotent: acking an older position is a no-op)
        let current = entry.cursor_position.unwrap_or(0);
        if position > current {
            entry.cursor_position = Some(position);

            // Persist ack event
            if let Some(ref store) = self.system_store {
                let entity_id = system_entity_id_value(SystemDomain::Consumer, consumer_id);
                let payload = json!({
                    "consumer_id": consumer_id,
                    "position": position,
                });
                if let Err(e) = store.append_system_event(
                    consumer_events::ACK_UPDATED,
                    entity_id,
                    payload,
                    None,
                ) {
                    tracing::warn!("Failed to persist consumer ack: {}", e);
                }
            }
        }

        Ok(())
    }

    /// Restore consumer state (called during WAL recovery).
    pub fn restore(&self, consumer: Consumer) {
        self.consumers
            .insert(consumer.consumer_id.clone(), consumer);
    }

    /// Number of registered consumers.
    pub fn count(&self) -> usize {
        self.consumers.len()
    }

    /// Rebuild in-memory cache from system store events.
    fn rebuild_cache(&self) {
        let Some(ref store) = self.system_store else {
            return;
        };
        let events = store.read_stream(SystemDomain::Consumer);
        for event in &events {
            self.apply_event(event);
        }
        tracing::info!(
            "Rebuilt consumer cache: {} consumers from {} events",
            self.consumers.len(),
            events.len()
        );
    }

    /// Apply a single system event to the in-memory cache.
    fn apply_event(&self, event: &crate::domain::entities::Event) {
        let event_type = event.event_type_str();
        let payload = event.payload();

        match event_type {
            consumer_events::REGISTERED => {
                let consumer_id = payload
                    .get("consumer_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
                    .to_string();
                let event_type_filters: Vec<String> = payload
                    .get("event_type_filters")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default();

                if !consumer_id.is_empty() {
                    self.consumers.insert(
                        consumer_id.clone(),
                        Consumer {
                            consumer_id,
                            event_type_filters,
                            cursor_position: None,
                        },
                    );
                }
            }
            consumer_events::ACK_UPDATED => {
                let consumer_id = payload
                    .get("consumer_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
                    .to_string();
                let position = payload.get("position").and_then(serde_json::Value::as_u64);

                if !consumer_id.is_empty() {
                    self.consumers
                        .entry(consumer_id.clone())
                        .and_modify(|c| {
                            // Only advance — never regress
                            if let Some(pos) = position {
                                let current = c.cursor_position.unwrap_or(0);
                                if pos > current {
                                    c.cursor_position = Some(pos);
                                }
                            }
                        })
                        .or_insert_with(|| Consumer {
                            consumer_id,
                            event_type_filters: vec![],
                            cursor_position: position,
                        });
                }
            }
            consumer_events::DELETED => {
                let consumer_id = payload
                    .get("consumer_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default();
                if !consumer_id.is_empty() {
                    self.consumers.remove(consumer_id);
                }
            }
            _ => {}
        }
    }

    /// Check if an event type matches a consumer's filters.
    /// Empty filters = match all.
    pub fn matches_filters(event_type: &str, filters: &[String]) -> bool {
        if filters.is_empty() {
            return true;
        }
        filters.iter().any(|filter| {
            if let Some(prefix) = filter.strip_suffix(".*") {
                event_type.starts_with(prefix)
                    && event_type
                        .as_bytes()
                        .get(prefix.len())
                        .is_none_or(|&b| b == b'.')
            } else {
                event_type == filter
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_register_and_get() {
        let registry = ConsumerRegistry::new();
        let c = registry.register("c1", &["scheduler.*".into()]);
        assert_eq!(c.consumer_id, "c1");
        assert_eq!(c.event_type_filters, vec!["scheduler.*"]);
        assert_eq!(c.cursor_position, None);

        let fetched = registry.get("c1").unwrap();
        assert_eq!(fetched.consumer_id, "c1");
    }

    #[test]
    fn test_get_or_create() {
        let registry = ConsumerRegistry::new();
        assert!(registry.get("c1").is_none());

        let c = registry.get_or_create("c1");
        assert_eq!(c.consumer_id, "c1");
        assert!(c.event_type_filters.is_empty());

        // Second call returns same consumer
        let c2 = registry.get_or_create("c1");
        assert_eq!(c2.consumer_id, "c1");
    }

    #[test]
    fn test_ack_advances_cursor() {
        let registry = ConsumerRegistry::new();
        registry.register("c1", &[]);

        registry.ack("c1", 5, 10).unwrap();
        assert_eq!(registry.get("c1").unwrap().cursor_position, Some(5));

        // Advance further
        registry.ack("c1", 8, 10).unwrap();
        assert_eq!(registry.get("c1").unwrap().cursor_position, Some(8));
    }

    #[test]
    fn test_ack_idempotent_no_regression() {
        let registry = ConsumerRegistry::new();
        registry.register("c1", &[]);

        registry.ack("c1", 5, 10).unwrap();
        // Acking an earlier position is a no-op
        registry.ack("c1", 3, 10).unwrap();
        assert_eq!(registry.get("c1").unwrap().cursor_position, Some(5));
    }

    #[test]
    fn test_ack_beyond_max_fails() {
        let registry = ConsumerRegistry::new();
        registry.register("c1", &[]);

        let result = registry.ack("c1", 15, 10);
        assert!(result.is_err());
    }

    #[test]
    fn test_ack_auto_creates_consumer() {
        let registry = ConsumerRegistry::new();
        registry.ack("c1", 5, 10).unwrap();
        assert_eq!(registry.get("c1").unwrap().cursor_position, Some(5));
    }

    #[test]
    fn test_matches_filters_empty() {
        assert!(ConsumerRegistry::matches_filters("anything", &[]));
    }

    #[test]
    fn test_matches_filters_prefix() {
        let filters = vec!["scheduler.*".to_string()];
        assert!(ConsumerRegistry::matches_filters(
            "scheduler.started",
            &filters
        ));
        assert!(ConsumerRegistry::matches_filters(
            "scheduler.completed",
            &filters
        ));
        assert!(!ConsumerRegistry::matches_filters(
            "trade.executed",
            &filters
        ));
    }

    #[test]
    fn test_matches_filters_exact() {
        let filters = vec!["scheduler.started".to_string()];
        assert!(ConsumerRegistry::matches_filters(
            "scheduler.started",
            &filters
        ));
        assert!(!ConsumerRegistry::matches_filters(
            "scheduler.completed",
            &filters
        ));
    }

    #[test]
    fn test_matches_filters_multiple() {
        let filters = vec!["scheduler.*".to_string(), "index.*".to_string()];
        assert!(ConsumerRegistry::matches_filters(
            "scheduler.started",
            &filters
        ));
        assert!(ConsumerRegistry::matches_filters("index.created", &filters));
        assert!(!ConsumerRegistry::matches_filters(
            "trade.executed",
            &filters
        ));
    }

    #[test]
    fn test_restore() {
        let registry = ConsumerRegistry::new();
        registry.restore(Consumer {
            consumer_id: "c1".into(),
            event_type_filters: vec!["scheduler.*".into()],
            cursor_position: Some(42),
        });

        let c = registry.get("c1").unwrap();
        assert_eq!(c.cursor_position, Some(42));
        assert_eq!(c.event_type_filters, vec!["scheduler.*"]);
    }

    #[test]
    fn test_count() {
        let registry = ConsumerRegistry::new();
        assert_eq!(registry.count(), 0);
        registry.register("c1", &[]);
        assert_eq!(registry.count(), 1);
        registry.register("c2", &[]);
        assert_eq!(registry.count(), 2);
    }

    #[test]
    fn test_in_memory_mode_still_works() {
        // Ensure new() works without a system store
        let registry = ConsumerRegistry::new();
        registry.register("c1", &["trade.*".into()]);
        registry.ack("c1", 10, 100).unwrap();
        let c = registry.get("c1").unwrap();
        assert_eq!(c.cursor_position, Some(10));
        assert_eq!(c.event_type_filters, vec!["trade.*"]);
    }

    #[test]
    fn test_durable_register_persists() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let store = Arc::new(SystemMetadataStore::new(temp_dir.path()).unwrap());

        // Register a consumer with durable registry
        {
            let registry = ConsumerRegistry::new_durable(store.clone());
            registry.register("c1", &["scheduler.*".into()]);
            assert_eq!(registry.count(), 1);
        }

        // Recreate from same store — should recover consumer
        {
            let registry = ConsumerRegistry::new_durable(store.clone());
            assert_eq!(registry.count(), 1);
            let c = registry.get("c1").unwrap();
            assert_eq!(c.consumer_id, "c1");
            assert_eq!(c.event_type_filters, vec!["scheduler.*"]);
            assert_eq!(c.cursor_position, None);
        }
    }

    #[test]
    fn test_durable_ack_persists() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let store = Arc::new(SystemMetadataStore::new(temp_dir.path()).unwrap());

        // Register + ack
        {
            let registry = ConsumerRegistry::new_durable(store.clone());
            registry.register("c1", &[]);
            registry.ack("c1", 42, 100).unwrap();
            assert_eq!(registry.get("c1").unwrap().cursor_position, Some(42));
        }

        // Recreate — cursor should survive
        {
            let registry = ConsumerRegistry::new_durable(store.clone());
            let c = registry.get("c1").unwrap();
            assert_eq!(c.cursor_position, Some(42));
        }
    }

    #[test]
    fn test_durable_recovery_multiple_acks() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let store = Arc::new(SystemMetadataStore::new(temp_dir.path()).unwrap());

        // Ack multiple times
        {
            let registry = ConsumerRegistry::new_durable(store.clone());
            registry.register("c1", &[]);
            registry.ack("c1", 10, 100).unwrap();
            registry.ack("c1", 25, 100).unwrap();
            registry.ack("c1", 50, 100).unwrap();
        }

        // Only latest position should be recovered
        {
            let registry = ConsumerRegistry::new_durable(store.clone());
            let c = registry.get("c1").unwrap();
            assert_eq!(c.cursor_position, Some(50));
        }
    }
}