tensor_blob 0.4.0

Content-addressable blob storage with streaming and garbage collection
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
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::{
    collections::HashSet,
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};

use tensor_store::TensorStore;
use tokio::{sync::broadcast, task::JoinHandle, time::interval};

use crate::{
    config::GcConfig,
    error::Result,
    metadata::GcStats,
    streaming::{get_int, get_pointers},
};

/// Background garbage collector for orphaned chunks.
pub struct GarbageCollector {
    store: TensorStore,
    config: GcConfig,
    shutdown_tx: broadcast::Sender<()>,
}

impl GarbageCollector {
    #[must_use]
    pub fn new(store: TensorStore, config: GcConfig) -> Self {
        let (shutdown_tx, _) = broadcast::channel(1);
        Self {
            store,
            config,
            shutdown_tx,
        }
    }

    /// Start background GC task. Returns a handle to the task.
    #[must_use]
    pub fn start(self: Arc<Self>) -> JoinHandle<()> {
        let gc = Arc::clone(&self);
        tokio::spawn(async move {
            gc.run().await;
        })
    }

    /// Get a shutdown sender for graceful termination.
    #[must_use]
    pub fn shutdown_sender(&self) -> broadcast::Sender<()> {
        self.shutdown_tx.clone()
    }

    /// Send shutdown signal.
    pub fn shutdown(&self) {
        // Receiver may already be dropped during shutdown
        self.shutdown_tx.send(()).ok();
    }

    async fn run(&self) {
        let mut interval = interval(self.config.check_interval);
        let mut shutdown_rx = self.shutdown_tx.subscribe();

        loop {
            tokio::select! {
                _ = interval.tick() => {
                    let _ = self.gc_cycle().await;
                }
                _ = shutdown_rx.recv() => {
                    break;
                }
            }
        }
    }

    /// Run a single GC cycle, processing up to `batch_size` chunks.
    #[allow(clippy::unused_async)]
    pub async fn gc_cycle(&self) -> GcStats {
        let mut deleted = 0;
        let mut freed_bytes = 0;

        let now = current_timestamp();
        let min_created = now.saturating_sub(self.config.min_age.as_secs());

        // Find chunks with zero refs
        let chunk_keys = self.store.scan("_blob:chunk:");

        for chunk_key in chunk_keys.into_iter().take(self.config.batch_size) {
            if let Ok(tensor) = self.store.get(&chunk_key) {
                let refs = get_int(&tensor, "_refs").unwrap_or(0);
                let created =
                    u64::try_from(get_int(&tensor, "_created").unwrap_or(0).max(0)).unwrap_or(0);

                // Zero refs and old enough
                if refs == 0 && created < min_created {
                    let size =
                        usize::try_from(get_int(&tensor, "_size").unwrap_or(0).max(0)).unwrap_or(0);

                    if self.store.delete(&chunk_key).is_ok() {
                        deleted += 1;
                        freed_bytes += size;
                    }
                }
            }
        }

        GcStats {
            deleted,
            freed_bytes,
        }
    }

    /// Full GC: recount all references from scratch.
    ///
    /// # Errors
    ///
    /// Returns an error if chunk deletion fails.
    #[allow(clippy::unused_async)]
    pub async fn full_gc(&self) -> Result<GcStats> {
        // 1. Build reference set from all artifacts
        let mut referenced: HashSet<String> = HashSet::new();

        for meta_key in self.store.scan("_blob:meta:") {
            if let Ok(tensor) = self.store.get(&meta_key) {
                if let Some(chunks) = get_pointers(&tensor, "_chunks") {
                    referenced.extend(chunks);
                }
            }
        }

        // 2. Delete unreferenced chunks
        let mut deleted = 0;
        let mut freed_bytes = 0;

        for chunk_key in self.store.scan("_blob:chunk:") {
            if !referenced.contains(&chunk_key) {
                if let Ok(tensor) = self.store.get(&chunk_key) {
                    let size =
                        usize::try_from(get_int(&tensor, "_size").unwrap_or(0).max(0)).unwrap_or(0);

                    if self.store.delete(&chunk_key).is_ok() {
                        deleted += 1;
                        freed_bytes += size;
                    }
                }
            }
        }

        Ok(GcStats {
            deleted,
            freed_bytes,
        })
    }

    /// Count orphaned chunks (chunks with zero references).
    #[must_use]
    pub fn count_orphans(&self) -> usize {
        let mut count = 0;

        for chunk_key in self.store.scan("_blob:chunk:") {
            if let Ok(tensor) = self.store.get(&chunk_key) {
                let refs = get_int(&tensor, "_refs").unwrap_or(0);
                if refs == 0 {
                    count += 1;
                }
            }
        }

        count
    }
}

/// Decrement chunk reference count. Used when deleting artifacts.
///
/// # Errors
///
/// Returns an error if the store operation fails.
pub fn decrement_chunk_refs(store: &TensorStore, chunk_key: &str) -> Result<()> {
    if let Ok(mut tensor) = store.get(chunk_key) {
        let refs = get_int(&tensor, "_refs").unwrap_or(1);
        let new_refs = (refs - 1).max(0);
        tensor.set(
            "_refs",
            tensor_store::TensorValue::Scalar(tensor_store::ScalarValue::Int(new_refs)),
        );
        store.put(chunk_key, tensor)?;
    }
    Ok(())
}

/// Increment chunk reference count. Used for deduplication.
///
/// # Errors
///
/// Returns an error if the store operation fails.
pub fn increment_chunk_refs(store: &TensorStore, chunk_key: &str) -> Result<()> {
    if let Ok(mut tensor) = store.get(chunk_key) {
        let refs = get_int(&tensor, "_refs").unwrap_or(0);
        tensor.set(
            "_refs",
            tensor_store::TensorValue::Scalar(tensor_store::ScalarValue::Int(refs + 1)),
        );
        store.put(chunk_key, tensor)?;
    }
    Ok(())
}

fn current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use tensor_store::{ScalarValue, TensorData, TensorValue};

    use super::*;
    use crate::chunker::Chunk;

    fn create_test_store() -> TensorStore {
        TensorStore::new()
    }

    fn store_chunk(store: &TensorStore, data: &[u8], refs: i64) -> String {
        let chunk = Chunk::new(data.to_vec());
        let chunk_key = chunk.key();

        let mut tensor = TensorData::new();
        tensor.set(
            "_type",
            TensorValue::Scalar(ScalarValue::String("blob_chunk".to_string())),
        );
        tensor.set(
            "_data",
            TensorValue::Scalar(ScalarValue::Bytes(data.to_vec())),
        );
        tensor.set(
            "_size",
            TensorValue::Scalar(ScalarValue::Int(data.len() as i64)),
        );
        tensor.set("_refs", TensorValue::Scalar(ScalarValue::Int(refs)));
        tensor.set("_created", TensorValue::Scalar(ScalarValue::Int(0))); // Old timestamp

        store.put(&chunk_key, tensor).unwrap();
        chunk_key
    }

    fn store_artifact(store: &TensorStore, id: &str, chunks: Vec<String>) {
        let mut tensor = TensorData::new();
        tensor.set(
            "_type",
            TensorValue::Scalar(ScalarValue::String("blob_artifact".to_string())),
        );
        tensor.set(
            "_id",
            TensorValue::Scalar(ScalarValue::String(id.to_string())),
        );
        tensor.set("_chunks", TensorValue::Pointers(chunks));

        let meta_key = format!("_blob:meta:{id}");
        store.put(&meta_key, tensor).unwrap();
    }

    #[tokio::test]
    async fn test_gc_cycle_deletes_orphans() {
        let store = create_test_store();

        // Create a chunk with 0 refs (orphan)
        let orphan_key = store_chunk(&store, b"orphan data", 0);

        // Create a chunk with refs (should be kept)
        let kept_key = store_chunk(&store, b"kept data", 1);

        // Run GC
        let config = GcConfig {
            check_interval: Duration::from_secs(1),
            batch_size: 100,
            min_age: Duration::from_secs(0), // No age requirement for test
        };
        let gc = GarbageCollector::new(store.clone(), config);
        let stats = gc.gc_cycle().await;

        assert_eq!(stats.deleted, 1);
        assert!(!store.exists(&orphan_key));
        assert!(store.exists(&kept_key));
    }

    #[tokio::test]
    async fn test_gc_respects_batch_size() {
        let store = create_test_store();

        // Create 5 orphan chunks
        for i in 0..5 {
            store_chunk(&store, &[i as u8; 10], 0);
        }

        // Run GC with batch size of 2
        let config = GcConfig {
            check_interval: Duration::from_secs(1),
            batch_size: 2,
            min_age: Duration::from_secs(0),
        };
        let gc = GarbageCollector::new(store.clone(), config);
        let stats = gc.gc_cycle().await;

        // Should only delete up to 2 chunks per cycle
        assert!(stats.deleted <= 2);
    }

    #[tokio::test]
    async fn test_full_gc() {
        let store = create_test_store();

        // Create chunks
        let chunk1 = store_chunk(&store, b"chunk 1", 1);
        let chunk2 = store_chunk(&store, b"chunk 2", 1);
        let _orphan = store_chunk(&store, b"orphan", 1);

        // Create artifact referencing only chunk1 and chunk2
        store_artifact(&store, "artifact1", vec![chunk1.clone(), chunk2.clone()]);

        // Run full GC
        let config = GcConfig::default();
        let gc = GarbageCollector::new(store.clone(), config);
        let stats = gc.full_gc().await.unwrap();

        // Orphan should be deleted
        assert_eq!(stats.deleted, 1);
        assert!(store.exists(&chunk1));
        assert!(store.exists(&chunk2));
    }

    #[tokio::test]
    async fn test_count_orphans() {
        let store = create_test_store();

        store_chunk(&store, b"orphan 1", 0);
        store_chunk(&store, b"orphan 2", 0);
        store_chunk(&store, b"referenced", 1);

        let config = GcConfig::default();
        let gc = GarbageCollector::new(store, config);

        assert_eq!(gc.count_orphans(), 2);
    }

    #[test]
    fn test_decrement_chunk_refs() {
        let store = create_test_store();
        let chunk_key = store_chunk(&store, b"data", 3);

        decrement_chunk_refs(&store, &chunk_key).unwrap();

        let tensor = store.get(&chunk_key).unwrap();
        assert_eq!(get_int(&tensor, "_refs"), Some(2));
    }

    #[test]
    fn test_decrement_chunk_refs_saturating() {
        let store = create_test_store();
        let chunk_key = store_chunk(&store, b"data", 0);

        decrement_chunk_refs(&store, &chunk_key).unwrap();

        let tensor = store.get(&chunk_key).unwrap();
        assert_eq!(get_int(&tensor, "_refs"), Some(0)); // Doesn't go negative
    }

    #[test]
    fn test_increment_chunk_refs() {
        let store = create_test_store();
        let chunk_key = store_chunk(&store, b"data", 1);

        increment_chunk_refs(&store, &chunk_key).unwrap();

        let tensor = store.get(&chunk_key).unwrap();
        assert_eq!(get_int(&tensor, "_refs"), Some(2));
    }

    #[tokio::test]
    async fn test_gc_shutdown() {
        let store = create_test_store();
        let config = GcConfig {
            check_interval: Duration::from_millis(10),
            batch_size: 100,
            min_age: Duration::from_secs(0),
        };
        let gc = Arc::new(GarbageCollector::new(store, config));

        let handle = gc.clone().start();

        // Let it run for a bit
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Shutdown
        gc.shutdown();

        // Task should complete
        let result = tokio::time::timeout(Duration::from_secs(1), handle).await;
        assert!(result.is_ok());
    }
}