rpytest-daemon 0.1.1

Pure Rust daemon for rpytest - handles test execution, collection, and state management
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
//! Storage layer using sled for persistent data.

use crate::error::{DaemonError, Result};
use crate::models::{
    FixtureState, FlakinessRecord, NativeTestNode, ScheduledTest, TestNode,
};
use rmp_serde::{Deserializer, Serializer};
use serde::{Deserialize, Serialize};
use sled::{Db, Tree};
use std::path::PathBuf;
use tracing::{debug, error};

/// Storage tree names.
const TREE_INVENTORY: &str = "inventory";
const TREE_NATIVE_TESTS: &str = "native_tests";
const TREE_FLAKINESS: &str = "flakiness";
const TREE_FIXTURES: &str = "fixtures";
const TREE_DURATION_HISTORY: &str = "duration_history";
const TREE_CONTEXTS: &str = "contexts";
const TREE_SCHEDULER: &str = "scheduler";
const TREE_CONFIG: &str = "config";

/// Schema version for storage format compatibility.
const STORAGE_VERSION: u32 = 1;

/// Main storage wrapper for the daemon.
#[derive(Clone)]
pub struct DaemonStorage {
    db: Db,
    inventory: Tree,
    native_tests: Tree,
    flakiness: Tree,
    fixtures: Tree,
    duration_history: Tree,
    contexts: Tree,
    scheduler: Tree,
    config: Tree,
}

impl std::fmt::Debug for DaemonStorage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DaemonStorage").finish_non_exhaustive()
    }
}

impl DaemonStorage {
    /// Open or create the storage database.
    pub fn open(storage_path: &PathBuf) -> Result<Self> {
        // Configure sled for high performance:
        // - cache_capacity: 256MB for hot data caching
        // - flush_every_ms: batch flushes every 5s instead of immediate
        // - mode: HighThroughput for write-heavy workloads
        let config = sled::Config::default()
            .path(storage_path)
            .temporary(false)
            .cache_capacity(256 * 1024 * 1024) // 256MB cache
            .flush_every_ms(Some(5000))         // Batch flushes every 5s
            .mode(sled::Mode::HighThroughput);

        let db = config.open()?;

        // Open or create trees
        let inventory = db.open_tree(TREE_INVENTORY)?;
        let native_tests = db.open_tree(TREE_NATIVE_TESTS)?;
        let flakiness = db.open_tree(TREE_FLAKINESS)?;
        let fixtures = db.open_tree(TREE_FIXTURES)?;
        let duration_history = db.open_tree(TREE_DURATION_HISTORY)?;
        let contexts = db.open_tree(TREE_CONTEXTS)?;
        let scheduler = db.open_tree(TREE_SCHEDULER)?;
        let config_tree = db.open_tree(TREE_CONFIG)?;

        // Validate schema version
        if let Some(version_bytes) = config_tree.get("version")? {
            let version: u32 = rmp_serde::from_slice(&version_bytes)?;
            if version != STORAGE_VERSION {
                error!(
                    "Storage schema version mismatch: expected {}, found {}",
                    STORAGE_VERSION, version
                );
                return Err(DaemonError::Other(format!(
                    "Storage schema version mismatch: expected {}, found {}",
                    STORAGE_VERSION, version
                )));
            }
        } else {
            // First run - set version
            let mut version_bytes = Vec::new();
            STORAGE_VERSION.serialize(&mut Serializer::new(&mut version_bytes))?;
            config_tree.insert("version", version_bytes)?;
        }

        Ok(Self {
            db,
            inventory,
            native_tests,
            flakiness,
            fixtures,
            duration_history,
            contexts,
            scheduler,
            config: config_tree,
        })
    }

    /// Clear all data (for testing or reset).
    pub fn clear_all(&self) -> Result<()> {
        self.inventory.clear()?;
        self.native_tests.clear()?;
        self.flakiness.clear()?;
        self.fixtures.clear()?;
        self.duration_history.clear()?;
        self.contexts.clear()?;
        self.scheduler.clear()?;
        Ok(())
    }

    // ==================== Inventory ====================

    /// Save a test node to inventory.
    pub fn save_test_node(&self, node: &TestNode) -> Result<()> {
        let mut buf = Vec::new();
        node.serialize(&mut Serializer::new(&mut buf))?;
        self.inventory.insert(&node.node_id, buf)?;
        Ok(())
    }

    /// Save multiple test nodes to inventory in a batch (much faster than individual saves).
    pub fn save_test_nodes_batch(&self, nodes: &[TestNode]) -> Result<()> {
        if nodes.is_empty() {
            return Ok(());
        }

        let mut batch = sled::Batch::default();
        for node in nodes {
            let mut buf = Vec::new();
            node.serialize(&mut Serializer::new(&mut buf))?;
            batch.insert(node.node_id.as_bytes(), buf);
        }
        self.inventory.apply_batch(batch)?;
        Ok(())
    }

    /// Load a test node from inventory.
    pub fn load_test_node(&self, node_id: &str) -> Result<Option<TestNode>> {
        if let Some(bytes) = self.inventory.get(node_id)? {
            let mut deserializer = Deserializer::new(&bytes[..]);
            let node: TestNode = Deserialize::deserialize(&mut deserializer)?;
            Ok(Some(node))
        } else {
            Ok(None)
        }
    }

    /// Get all test nodes in inventory.
    pub fn get_all_inventory(&self) -> Result<Vec<TestNode>> {
        let mut nodes = Vec::new();
        for item in self.inventory.iter() {
            let (_, bytes) = item?;
            let mut deserializer = Deserializer::new(&bytes[..]);
            let node: TestNode = Deserialize::deserialize(&mut deserializer)?;
            nodes.push(node);
        }
        Ok(nodes)
    }

    /// Get inventory count.
    pub fn inventory_count(&self) -> usize {
        self.inventory.len()
    }

    /// Clear inventory.
    pub fn clear_inventory(&self) -> Result<()> {
        self.inventory.clear()?;
        Ok(())
    }

    // ==================== Native Tests ====================

    /// Save a native test node.
    pub fn save_native_test(&self, node: &NativeTestNode) -> Result<()> {
        let mut buf = Vec::new();
        node.serialize(&mut Serializer::new(&mut buf))?;
        self.native_tests.insert(&node.node_id, buf)?;
        Ok(())
    }

    /// Save multiple native test nodes in a batch (much faster than individual saves).
    pub fn save_native_tests_batch(&self, nodes: &[NativeTestNode]) -> Result<()> {
        if nodes.is_empty() {
            return Ok(());
        }

        let mut batch = sled::Batch::default();
        for node in nodes {
            let mut buf = Vec::new();
            node.serialize(&mut Serializer::new(&mut buf))?;
            batch.insert(node.node_id.as_bytes(), buf);
        }
        self.native_tests.apply_batch(batch)?;
        Ok(())
    }

    /// Load a native test node.
    pub fn load_native_test(&self, node_id: &str) -> Result<Option<NativeTestNode>> {
        if let Some(bytes) = self.native_tests.get(node_id)? {
            let mut deserializer = Deserializer::new(&bytes[..]);
            let node: NativeTestNode = Deserialize::deserialize(&mut deserializer)?;
            Ok(Some(node))
        } else {
            Ok(None)
        }
    }

    /// Get all native tests.
    pub fn get_all_native_tests(&self) -> Result<Vec<NativeTestNode>> {
        let mut nodes = Vec::new();
        for item in self.native_tests.iter() {
            let (_, bytes) = item?;
            let mut deserializer = Deserializer::new(&bytes[..]);
            let node: NativeTestNode = Deserialize::deserialize(&mut deserializer)?;
            nodes.push(node);
        }
        Ok(nodes)
    }

    /// Clear native tests.
    pub fn clear_native_tests(&self) -> Result<()> {
        self.native_tests.clear()?;
        Ok(())
    }

    // ==================== Flakiness ====================

    /// Save flakiness record.
    pub fn save_flakiness_record(&self, record: &FlakinessRecord) -> Result<()> {
        let mut buf = Vec::new();
        record.serialize(&mut Serializer::new(&mut buf))?;
        self.flakiness.insert(&record.node_id, buf)?;
        Ok(())
    }

    /// Load flakiness record.
    pub fn load_flakiness_record(&self, node_id: &str) -> Result<Option<FlakinessRecord>> {
        if let Some(bytes) = self.flakiness.get(node_id)? {
            let mut deserializer = Deserializer::new(&bytes[..]);
            let record: FlakinessRecord = Deserialize::deserialize(&mut deserializer)?;
            Ok(Some(record))
        } else {
            Ok(None)
        }
    }

    /// Get all flakiness records.
    pub fn get_all_flakiness(&self) -> Result<Vec<FlakinessRecord>> {
        let mut records = Vec::new();
        for item in self.flakiness.iter() {
            let (_, bytes) = item?;
            let mut deserializer = Deserializer::new(&bytes[..]);
            let record: FlakinessRecord = Deserialize::deserialize(&mut deserializer)?;
            records.push(record);
        }
        Ok(records)
    }

    /// Delete flakiness record.
    pub fn delete_flakiness_record(&self, node_id: &str) -> Result<()> {
        self.flakiness.remove(node_id)?;
        Ok(())
    }

    // ==================== Fixtures ====================

    /// Save fixture state.
    pub fn save_fixture(&self, context_id: &str, fixture: &FixtureState) -> Result<()> {
        let key = format!("{}/{}", context_id, fixture.name);
        let mut buf = Vec::new();
        fixture.serialize(&mut Serializer::new(&mut buf))?;
        self.fixtures.insert(key, buf)?;
        Ok(())
    }

    /// Load fixture state.
    pub fn load_fixture(&self, context_id: &str, name: &str) -> Result<Option<FixtureState>> {
        let key = format!("{}/{}", context_id, name);
        if let Some(bytes) = self.fixtures.get(key)? {
            let mut deserializer = Deserializer::new(&bytes[..]);
            let fixture: FixtureState = Deserialize::deserialize(&mut deserializer)?;
            Ok(Some(fixture))
        } else {
            Ok(None)
        }
    }

    /// Get all fixtures for a context.
    pub fn get_context_fixtures(&self, context_id: &str) -> Result<Vec<FixtureState>> {
        let prefix = format!("{}/", context_id);
        let mut fixtures = Vec::new();
        for item in self.fixtures.scan_prefix(prefix) {
            let (_, bytes) = item?;
            let mut deserializer = Deserializer::new(&bytes[..]);
            let fixture: FixtureState = Deserialize::deserialize(&mut deserializer)?;
            fixtures.push(fixture);
        }
        Ok(fixtures)
    }

    /// Delete fixture.
    pub fn delete_fixture(&self, context_id: &str, name: &str) -> Result<()> {
        let key = format!("{}/{}", context_id, name);
        self.fixtures.remove(key)?;
        Ok(())
    }

    /// Clear all fixtures for a context.
    pub fn clear_context_fixtures(&self, context_id: &str) -> Result<()> {
        let prefix = format!("{}/", context_id);
        for item in self.fixtures.scan_prefix(prefix) {
            let (key, _) = item?;
            self.fixtures.remove(key)?;
        }
        Ok(())
    }

    // ==================== Duration History ====================

    /// Save duration history for a test.
    pub fn save_duration_history(&self, node_id: &str, durations: &[u64]) -> Result<()> {
        let mut buf = Vec::new();
        durations.serialize(&mut Serializer::new(&mut buf))?;
        self.duration_history.insert(node_id, buf)?;
        Ok(())
    }

    /// Load duration history for a test.
    pub fn load_duration_history(&self, node_id: &str) -> Result<Option<Vec<u64>>> {
        if let Some(bytes) = self.duration_history.get(node_id)? {
            let mut deserializer = Deserializer::new(&bytes[..]);
            let durations: Vec<u64> = Deserialize::deserialize(&mut deserializer)?;
            Ok(Some(durations))
        } else {
            Ok(None)
        }
    }

    /// Get average duration for a test.
    pub fn get_average_duration(&self, node_id: &str) -> Result<Option<u64>> {
        if let Some(durations) = self.load_duration_history(node_id)? {
            if durations.is_empty() {
                return Ok(None);
            }
            let sum: u64 = durations.iter().sum();
            Ok(Some(sum / durations.len() as u64))
        } else {
            Ok(None)
        }
    }

    /// Save duration history for multiple tests in a batch (much faster than individual saves).
    pub fn save_duration_history_batch(&self, histories: &[(&str, &[u64])]) -> Result<()> {
        if histories.is_empty() {
            return Ok(());
        }

        let mut batch = sled::Batch::default();
        for (node_id, durations) in histories {
            let mut buf = Vec::new();
            durations.serialize(&mut Serializer::new(&mut buf))?;
            batch.insert(node_id.as_bytes(), buf);
        }
        self.duration_history.apply_batch(batch)?;
        Ok(())
    }

    // ==================== Scheduler ====================

    /// Save scheduled test.
    pub fn save_scheduled_test(&self, test: &ScheduledTest) -> Result<()> {
        let mut buf = Vec::new();
        test.serialize(&mut Serializer::new(&mut buf))?;
        self.scheduler.insert(&test.node_id, buf)?;
        Ok(())
    }

    /// Load all scheduled tests.
    pub fn get_all_scheduled_tests(&self) -> Result<Vec<ScheduledTest>> {
        let mut tests = Vec::new();
        for item in self.scheduler.iter() {
            let (_, bytes) = item?;
            let mut deserializer = Deserializer::new(&bytes[..]);
            let test: ScheduledTest = Deserialize::deserialize(&mut deserializer)?;
            tests.push(test);
        }
        Ok(tests)
    }

    /// Clear scheduler.
    pub fn clear_scheduler(&self) -> Result<()> {
        self.scheduler.clear()?;
        Ok(())
    }

    // ==================== Contexts ====================

    /// Save context metadata (simple string map).
    pub fn save_context(&self, context_id: &str, data: &serde_json::Value) -> Result<()> {
        let bytes = serde_json::to_vec(data)?;
        self.contexts.insert(context_id, bytes)?;
        Ok(())
    }

    /// Load context metadata.
    pub fn load_context(&self, context_id: &str) -> Result<Option<serde_json::Value>> {
        if let Some(bytes) = self.contexts.get(context_id)? {
            let data: serde_json::Value = serde_json::from_slice(&bytes)?;
            Ok(Some(data))
        } else {
            Ok(None)
        }
    }

    /// Get all context IDs.
    pub fn get_all_context_ids(&self) -> Vec<String> {
        self.contexts
            .iter()
            .filter_map(|item| item.ok())
            .map(|(key, _)| String::from_utf8_lossy(&key).to_string())
            .collect()
    }

    /// Delete context.
    pub fn delete_context(&self, context_id: &str) -> Result<()> {
        self.contexts.remove(context_id)?;
        Ok(())
    }

    // ==================== Config ====================

    /// Save config value.
    pub fn save_config(&self, key: &str, value: &serde_json::Value) -> Result<()> {
        let bytes = serde_json::to_vec(value)?;
        self.config.insert(key, bytes)?;
        Ok(())
    }

    /// Load config value.
    pub fn load_config(&self, key: &str) -> Result<Option<serde_json::Value>> {
        if let Some(bytes) = self.config.get(key)? {
            let value: serde_json::Value = serde_json::from_slice(&bytes)?;
            Ok(Some(value))
        } else {
            Ok(None)
        }
    }

    /// Flush to disk.
    pub fn flush(&self) -> Result<()> {
        self.db.flush()?;
        Ok(())
    }

    // ==================== Maintenance ====================

    /// Evict old data that hasn't been accessed in `max_age_days`.
    /// Returns the number of records removed.
    pub fn evict_old_data(&self, _max_age_days: u32) -> Result<usize> {
        // Note: max_age_days reserved for future timestamp-based eviction
        let mut removed = 0;

        // Evict old flakiness records with few runs
        let mut keys_to_remove = Vec::new();
        for item in self.flakiness.iter() {
            if let Ok((key, value)) = item {
                let mut deserializer = Deserializer::new(&value[..]);
                if let Ok(record) = FlakinessRecord::deserialize(&mut deserializer) {
                    // Records with few runs are candidates for eviction
                    if record.total_runs < 3 {
                        keys_to_remove.push(key);
                    }
                }
            }
        }

        for key in keys_to_remove {
            self.flakiness.remove(key)?;
            removed += 1;
        }

        // Trigger compaction after bulk delete
        if removed > 100 {
            debug!("Evicted {} records, triggering compaction", removed);
            self.compact_async();
        }

        Ok(removed)
    }

    /// Trigger background compaction to reclaim disk space.
    pub fn compact_async(&self) {
        let db = self.db.clone();
        std::thread::spawn(move || {
            if let Err(e) = db.flush() {
                error!("Background compaction failed: {}", e);
            }
        });
    }

    /// Get the internal database for advanced operations.
    pub fn db(&self) -> &Db {
        &self.db
    }
}