xds-cache 0.1.0

High-performance snapshot cache for xDS resources
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Cache trait and ShardedCache implementation.
//!
//! The cache stores snapshots keyed by node hash. The [`ShardedCache`]
//! implementation uses `DashMap` for lock-free concurrent access.

use std::sync::Arc;

use dashmap::DashMap;
use tracing::{debug, trace};
use xds_core::NodeHash;

use crate::snapshot::Snapshot;
use crate::stats::CacheStats;
use crate::watch::WatchManager;

/// Trait for xDS snapshot caches.
///
/// Provides the interface for storing and retrieving snapshots.
pub trait Cache: Send + Sync {
    /// Get a snapshot for a node.
    fn get_snapshot(&self, node: NodeHash) -> Option<Arc<Snapshot>>;

    /// Set a snapshot for a node.
    ///
    /// This will notify any watches for this node.
    fn set_snapshot(&self, node: NodeHash, snapshot: Snapshot);

    /// Clear the snapshot for a node.
    fn clear_snapshot(&self, node: NodeHash);

    /// Get the number of cached snapshots.
    fn snapshot_count(&self) -> usize;
}

/// A high-performance sharded cache using DashMap.
///
/// This cache implementation:
/// - Uses `DashMap` for lock-free concurrent reads
/// - Automatically notifies watches on snapshot updates
/// - Tracks statistics for monitoring
///
/// ## Thread Safety
///
/// All operations are thread-safe. The cache uses `DashMap` internally,
/// which provides fine-grained locking at the bucket level rather than
/// a global lock.
///
/// ## Important
///
/// All `DashMap` references are dropped before any async operations
/// to prevent holding locks across await points.
#[derive(Debug)]
pub struct ShardedCache {
    /// Snapshots keyed by node hash.
    snapshots: DashMap<NodeHash, Arc<Snapshot>>,
    /// Watch manager for notifications.
    watches: WatchManager,
    /// Statistics.
    stats: CacheStats,
}

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

impl ShardedCache {
    /// Create a new sharded cache with default settings.
    pub fn new() -> Self {
        Self::with_capacity(64)
    }

    /// Create a new sharded cache with a specific initial capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            snapshots: DashMap::with_capacity(capacity),
            watches: WatchManager::new(),
            stats: CacheStats::new(),
        }
    }

    /// Get the watch manager for creating watches.
    #[inline]
    pub fn watches(&self) -> &WatchManager {
        &self.watches
    }

    /// Get cache statistics.
    #[inline]
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }

    /// Create a watch for a node.
    ///
    /// The watch will receive updates when the snapshot for this node changes.
    /// If a snapshot already exists, the caller should check with `get_snapshot`
    /// first.
    #[inline]
    pub fn create_watch(&self, node: NodeHash) -> crate::watch::Watch {
        self.watches.create_watch(node)
    }

    /// Cancel a watch.
    #[inline]
    pub fn cancel_watch(&self, watch_id: crate::watch::WatchId) {
        self.watches.cancel_watch(watch_id)
    }

    /// Get all node hashes in the cache.
    pub fn nodes(&self) -> Vec<NodeHash> {
        self.snapshots.iter().map(|r| *r.key()).collect()
    }

    /// Check if a snapshot exists for a node.
    pub fn has_snapshot(&self, node: NodeHash) -> bool {
        self.snapshots.contains_key(&node)
    }

    /// Iterate over all snapshots.
    ///
    /// Note: This acquires read locks on all shards.
    pub fn iter(&self) -> impl Iterator<Item = (NodeHash, Arc<Snapshot>)> + '_ {
        self.snapshots
            .iter()
            .map(|r| (*r.key(), Arc::clone(r.value())))
    }
}

impl Cache for ShardedCache {
    fn get_snapshot(&self, node: NodeHash) -> Option<Arc<Snapshot>> {
        // DashMap::get returns a Ref that holds a read lock.
        // We clone the Arc and drop the Ref immediately.
        let result = self.snapshots.get(&node).map(|r| Arc::clone(&*r));

        if result.is_some() {
            self.stats.record_hit();
            trace!(node = %node, "cache hit");
        } else {
            self.stats.record_miss();
            trace!(node = %node, "cache miss");
        }

        result
    }

    fn set_snapshot(&self, node: NodeHash, snapshot: Snapshot) {
        let snapshot = Arc::new(snapshot);

        // Insert snapshot (DashMap insert is lock-free for the caller)
        self.snapshots.insert(node, Arc::clone(&snapshot));
        self.stats.record_set();

        debug!(
            node = %node,
            version = %snapshot.version(),
            resources = snapshot.total_resources(),
            "set snapshot"
        );

        // Notify watches (no DashMap lock held)
        self.watches.notify(node, snapshot);
    }

    fn clear_snapshot(&self, node: NodeHash) {
        if self.snapshots.remove(&node).is_some() {
            self.stats.record_clear();
            debug!(node = %node, "cleared snapshot");
        }
    }

    fn snapshot_count(&self) -> usize {
        self.snapshots.len()
    }
}

/// Builder for creating a configured cache.
#[derive(Debug, Default)]
#[allow(dead_code)] // Public API surface
pub struct CacheBuilder {
    capacity: Option<usize>,
    watch_buffer_size: Option<usize>,
}

#[allow(dead_code)] // Public API surface
impl CacheBuilder {
    /// Create a new cache builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the initial capacity.
    pub fn capacity(mut self, capacity: usize) -> Self {
        self.capacity = Some(capacity);
        self
    }

    /// Set the watch channel buffer size.
    pub fn watch_buffer_size(mut self, size: usize) -> Self {
        self.watch_buffer_size = Some(size);
        self
    }

    /// Build the cache.
    pub fn build(self) -> ShardedCache {
        let capacity = self.capacity.unwrap_or(64);
        let watch_buffer = self.watch_buffer_size.unwrap_or(16);

        ShardedCache {
            snapshots: DashMap::with_capacity(capacity),
            watches: WatchManager::with_buffer_size(watch_buffer),
            stats: CacheStats::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::thread;
    use std::time::Duration;

    #[test]
    fn cache_basic_operations() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("test-node");

        // Initially empty
        assert!(cache.get_snapshot(node).is_none());
        assert_eq!(cache.snapshot_count(), 0);

        // Set a snapshot
        let snapshot = Snapshot::builder().version("v1").build();
        cache.set_snapshot(node, snapshot);

        // Now exists
        assert!(cache.has_snapshot(node));
        assert_eq!(cache.snapshot_count(), 1);

        let retrieved = cache.get_snapshot(node).unwrap();
        assert_eq!(retrieved.version(), "v1");

        // Clear
        cache.clear_snapshot(node);
        assert!(!cache.has_snapshot(node));
        assert_eq!(cache.snapshot_count(), 0);
    }

    #[test]
    fn cache_stats_tracking() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("test-node");

        // Miss
        cache.get_snapshot(node);
        assert_eq!(cache.stats().snapshot_misses(), 1);

        // Set
        cache.set_snapshot(node, Snapshot::builder().version("v1").build());
        assert_eq!(cache.stats().snapshots_set(), 1);

        // Hit
        cache.get_snapshot(node);
        assert_eq!(cache.stats().snapshot_hits(), 1);
    }

    #[tokio::test]
    async fn cache_watch_notification() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("test-node");

        let mut watch = cache.create_watch(node);

        // Set snapshot
        cache.set_snapshot(node, Snapshot::builder().version("v1").build());

        // Watch should receive it
        let snapshot = watch.recv().await.unwrap();
        assert_eq!(snapshot.version(), "v1");
    }

    #[test]
    fn cache_builder() {
        let cache = CacheBuilder::new()
            .capacity(128)
            .watch_buffer_size(32)
            .build();

        assert_eq!(cache.snapshot_count(), 0);
    }

    // === Concurrent Access Tests ===

    #[test]
    fn cache_concurrent_reads() {
        let cache = Arc::new(ShardedCache::new());
        let node = NodeHash::from_id("test-node");

        // Pre-populate cache
        cache.set_snapshot(node, Snapshot::builder().version("v1").build());

        let read_count = Arc::new(AtomicUsize::new(0));
        let mut handles = vec![];

        // Spawn 10 reader threads
        for _ in 0..10 {
            let cache = Arc::clone(&cache);
            let count = Arc::clone(&read_count);
            handles.push(thread::spawn(move || {
                for _ in 0..100 {
                    if cache.get_snapshot(node).is_some() {
                        count.fetch_add(1, Ordering::Relaxed);
                    }
                }
            }));
        }

        for handle in handles {
            handle.join().expect("Thread panicked");
        }

        // All reads should succeed
        assert_eq!(read_count.load(Ordering::Relaxed), 1000);
    }

    #[test]
    fn cache_concurrent_writes() {
        let cache = Arc::new(ShardedCache::new());
        let mut handles = vec![];

        // Spawn 10 writer threads, each writing to different nodes
        for i in 0..10 {
            let cache = Arc::clone(&cache);
            handles.push(thread::spawn(move || {
                for j in 0..100 {
                    let node = NodeHash::from_id(&format!("node-{}-{}", i, j));
                    cache
                        .set_snapshot(node, Snapshot::builder().version(format!("v{}", j)).build());
                }
            }));
        }

        for handle in handles {
            handle.join().expect("Thread panicked");
        }

        // All 1000 nodes should be in cache
        assert_eq!(cache.snapshot_count(), 1000);
    }

    #[test]
    fn cache_concurrent_read_write() {
        let cache = Arc::new(ShardedCache::new());
        let node = NodeHash::from_id("contended-node");

        // Pre-populate
        cache.set_snapshot(node, Snapshot::builder().version("v0").build());

        let reads = Arc::new(AtomicUsize::new(0));
        let writes = Arc::new(AtomicUsize::new(0));
        let mut handles = vec![];

        // Writer thread
        {
            let cache = Arc::clone(&cache);
            let writes = Arc::clone(&writes);
            handles.push(thread::spawn(move || {
                for i in 1..=50 {
                    cache
                        .set_snapshot(node, Snapshot::builder().version(format!("v{}", i)).build());
                    writes.fetch_add(1, Ordering::Relaxed);
                    thread::sleep(Duration::from_micros(100));
                }
            }));
        }

        // Reader threads
        for _ in 0..5 {
            let cache = Arc::clone(&cache);
            let reads = Arc::clone(&reads);
            handles.push(thread::spawn(move || {
                for _ in 0..100 {
                    if cache.get_snapshot(node).is_some() {
                        reads.fetch_add(1, Ordering::Relaxed);
                    }
                    thread::sleep(Duration::from_micros(50));
                }
            }));
        }

        for handle in handles {
            handle.join().expect("Thread panicked");
        }

        assert_eq!(writes.load(Ordering::Relaxed), 50);
        // All reads should succeed (snapshot always exists)
        assert_eq!(reads.load(Ordering::Relaxed), 500);
    }

    // === Large Snapshot Tests ===

    #[test]
    fn cache_many_nodes() {
        let cache = ShardedCache::with_capacity(10000);

        // Add 10,000 nodes
        for i in 0..10000 {
            let node = NodeHash::from_id(&format!("node-{}", i));
            cache.set_snapshot(node, Snapshot::builder().version(format!("v{}", i)).build());
        }

        assert_eq!(cache.snapshot_count(), 10000);

        // Verify random access
        for i in [0, 999, 5000, 9999] {
            let node = NodeHash::from_id(&format!("node-{}", i));
            let snap = cache.get_snapshot(node).unwrap();
            assert_eq!(snap.version(), format!("v{}", i));
        }
    }

    #[test]
    fn cache_snapshot_update() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("test-node");

        // Initial version
        cache.set_snapshot(node, Snapshot::builder().version("v1").build());
        assert_eq!(cache.get_snapshot(node).unwrap().version(), "v1");

        // Update version
        cache.set_snapshot(node, Snapshot::builder().version("v2").build());
        assert_eq!(cache.get_snapshot(node).unwrap().version(), "v2");

        // Stats should show 2 sets
        assert_eq!(cache.stats().snapshots_set(), 2);
    }

    // === Watch Tests ===

    #[tokio::test]
    async fn cache_multiple_watches_same_node() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("test-node");

        let mut watch1 = cache.create_watch(node);
        let mut watch2 = cache.create_watch(node);

        // Set snapshot
        cache.set_snapshot(node, Snapshot::builder().version("v1").build());

        // Both watches should receive it
        let snap1 = watch1.recv().await.unwrap();
        let snap2 = watch2.recv().await.unwrap();
        assert_eq!(snap1.version(), "v1");
        assert_eq!(snap2.version(), "v1");
    }

    #[tokio::test]
    async fn cache_watch_receives_updates() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("test-node");

        let mut watch = cache.create_watch(node);

        // Send multiple updates
        for i in 1..=3 {
            cache.set_snapshot(node, Snapshot::builder().version(format!("v{}", i)).build());
        }

        // Watch should receive all updates (buffered)
        let snap1 = watch.recv().await.unwrap();
        assert_eq!(snap1.version(), "v1");

        let snap2 = watch.recv().await.unwrap();
        assert_eq!(snap2.version(), "v2");

        let snap3 = watch.recv().await.unwrap();
        assert_eq!(snap3.version(), "v3");
    }

    // === Edge Cases ===

    #[test]
    fn cache_clear_nonexistent_node() {
        let cache = ShardedCache::new();
        let node = NodeHash::from_id("nonexistent");

        // Should not panic
        cache.clear_snapshot(node);
        assert_eq!(cache.snapshot_count(), 0);
    }

    #[test]
    fn cache_wildcard_node() {
        let cache = ShardedCache::new();
        let wildcard = NodeHash::wildcard();

        cache.set_snapshot(wildcard, Snapshot::builder().version("v1").build());
        assert!(cache.has_snapshot(wildcard));

        let snap = cache.get_snapshot(wildcard).unwrap();
        assert_eq!(snap.version(), "v1");
    }

    #[test]
    fn cache_node_hash_collision_unlikely() {
        // FNV-1a should give different hashes for similar strings
        let node1 = NodeHash::from_id("node-1");
        let node2 = NodeHash::from_id("node-2");
        let node3 = NodeHash::from_id("1-node");

        // All should be different (this is a sanity check, not guaranteed)
        assert_ne!(node1, node2);
        assert_ne!(node2, node3);
        assert_ne!(node1, node3);
    }
}