claviron-cache 0.2.6

Named cache stores, typed values, and fleet-shared caching for Claviron
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
use std::any::Any;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};

use arc_swap::ArcSwapOption;

use crate::config::CacheConfig;
use crate::drivers::memory::{self, MemoryCacheStore, MemoryLimit};
use crate::provider::CacheCatalog;
use crate::{CacheDriver, CacheProvider, CacheStore, Error};

/// Registration-time extension point for cache drivers and stores.
///
/// Drivers may be added by downstream application registration after
/// `claviron-cache` registers its built-ins; `cache.yaml` arrives afterwards,
/// prepared by Congif. During boot the registry
/// builds every configured store from the registered drivers, completes
/// cross-store links, publishes the final [`CacheProvider`] catalog, and
/// seals itself against runtime mutation.
pub struct CacheRegistry {
    config: ArcSwapOption<CacheConfig>,
    drivers: RwLock<HashMap<String, Arc<dyn CacheDriver>>>,
    stores: RwLock<HashMap<String, Arc<dyn CacheStore>>>,
    ready_stores: RwLock<HashMap<String, Arc<dyn CacheStore>>>,
    provider: Arc<CacheProvider>,
    sealed: AtomicBool,
}

impl CacheRegistry {
    pub(crate) fn new(provider: Arc<CacheProvider>) -> Self {
        Self {
            config: ArcSwapOption::empty(),
            drivers: RwLock::new(HashMap::new()),
            stores: RwLock::new(HashMap::new()),
            ready_stores: RwLock::new(HashMap::new()),
            provider,
            sealed: AtomicBool::new(false),
        }
    }

    /// The prepared `cache.yaml` the sealing boot builds from. Until sealed
    /// a later preparation replaces the earlier one; after it, the provider's
    /// reload path takes over.
    pub(crate) fn configure(
        &self,
        config: CacheConfig,
    ) -> crate::Result<()> {
        self.ensure_open()?;
        validate_config(&config)?;
        self.config.store(Some(Arc::new(config)));
        Ok(())
    }

    fn config(&self) -> crate::Result<Arc<CacheConfig>> {
        self.config.load_full().ok_or_else(|| {
            Error::Missing(
                "cache configuration has not been applied".to_string(),
            )
        })
    }

    /// Register a driver. Its configured stores are built when the registry
    /// seals, from the configuration prepared by then.
    pub fn register_driver(
        &self,
        driver: Arc<dyn CacheDriver>,
    ) -> crate::Result<&Self> {
        self.ensure_open()?;
        let name = normalize_driver_name(driver.driver_name())?;
        let mut drivers =
            self.drivers.write().expect("cache drivers lock poisoned");
        if drivers.contains_key(&name) {
            return Err(Error::Rejected(format!(
                "cache driver '{name}' is already registered"
            )));
        }
        drivers.insert(name, driver);
        Ok(self)
    }

    /// Register a ready store built from driver-owned configuration.
    pub fn register_store(
        &self,
        store: Arc<dyn CacheStore>,
    ) -> crate::Result<&Self> {
        self.ensure_open()?;
        let name = validate_store_name(store.store_name())?;
        let mut stores =
            self.stores.write().expect("cache stores lock poisoned");
        if stores.contains_key(&name) {
            return Err(Error::Rejected(format!(
                "cache store '{name}' is already registered"
            )));
        }
        stores.insert(name.clone(), store.clone());
        self.ready_stores
            .write()
            .expect("cache ready stores lock poisoned")
            .insert(name, store);
        Ok(self)
    }

    pub fn store(
        &self,
        name: &str,
    ) -> crate::Result<Arc<dyn CacheStore>> {
        if self.is_sealed() {
            return self.provider.raw_store(name);
        }
        self.stores
            .read()
            .expect("cache stores lock poisoned")
            .get(name)
            .cloned()
            .ok_or_else(|| {
                Error::Missing(format!(
                    "cache store '{name}' is not registered"
                ))
            })
    }

    pub fn is_sealed(&self) -> bool {
        self.sealed.load(Ordering::Acquire)
    }

    fn ensure_open(&self) -> crate::Result<()> {
        if self.is_sealed() {
            return Err(Error::Rejected(
                "cache registry is already sealed".to_string(),
            ));
        }
        Ok(())
    }

    /// Build every configured store from the registered drivers, finish
    /// their links and publish the catalog with its default store.
    pub(crate) fn seal(&self) -> crate::Result<()> {
        if self.is_sealed() {
            return Ok(());
        }
        let mut config = (*self.config()?).clone();

        let drivers =
            self.drivers.read().expect("cache drivers lock poisoned").clone();
        let mut stores =
            self.stores.read().expect("cache stores lock poisoned").clone();
        let mut configured = config.stores.iter().collect::<Vec<_>>();
        configured.sort_unstable_by_key(|(name, _)| *name);
        for (store_name, store_config) in configured {
            if stores.contains_key(store_name) {
                return Err(Error::Rejected(format!(
                    "cache store '{store_name}' is already registered"
                )));
            }
            let Some(driver) = drivers.get(&store_config.driver_key()) else {
                continue;
            };
            let store =
                driver.clone().create_store(store_name, store_config)?;
            validate_built_store(store_name, &store)?;
            stores.insert(store_name.clone(), store);
        }
        complete_catalog(&mut config, &mut stores)?;

        for (store_name, store_config) in &config.stores {
            let Some(driver) = drivers.get(&store_config.driver_key()) else {
                continue;
            };
            let store = stores
                .get(store_name)
                .expect("configured cache store was checked above");
            driver.finish_store(store, &stores)?;
        }

        *self.stores.write().expect("cache stores lock poisoned") =
            stores.clone();
        self.provider.install(CacheCatalog::new(config, stores))?;
        self.sealed.store(true, Ordering::Release);
        Ok(())
    }

    pub(crate) fn build_reload_catalog(
        &self,
        mut config: CacheConfig,
    ) -> crate::Result<ReloadCatalog> {
        if !self.is_sealed() {
            return Err(Error::Rejected(
                "cache registry is not sealed".to_string(),
            ));
        }
        validate_config(&config)?;

        let current = self.provider.catalog()?;
        #[cfg(feature = "orbit")]
        validate_orbit_reload(&current.config, &config)?;

        let drivers =
            self.drivers.read().expect("cache drivers lock poisoned").clone();
        let mut stores = self
            .ready_stores
            .read()
            .expect("cache ready stores lock poisoned")
            .clone();
        let mut unfinished = Vec::new();
        let mut memory_limits = Vec::new();

        // Keep the exact backing instances used by retained memory stores
        // visible through the new catalog as well.
        for (store_name, store_config) in &config.stores {
            if store_config.driver_key() == memory::NAME
                && let Some(store) = current_memory_store(&current, store_name)
                && let Some(l2_name) = store.l2_name()
            {
                if current.config.stores.get(l2_name)
                    != config.stores.get(l2_name)
                {
                    return Err(Error::Rejected(format!(
                        "cache store '{l2_name}' backs memory store '{store_name}' and cannot be reconfigured during reload; restart is required"
                    )));
                }
                if l2_name == memory::NAME
                    && !config.stores.contains_key(l2_name)
                    && !selects_implicit_memory(&config)
                    && !stores.contains_key(l2_name)
                {
                    return Err(Error::Rejected(format!(
                        "cannot remove the implicit memory store while it backs '{store_name}'"
                    )));
                }
                stores.insert(l2_name.to_owned(), current.store(l2_name)?);
            }
        }

        let mut configured = config.stores.iter().collect::<Vec<_>>();
        configured.sort_unstable_by_key(|(name, _)| *name);
        for (store_name, store_config) in configured {
            if stores.contains_key(store_name) {
                continue;
            }

            #[cfg(feature = "orbit")]
            if store_config.driver_key() == "orbit"
                && current.config.stores.get(store_name) == Some(store_config)
            {
                stores.insert(store_name.clone(), current.store(store_name)?);
                continue;
            }

            // A memory store's contents are process state, not configuration:
            // keep the store and stage its bound for commit.
            if store_config.driver_key() == memory::NAME
                && let Some(store) = current_memory_store(&current, store_name)
            {
                let limit = store.reload_limit(store_config)?;
                stores.insert(store_name.clone(), store.clone());
                memory_limits.push((store, limit));
                continue;
            }

            let driver_name = store_config.driver_key();
            let driver = drivers.get(&driver_name).cloned().ok_or_else(|| {
                Error::Missing(format!(
                    "cache store '{store_name}' uses unregistered driver '{}'",
                    store_config.driver
                ))
            })?;
            let store =
                driver.clone().create_reload_store(store_name, store_config)?;
            validate_built_store(store_name, &store)?;
            stores.insert(store_name.clone(), store.clone());
            unfinished.push((driver, store));
        }

        if selects_implicit_memory(&config)
            && !stores.contains_key(memory::NAME)
            && let Some(store) = current_memory_store(&current, memory::NAME)
        {
            if store.l2_name().is_some() {
                return Err(Error::Rejected(
                    "the default memory store cannot remove its L2 during reload; restart is required".into(),
                ));
            }
            stores.insert(memory::NAME.to_owned(), store.clone());
            memory_limits.push((store, MemoryLimit::DEFAULT));
        }

        complete_catalog(&mut config, &mut stores)?;
        for (driver, store) in unfinished {
            driver.finish_store(&store, &stores)?;
        }

        Ok(ReloadCatalog {
            catalog: CacheCatalog::new(config, stores),
            memory_limits,
        })
    }
}

/// A validated reload candidate.
///
/// Memory stores carried over from the current generation keep their
/// contents; their new bounds apply only once the whole candidate has been
/// built, so a rejected reload leaves every live store as it was.
pub(crate) struct ReloadCatalog {
    catalog: CacheCatalog,
    memory_limits: Vec<(Arc<MemoryCacheStore>, MemoryLimit)>,
}

impl ReloadCatalog {
    /// Apply the staged bounds and hand over the catalog to publish.
    pub(crate) fn commit(self) -> CacheCatalog {
        for (store, limit) in self.memory_limits {
            store.set_limit(limit);
        }
        self.catalog
    }
}

fn current_memory_store(
    current: &CacheCatalog,
    name: &str,
) -> Option<Arc<MemoryCacheStore>> {
    let store: Arc<dyn Any + Send + Sync> = current.store(name).ok()?;
    store.downcast::<MemoryCacheStore>().ok()
}

/// Whether the catalog will supply the built-in `memory` default itself.
fn selects_implicit_memory(config: &CacheConfig) -> bool {
    (config.default.trim().is_empty() || config.default == memory::NAME)
        && !config.stores.contains_key(memory::NAME)
}

/// Validate an explicit default name. An omitted name selects memory.
pub(crate) fn validate_config(config: &CacheConfig) -> crate::Result<()> {
    if !config.default.trim().is_empty() {
        validate_store_name(&config.default)?;
    }
    Ok(())
}

fn complete_catalog(
    config: &mut CacheConfig,
    stores: &mut HashMap<String, Arc<dyn CacheStore>>,
) -> crate::Result<()> {
    for (store_name, store_config) in &config.stores {
        if !stores.contains_key(store_name) {
            return Err(Error::Missing(format!(
                "cache store '{store_name}' uses unregistered driver '{}'",
                store_config.driver
            )));
        }
    }
    if config.default.trim().is_empty() {
        if stores
            .get(memory::NAME)
            .is_some_and(|store| store.driver_name() != memory::NAME)
        {
            return Err(Error::Rejected(
                "cache store 'memory' uses another driver; choose a default explicitly".into(),
            ));
        }
        config.default = memory::NAME.to_owned();
    }
    if config.default == memory::NAME {
        stores.entry(config.default.clone()).or_insert_with(|| {
            Arc::new(MemoryCacheStore::new(memory::NAME, MemoryLimit::DEFAULT))
        });
    }
    if !stores.contains_key(&config.default) {
        return Err(Error::Missing(format!(
            "cache default store '{}' is not registered",
            config.default
        )));
    }
    Ok(())
}

fn validate_built_store(
    expected_name: &str,
    store: &Arc<dyn CacheStore>,
) -> crate::Result<()> {
    let actual_name = validate_store_name(store.store_name())?;
    if actual_name != expected_name {
        return Err(Error::Rejected(format!(
            "cache driver built store '{actual_name}' for configured name '{expected_name}'"
        )));
    }
    Ok(())
}

#[cfg(feature = "orbit")]
fn validate_orbit_reload(
    current: &CacheConfig,
    candidate: &CacheConfig,
) -> crate::Result<()> {
    for (name, old) in current
        .stores
        .iter()
        .filter(|(_, config)| config.driver_key() == "orbit")
    {
        let Some(new) = candidate.stores.get(name) else {
            continue;
        };
        if old != new {
            return Err(Error::Rejected(format!(
                "Orbit cache store '{name}' cannot be reconfigured during reload; disable it or restart"
            )));
        }

        {
            let orbit: crate::OrbitCacheStoreConfig =
                old.deserialize_options()?;
            if let Some(l2_name) = orbit.l2
                && current.stores.get(&l2_name)
                    != candidate.stores.get(&l2_name)
            {
                return Err(Error::Rejected(format!(
                    "cache store '{l2_name}' backs Orbit store '{name}' and cannot be reconfigured during reload; restart is required"
                )));
            }
        }
    }
    Ok(())
}

fn normalize_driver_name(name: &str) -> crate::Result<String> {
    let name = name.trim().to_ascii_lowercase();
    if name.is_empty() {
        return Err(Error::Rejected(
            "cache driver name must not be empty".to_string(),
        ));
    }
    Ok(name)
}

fn validate_store_name(name: &str) -> crate::Result<String> {
    if name.is_empty() || name.trim() != name {
        return Err(Error::Rejected(
            "cache store name must not be empty or contain surrounding whitespace".to_string()
        ));
    }
    Ok(name.to_string())
}

#[cfg(test)]
#[path = "../tests/registry.rs"]
mod tests;