itzma-core 0.2.1

Vector search on object storage: an embedded library, no server, just your bucket
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! The decoded-structure cache.
//!
//! [`CacheStore`](crate::CacheStore) keeps bytes, so a warm query still parses
//! every head, partition, block and directory it touches. This keeps what the
//! parse produced, keyed by the object's path and the structure, so a warm
//! query reads nothing from the store and decodes nothing. It needs no
//! invalidation: an object's bytes never change while it exists, and index
//! generations are never reused.
//!
//! Loads are single-flight. The first caller of a missing key marks it pending
//! and runs its own load. Callers that find it pending wait, then look again:
//! a value is a hit, and a load that failed or was dropped leaves nothing
//! behind, so the next caller loads it itself with its own read budget. No
//! error is ever shared between callers, so [`ItzmaError`] needs no `Clone`.
//!
//! One LRU bounded by weight in bytes, not shards: a decoded `Layer` of a whole
//! segment can be larger than any fixed share of the limit would be.

use std::any::Any;
use std::collections::HashMap;
use std::future::Future;
use std::sync::{Arc, Mutex, PoisonError, Weak};

use futures::channel::oneshot;
use futures::future::{FutureExt, Shared};
use lru::LruCache;
use object_store::ObjectStore;
use object_store::path::Path;

use crate::error::{ItzmaError, Result};

/// The limit a [`DecodedCache`] has when the caller does not say: 256 MiB.
pub const DEFAULT_DECODED_MAX_BYTES: u64 = 256 * 1024 * 1024;

/// How a [`DecodedCache`] is sized.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct DecodedCacheOptions {
    /// The most bytes of decoded structures kept, by each structure's own
    /// estimate of its size. A structure bigger than this is used and not
    /// kept. Default [`DEFAULT_DECODED_MAX_BYTES`].
    pub max_bytes: u64,
}

impl Default for DecodedCacheOptions {
    fn default() -> Self {
        Self {
            max_bytes: DEFAULT_DECODED_MAX_BYTES,
        }
    }
}

impl DecodedCacheOptions {
    /// Set [`max_bytes`](Self::max_bytes).
    #[must_use]
    pub fn max_bytes(mut self, max_bytes: u64) -> Self {
        self.max_bytes = max_bytes;
        self
    }
}

/// Which structure of an object a cache entry holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum What {
    /// A segment's header, trailer and directory (`RangedSegment::open`).
    SegmentMeta,
    /// A segment's Bloom filter.
    Bloom,
    /// One block of a segment's ids, attributes or tombstones.
    IdBlock(usize),
    AttrBlock(usize),
    TombstoneBlock(usize),
    /// An index object's prefix and head bytes (`index::open`), any kind.
    IndexHead,
    /// An SQ8 head, parsed.
    Sq8Head,
    /// One SQ8 partition, parsed.
    Partition(u32),
    /// A sparse index's term directory.
    SparseDirectory,
    /// A whole segment for the query's flat path, as a `SegmentView`.
    View,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Key {
    store: usize,
    path: Path,
    what: What,
}

type Value = Arc<dyn Any + Send + Sync>;
type Pending = Shared<oneshot::Receiver<()>>;

struct Inner {
    ready: LruCache<Key, (Value, u64)>,
    pending: HashMap<Key, Pending>,
    used: u64,
    /// The stores whose handles have used this cache, by address, so a store
    /// dropped and another allocated at the same address cannot see its
    /// entries.
    stores: HashMap<usize, Weak<dyn ObjectStore>>,
}

/// Parsed index heads, partitions, segment directories, blocks and decoded
/// segments, shared by every handle given it with
/// [`Namespace::with_decoded_cache`](crate::Namespace::with_decoded_cache).
///
/// A hit costs no store call and is not charged to the read budget; a miss is
/// read and charged as it would be without the cache. Only the query path uses
/// it: `index`, `compact`, `gc`, `inspect` and `recover` never do. Entries are
/// keyed by store as well as path, so one cache can serve handles over several
/// stores.
pub struct DecodedCache {
    max_bytes: u64,
    inner: Mutex<Inner>,
}

impl std::fmt::Debug for DecodedCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let inner = self.lock();
        f.debug_struct("DecodedCache")
            .field("max_bytes", &self.max_bytes)
            .field("used", &inner.used)
            .field("entries", &inner.ready.len())
            .finish()
    }
}

impl DecodedCache {
    /// An empty cache.
    #[must_use]
    pub fn new(options: DecodedCacheOptions) -> Arc<Self> {
        Arc::new(Self {
            max_bytes: options.max_bytes,
            inner: Mutex::new(Inner {
                ready: LruCache::unbounded(),
                pending: HashMap::new(),
                used: 0,
                stores: HashMap::new(),
            }),
        })
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, Inner> {
        self.inner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// A store's identity while it lives: the address of its data.
    pub(crate) fn store_id(store: &Arc<dyn ObjectStore>) -> usize {
        Arc::as_ptr(store).cast::<()>() as usize
    }

    /// A handle for reads from `store`.
    pub(crate) fn handle(self: &Arc<Self>, store: &Arc<dyn ObjectStore>) -> DecodedRef {
        let id = Self::store_id(store);
        let mut inner = self.lock();
        let known = inner.stores.get(&id).map(|weak| weak.strong_count() > 0);
        if known == Some(false) {
            Self::purge(&mut inner, id);
        }
        inner.stores.insert(id, Arc::downgrade(store));
        DecodedRef {
            cache: self.clone(),
            store: id,
        }
    }

    /// Drop the entries of the store at `id` if it is gone.
    #[cfg(test)]
    pub(crate) fn forget_dead(&self, id: usize) {
        let mut inner = self.lock();
        if inner.stores.get(&id).is_some_and(|w| w.strong_count() == 0) {
            Self::purge(&mut inner, id);
        }
    }

    #[cfg(test)]
    pub(crate) fn entries_of(&self, id: usize) -> usize {
        self.lock()
            .ready
            .iter()
            .filter(|(k, _)| k.store == id)
            .count()
    }

    fn purge(inner: &mut Inner, id: usize) {
        let dead: Vec<Key> = inner
            .ready
            .iter()
            .filter(|(key, _)| key.store == id)
            .map(|(key, _)| key.clone())
            .collect();
        for key in dead {
            if let Some((_, weight)) = inner.ready.pop(&key) {
                inner.used -= weight;
            }
        }
        inner.stores.remove(&id);
    }

    fn insert(&self, inner: &mut Inner, key: Key, value: Value, weight: u64) {
        if weight > self.max_bytes {
            return;
        }
        if let Some((_, old)) = inner.ready.put(key, (value, weight)) {
            inner.used -= old;
        }
        inner.used += weight;
        while inner.used > self.max_bytes {
            match inner.ready.pop_lru() {
                Some((_, (_, w))) => inner.used -= w,
                None => break,
            }
        }
    }
}

/// Removes the keys a caller claimed but did not fill, and wakes their
/// waiters, however the load ended: an error, success, or a dropped future.
struct Claim {
    cache: Arc<DecodedCache>,
    keys: Vec<Key>,
    _wake: Vec<oneshot::Sender<()>>,
}

impl Drop for Claim {
    fn drop(&mut self) {
        let mut inner = self.cache.lock();
        for key in &self.keys {
            inner.pending.remove(key);
        }
    }
}

/// A [`DecodedCache`] bound to one store, as a query holds it.
#[derive(Clone)]
pub(crate) struct DecodedRef {
    cache: Arc<DecodedCache>,
    store: usize,
}

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

impl DecodedRef {
    fn key(&self, path: &Path, what: What) -> Key {
        Key {
            store: self.store,
            path: path.clone(),
            what,
        }
    }

    /// The structure `what` of `path`, from the cache or from `load`, which
    /// gives the value and its weight in bytes and runs at most once per call.
    pub(crate) async fn get_or_load<T, F, Fut>(
        &self,
        path: &Path,
        what: What,
        load: F,
    ) -> Result<Arc<T>>
    where
        T: Send + Sync + 'static,
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<(T, u64)>>,
    {
        let mut load = Some(load);
        let mut got = self
            .get_or_load_many(path, &[what], |_| {
                let load = load.take();
                async move {
                    match load {
                        Some(load) => Ok(vec![load().await?]),
                        // Only the first claim loads: a second would mean the
                        // first one's value vanished, which `get_or_load_many`
                        // never lets happen within one call.
                        None => Err(ItzmaError::InvalidQuery("a cache load ran twice")),
                    }
                }
            })
            .await?;
        got.pop()
            .ok_or(ItzmaError::InvalidQuery("a cache load gave no value"))
    }

    /// The structures `whats` of `path`, in order. `load` is given the
    /// positions in `whats` that are neither cached nor being loaded by another
    /// caller, and returns their values and weights in that order, so it can
    /// fetch them together. It runs once per round that finds such positions.
    pub(crate) async fn get_or_load_many<T, F, Fut>(
        &self,
        path: &Path,
        whats: &[What],
        mut load: F,
    ) -> Result<Vec<Arc<T>>>
    where
        T: Send + Sync + 'static,
        F: FnMut(Vec<usize>) -> Fut,
        Fut: Future<Output = Result<Vec<(T, u64)>>>,
    {
        let keys: Vec<Key> = whats.iter().map(|w| self.key(path, *w)).collect();
        let mut out: Vec<Option<Arc<T>>> = vec![None; keys.len()];
        loop {
            let mut claimed = Vec::new();
            let mut senders = Vec::new();
            let mut waits = Vec::new();
            {
                let mut inner = self.cache.lock();
                for (at, key) in keys.iter().enumerate() {
                    if out[at].is_some() {
                        continue;
                    }
                    let hit = inner.ready.get(key).map(|(value, _)| value.clone());
                    if let Some(typed) = hit.and_then(|value| value.downcast::<T>().ok()) {
                        out[at] = Some(typed);
                    } else if let Some(pending) = inner.pending.get(key) {
                        waits.push(pending.clone());
                    } else {
                        let (tx, rx) = oneshot::channel();
                        inner.pending.insert(key.clone(), rx.shared());
                        senders.push(tx);
                        claimed.push(at);
                    }
                }
            }
            if claimed.is_empty() && waits.is_empty() {
                break;
            }
            if !claimed.is_empty() {
                let claim = Claim {
                    cache: self.cache.clone(),
                    keys: claimed.iter().map(|at| keys[*at].clone()).collect(),
                    _wake: senders,
                };
                let values = load(claimed.clone()).await?;
                if values.len() != claimed.len() {
                    return Err(ItzmaError::InvalidQuery(
                        "a cache load gave the wrong number of values",
                    ));
                }
                let mut inner = self.cache.lock();
                for (at, (value, weight)) in claimed.iter().zip(values) {
                    let value = Arc::new(value);
                    let shared: Value = value.clone();
                    self.cache
                        .insert(&mut inner, keys[*at].clone(), shared, weight);
                    out[*at] = Some(value);
                }
                drop(inner);
                drop(claim);
            }
            for wait in waits {
                let _ = wait.await;
            }
        }
        Ok(out.into_iter().flatten().collect())
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use futures::channel::oneshot;
    use futures::future::FutureExt;
    use object_store::ObjectStore;
    use object_store::memory::InMemory;
    use object_store::path::Path;

    use super::*;
    use crate::error::ItzmaError;

    fn store() -> Arc<dyn ObjectStore> {
        Arc::new(InMemory::new())
    }

    fn cache(max_bytes: u64) -> Arc<DecodedCache> {
        DecodedCache::new(DecodedCacheOptions::default().max_bytes(max_bytes))
    }

    fn path(name: &str) -> Path {
        Path::from(name)
    }

    /// A load that counts its calls and gives `value` with weight `weight`.
    async fn counted(
        calls: &AtomicUsize,
        value: u64,
        weight: u64,
    ) -> crate::error::Result<(u64, u64)> {
        calls.fetch_add(1, Ordering::SeqCst);
        Ok((value, weight))
    }

    #[tokio::test]
    async fn two_concurrent_gets_of_one_key_load_once() {
        let s = store();
        let handle = cache(1 << 20).handle(&s);
        let calls = AtomicUsize::new(0);
        let (open, gate) = oneshot::channel::<()>();
        let gate = gate.shared();
        let a = path("a");

        let first = handle.get_or_load(&a, What::IndexHead, || async {
            calls.fetch_add(1, Ordering::SeqCst);
            let _ = gate.clone().await;
            Ok::<_, ItzmaError>((7u64, 8))
        });
        let second = handle.get_or_load(&a, What::IndexHead, || async {
            calls.fetch_add(1, Ordering::SeqCst);
            Ok::<_, ItzmaError>((9u64, 8))
        });
        let release = async {
            tokio::task::yield_now().await;
            let _ = open.send(());
        };
        let (a, b, ()) = futures::join!(first, second, release);
        assert_eq!(calls.load(Ordering::SeqCst), 1, "the waiter must not load");
        assert!(Arc::ptr_eq(&a.unwrap(), &b.unwrap()));
    }

    #[tokio::test]
    async fn a_hit_does_not_load_and_moves_the_entry_to_the_front() {
        let s = store();
        let handle = cache(100).handle(&s);
        let calls = AtomicUsize::new(0);
        let get = |name: &'static str, weight: u64| {
            let handle = handle.clone();
            let calls = &calls;
            async move {
                handle
                    .get_or_load(&path(name), What::IndexHead, || counted(calls, 1, weight))
                    .await
                    .unwrap()
            }
        };
        get("a", 60).await;
        get("b", 30).await;
        get("a", 60).await;
        assert_eq!(calls.load(Ordering::SeqCst), 2, "the third get is a hit");

        // 60 + 30 + 30 is over 100: the least recently used, `b`, goes.
        get("c", 30).await;
        get("a", 60).await;
        assert_eq!(calls.load(Ordering::SeqCst), 3, "a stayed");
        get("b", 30).await;
        assert_eq!(calls.load(Ordering::SeqCst), 4, "b was evicted");
    }

    #[tokio::test]
    async fn a_value_bigger_than_the_cache_is_returned_and_not_kept() {
        let s = store();
        let handle = cache(10).handle(&s);
        let calls = AtomicUsize::new(0);
        for _ in 0..2 {
            let got = handle
                .get_or_load(&path("big"), What::View, || counted(&calls, 5, 11))
                .await
                .unwrap();
            assert_eq!(*got, 5);
        }
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn a_failed_load_is_not_kept_and_the_next_caller_loads_again() {
        let s = store();
        let handle = cache(1 << 20).handle(&s);
        let failed = handle
            .get_or_load(&path("a"), What::Bloom, || async {
                Err::<(u64, u64), _>(ItzmaError::EmptyBatch)
            })
            .await;
        assert!(matches!(failed, Err(ItzmaError::EmptyBatch)), "{failed:?}");
        let calls = AtomicUsize::new(0);
        let got = handle
            .get_or_load(&path("a"), What::Bloom, || counted(&calls, 3, 1))
            .await
            .unwrap();
        assert_eq!((*got, calls.load(Ordering::SeqCst)), (3, 1));
    }

    /// A waiter whose loader fails, or is dropped mid-load, loads itself.
    #[tokio::test]
    async fn a_waiter_loads_itself_when_the_loader_fails_or_is_dropped() {
        for drop_the_loader in [false, true] {
            let s = store();
            let handle = cache(1 << 20).handle(&s);
            let (open, gate) = oneshot::channel::<()>();
            let gate = gate.shared();
            let a = path("a");
            let mut first = handle
                .get_or_load(&a, What::IndexHead, || async {
                    let _ = gate.clone().await;
                    Err::<(u64, u64), _>(ItzmaError::EmptyBatch)
                })
                .boxed();
            // The first caller claims the key and stops at the gate.
            assert!(futures::poll!(&mut first).is_pending());
            let calls = AtomicUsize::new(0);
            let second = handle.get_or_load(&a, What::IndexHead, || counted(&calls, 4, 1));

            // `second` is polled first and waits; then the loader fails or
            // goes away.
            let driver = async move {
                if drop_the_loader {
                    drop(first);
                } else {
                    let _ = open.send(());
                    assert!(first.await.is_err());
                }
            };
            let (got, ()) = futures::join!(second, driver);
            assert_eq!(*got.unwrap(), 4, "drop: {drop_the_loader}");
            assert_eq!(calls.load(Ordering::SeqCst), 1);
        }
    }

    #[tokio::test]
    async fn many_loads_only_the_missing_keys_in_one_call() {
        let s = store();
        let handle = cache(1 << 20).handle(&s);
        let p = path("idx");
        handle
            .get_or_load(&p, What::Partition(1), || async {
                Ok::<_, ItzmaError>((10u64, 1))
            })
            .await
            .unwrap();
        let asked = std::sync::Mutex::new(Vec::new());
        let got = handle
            .get_or_load_many(
                &p,
                &[What::Partition(0), What::Partition(1), What::Partition(2)],
                |missing: Vec<usize>| {
                    asked.lock().unwrap().push(missing.clone());
                    async move { Ok(missing.iter().map(|i| (*i as u64 * 100, 1)).collect()) }
                },
            )
            .await
            .unwrap();
        let values: Vec<u64> = got.iter().map(|v| **v).collect();
        assert_eq!(values, [0, 10, 200]);
        assert_eq!(*asked.lock().unwrap(), [vec![0, 2]]);
    }

    #[tokio::test]
    async fn two_stores_never_share_an_entry() {
        let (s1, s2) = (store(), store());
        let shared = cache(1 << 20);
        let (h1, h2) = (shared.handle(&s1), shared.handle(&s2));
        let calls = AtomicUsize::new(0);
        let a = h1
            .get_or_load(&path("x"), What::IndexHead, || counted(&calls, 1, 1))
            .await
            .unwrap();
        let b = h2
            .get_or_load(&path("x"), What::IndexHead, || counted(&calls, 2, 1))
            .await
            .unwrap();
        assert_eq!((*a, *b), (1, 2));
    }

    /// A store dropped and a new one allocated at the same address must not see
    /// the old store's entries.
    #[tokio::test]
    async fn a_dead_stores_entries_are_dropped_when_its_address_comes_back() {
        let shared = cache(1 << 20);
        let s1 = store();
        let id = DecodedCache::store_id(&s1);
        shared
            .handle(&s1)
            .get_or_load(&path("x"), What::IndexHead, || async {
                Ok::<_, ItzmaError>((1u64, 1))
            })
            .await
            .unwrap();
        drop(s1);
        shared.forget_dead(id);
        assert_eq!(shared.entries_of(id), 0);
    }

    #[test]
    fn options_default_to_256_mib() {
        assert_eq!(DecodedCacheOptions::default().max_bytes, 256 << 20);
        assert_eq!(DecodedCacheOptions::default().max_bytes(5).max_bytes, 5);
    }
}