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
//! Snapshot: immutable collection of xDS resources.
//!
//! A snapshot represents a consistent view of all resources for a node
//! at a specific version. Snapshots are:
//!
//! - **Immutable**: Once created, a snapshot cannot be modified
//! - **Versioned**: Each snapshot has a version string
//! - **Type-organized**: Resources are grouped by their type URL

use std::collections::HashMap;
use std::sync::Arc;

use xds_core::{BoxResource, TypeUrl};

/// Resources for a specific type within a snapshot.
#[derive(Debug, Clone, Default)]
pub struct SnapshotResources {
    /// Version string for this resource type.
    version: String,
    /// Resources keyed by name.
    resources: HashMap<String, BoxResource>,
}

impl SnapshotResources {
    /// Create a new empty resource collection.
    pub fn new(version: impl Into<String>) -> Self {
        Self {
            version: version.into(),
            resources: HashMap::new(),
        }
    }

    /// Get the version for this resource type.
    #[inline]
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Get the number of resources.
    #[inline]
    pub fn len(&self) -> usize {
        self.resources.len()
    }

    /// Check if there are no resources.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.resources.is_empty()
    }

    /// Get a resource by name.
    #[inline]
    pub fn get(&self, name: &str) -> Option<&BoxResource> {
        self.resources.get(name)
    }

    /// Iterate over all resources.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&String, &BoxResource)> {
        self.resources.iter()
    }

    /// Get all resource names.
    #[inline]
    pub fn names(&self) -> impl Iterator<Item = &String> {
        self.resources.keys()
    }

    /// Get all resources as a vec.
    pub fn to_vec(&self) -> Vec<BoxResource> {
        self.resources.values().cloned().collect()
    }
}

/// An immutable snapshot of xDS resources for a node.
///
/// Snapshots are the primary unit of cache storage. Each snapshot
/// contains resources organized by type, with per-type versioning.
#[derive(Debug, Clone)]
pub struct Snapshot {
    /// Global version for this snapshot.
    version: String,
    /// Resources grouped by type URL.
    resources: HashMap<TypeUrl, SnapshotResources>,
    /// Creation timestamp.
    created_at: std::time::Instant,
}

impl Snapshot {
    /// Create a new snapshot builder.
    #[must_use = "builder is unused unless `.build()` is called"]
    pub fn builder() -> SnapshotBuilder {
        SnapshotBuilder::new()
    }

    /// Get the global version of this snapshot.
    #[inline]
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Get the creation timestamp.
    #[inline]
    pub fn created_at(&self) -> std::time::Instant {
        self.created_at
    }

    /// Get resources for a specific type.
    #[inline]
    pub fn get_resources(&self, type_url: TypeUrl) -> Option<&SnapshotResources> {
        self.resources.get(&type_url)
    }

    /// Get the version for a specific resource type.
    #[inline]
    pub fn get_version(&self, type_url: TypeUrl) -> Option<&str> {
        self.resources.get(&type_url).map(|r| r.version.as_str())
    }

    /// Check if this snapshot contains a specific resource type.
    #[inline]
    pub fn contains_type(&self, type_url: TypeUrl) -> bool {
        self.resources.contains_key(&type_url)
    }

    /// Get all type URLs present in this snapshot.
    pub fn type_urls(&self) -> impl Iterator<Item = &TypeUrl> {
        self.resources.keys()
    }

    /// Get the total number of resources across all types.
    pub fn total_resources(&self) -> usize {
        self.resources.values().map(|r| r.len()).sum()
    }

    /// Check if this snapshot is empty (no resources).
    pub fn is_empty(&self) -> bool {
        self.resources.is_empty() || self.resources.values().all(|r| r.is_empty())
    }
}

/// Builder for creating snapshots.
#[must_use = "builder is unused unless `.build()` is called"]
#[derive(Debug, Default)]
pub struct SnapshotBuilder {
    version: String,
    resources: HashMap<TypeUrl, SnapshotResources>,
}

impl SnapshotBuilder {
    /// Create a new snapshot builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the global version for this snapshot.
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.version = version.into();
        self
    }

    /// Add resources of a specific type.
    ///
    /// The version for this resource type defaults to the global version.
    pub fn resources(
        self,
        type_url: TypeUrl,
        resources: impl IntoIterator<Item = BoxResource>,
    ) -> Self {
        let version = self.version.clone();
        self.resources_with_version(type_url, version, resources)
    }

    /// Add resources of a specific type with a custom version.
    pub fn resources_with_version(
        mut self,
        type_url: TypeUrl,
        version: impl Into<String>,
        resources: impl IntoIterator<Item = BoxResource>,
    ) -> Self {
        let mut snapshot_resources = SnapshotResources::new(version);
        for resource in resources {
            snapshot_resources
                .resources
                .insert(resource.name().to_string(), resource);
        }
        self.resources.insert(type_url, snapshot_resources);
        self
    }

    /// Add a single resource.
    pub fn resource(mut self, type_url: TypeUrl, resource: BoxResource) -> Self {
        let entry = self
            .resources
            .entry(type_url)
            .or_insert_with(|| SnapshotResources::new(self.version.clone()));
        entry
            .resources
            .insert(resource.name().to_string(), resource);
        self
    }

    /// Build the snapshot.
    pub fn build(self) -> Snapshot {
        Snapshot {
            version: self.version,
            resources: self.resources,
            created_at: std::time::Instant::now(),
        }
    }
}

/// Wrapper around `Arc<Snapshot>` for convenient sharing.
#[allow(dead_code)] // Public API surface
pub type SharedSnapshot = Arc<Snapshot>;

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

    /// A simple test resource for testing
    #[derive(Debug)]
    struct TestResource {
        name: String,
        data: Vec<u8>,
    }

    impl TestResource {
        fn new(name: impl Into<String>) -> Self {
            Self {
                name: name.into(),
                data: vec![],
            }
        }
    }

    impl Resource for TestResource {
        fn type_url(&self) -> &str {
            "test.type/TestResource"
        }

        fn name(&self) -> &str {
            &self.name
        }

        fn encode(&self) -> Result<prost_types::Any, Box<dyn std::error::Error + Send + Sync>> {
            Ok(prost_types::Any {
                type_url: self.type_url().to_string(),
                value: self.data.clone(),
            })
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn snapshot_builder_basic() {
        let snapshot = Snapshot::builder().version("v1").build();

        assert_eq!(snapshot.version(), "v1");
        assert!(snapshot.is_empty());
    }

    #[test]
    fn snapshot_builder_with_resources() {
        let snapshot = Snapshot::builder()
            .version("v2")
            .resources(TypeUrl::CLUSTER.into(), vec![])
            .build();

        assert_eq!(snapshot.version(), "v2");
        assert!(snapshot.contains_type(TypeUrl::CLUSTER.into()));
    }

    #[test]
    fn snapshot_resources_version() {
        let snapshot = Snapshot::builder()
            .version("global-v1")
            .resources(TypeUrl::CLUSTER.into(), vec![])
            .build();

        assert_eq!(
            snapshot.get_version(TypeUrl::CLUSTER.into()),
            Some("global-v1")
        );
    }

    #[test]
    fn snapshot_with_custom_version_per_type() {
        let snapshot = Snapshot::builder()
            .version("global-v1")
            .resources_with_version(TypeUrl::CLUSTER.into(), "cluster-v2", vec![])
            .resources_with_version(TypeUrl::LISTENER.into(), "listener-v3", vec![])
            .build();

        assert_eq!(
            snapshot.get_version(TypeUrl::CLUSTER.into()),
            Some("cluster-v2")
        );
        assert_eq!(
            snapshot.get_version(TypeUrl::LISTENER.into()),
            Some("listener-v3")
        );
    }

    #[test]
    fn snapshot_with_actual_resources() {
        let resource1: BoxResource = Arc::new(TestResource::new("resource-1"));
        let resource2: BoxResource = Arc::new(TestResource::new("resource-2"));

        let type_url = TypeUrl::new("test.type/TestResource");

        let snapshot = Snapshot::builder()
            .version("v1")
            .resources(type_url.clone(), vec![resource1, resource2])
            .build();

        assert_eq!(snapshot.total_resources(), 2);
        assert!(!snapshot.is_empty());

        let resources = snapshot.get_resources(type_url).unwrap();
        assert_eq!(resources.len(), 2);
    }

    #[test]
    fn snapshot_get_resource_by_name() {
        let resource: BoxResource = Arc::new(TestResource::new("my-resource"));
        let type_url = TypeUrl::new("test.type/TestResource");

        let snapshot = Snapshot::builder()
            .version("v1")
            .resources(type_url.clone(), vec![resource])
            .build();

        // Use get_resources to get the SnapshotResources, then get() to find by name
        let resources = snapshot.get_resources(type_url.clone()).unwrap();
        let found = resources.get("my-resource");
        assert!(found.is_some());
        assert_eq!(found.unwrap().name(), "my-resource");

        let not_found = resources.get("nonexistent");
        assert!(not_found.is_none());
    }

    #[test]
    fn snapshot_add_single_resource() {
        let resource: BoxResource = Arc::new(TestResource::new("single"));
        let type_url = TypeUrl::new("test.type/TestResource");

        let snapshot = Snapshot::builder()
            .version("v1")
            .resource(type_url.clone(), resource)
            .build();

        assert_eq!(snapshot.total_resources(), 1);
    }

    #[test]
    fn snapshot_multiple_types() {
        let cluster: BoxResource = Arc::new(TestResource::new("cluster-1"));
        let listener: BoxResource = Arc::new(TestResource::new("listener-1"));
        let route: BoxResource = Arc::new(TestResource::new("route-1"));

        let snapshot = Snapshot::builder()
            .version("v1")
            .resource(TypeUrl::CLUSTER.into(), cluster)
            .resource(TypeUrl::LISTENER.into(), listener)
            .resource(TypeUrl::ROUTE.into(), route)
            .build();

        assert_eq!(snapshot.total_resources(), 3);
        assert!(snapshot.contains_type(TypeUrl::CLUSTER.into()));
        assert!(snapshot.contains_type(TypeUrl::LISTENER.into()));
        assert!(snapshot.contains_type(TypeUrl::ROUTE.into()));
    }

    #[test]
    fn snapshot_type_urls() {
        let cluster: BoxResource = Arc::new(TestResource::new("cluster-1"));
        let listener: BoxResource = Arc::new(TestResource::new("listener-1"));

        let snapshot = Snapshot::builder()
            .version("v1")
            .resource(TypeUrl::CLUSTER.into(), cluster)
            .resource(TypeUrl::LISTENER.into(), listener)
            .build();

        let type_urls: Vec<_> = snapshot.type_urls().collect();
        assert_eq!(type_urls.len(), 2);
    }

    #[test]
    fn snapshot_created_at() {
        use std::time::Instant;

        let before = Instant::now();
        let snapshot = Snapshot::builder().version("v1").build();
        let after = Instant::now();

        assert!(snapshot.created_at() >= before);
        assert!(snapshot.created_at() <= after);
    }

    #[test]
    fn snapshot_age_via_created_at() {
        use std::time::Duration;

        let snapshot = Snapshot::builder().version("v1").build();
        std::thread::sleep(Duration::from_millis(10));

        // Calculate age from created_at
        let elapsed = snapshot.created_at().elapsed();
        assert!(elapsed.as_millis() >= 10);
    }

    #[test]
    fn snapshot_resource_iteration() {
        let resource: BoxResource = Arc::new(TestResource::new("my-resource"));
        let type_url = TypeUrl::new("test.type/TestResource");

        let snapshot = Snapshot::builder()
            .version("v1")
            .resources(type_url.clone(), vec![resource])
            .build();

        assert_eq!(snapshot.total_resources(), 1);
        let resources = snapshot.get_resources(type_url).unwrap();
        // Use the iter() method instead of indexing
        let items: Vec<_> = resources.iter().collect();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].1.name(), "my-resource");
    }

    #[test]
    fn snapshot_empty_version() {
        let snapshot = Snapshot::builder().version("").build();
        assert_eq!(snapshot.version(), "");
    }

    #[test]
    fn snapshot_is_empty_with_empty_resources() {
        // A snapshot with type but no actual resources
        let snapshot = Snapshot::builder()
            .version("v1")
            .resources(TypeUrl::CLUSTER.into(), vec![])
            .build();

        // is_empty should return true since there are no actual resources
        assert!(snapshot.is_empty());
    }
}