itzma-core 0.4.0

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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! A read-only look at one namespace: what is stored and what looks wrong.
//!
//! [`report`] loads the manifest and `schema.json` and lists `wal/` and
//! `segments/`. It never writes, and unlike [`Namespace::open`] it does not
//! heal a missing schema object, so it is safe to point at a namespace someone
//! else is running, or at one that is damaged. Nothing it finds is repaired:
//! a problem is a line in [`Report::problems`].
//!
//! The listings come after the manifest, as in [`gc`], so a segment
//! or a WAL object written in between is past the fold point and never looks
//! like damage. A namespace that is being written to can still show a passing
//! problem, such as a gap while a compaction's segment is half listed. Run it
//! again before believing it.
//!
//! It reads no object bodies apart from the manifest and the schema, so its
//! cost is two listings and two small GETs however large the namespace is. That
//! also means it cannot tell a WAL object's format version, or a segment's
//! document count, or whether an index object decodes: it shows which index the
//! manifest names for each segment and whether that object is there, and a
//! query is what finds a damaged one. A WAL object of exactly 8 bytes is a
//! fence, since every real object is longer.
//!
//! [`Namespace::open`]: crate::Namespace::open

use std::collections::{BTreeMap, HashSet};
use std::fmt;

use futures::TryStreamExt;
use object_store::path::Path;
use object_store::{ObjectMeta, ObjectStore};

use crate::document::Schema;
use crate::error::{ItzmaError, Result};
use crate::gc::{self, Found};
use crate::manifest::{self, AttrIndexEntry, IndexEntry, Manifest, SparseIndexEntry};
use crate::namespace::validate_id;
use crate::{schema_object, segment, wal_codec};

/// What [`report`] found in one namespace.
#[derive(Debug, Clone, PartialEq)]
pub struct Report {
    /// The namespace id that was inspected.
    pub namespace: String,
    /// The schema in `schema.json`, or `None` if the object is missing or
    /// unreadable (the second is also a problem).
    pub schema: Option<Schema>,
    /// The manifest, or `None` if it is missing or unreadable (also a problem).
    pub manifest: Option<Manifest>,
    /// The WAL objects listed under `wal/`.
    pub wal: WalReport,
    /// Every segment directory listed under `segments/`, oldest range first.
    pub segments: Vec<SegmentReport>,
    /// What looks wrong, one sentence each. Empty means nothing did.
    pub problems: Vec<String>,
}

/// A summary of the WAL objects in a namespace.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WalReport {
    /// How many WAL objects there are, fences included.
    pub objects: usize,
    /// The lowest seq listed.
    pub first: Option<u64>,
    /// The highest seq listed.
    pub last: Option<u64>,
    /// Total stored size of the objects.
    pub bytes: u64,
    /// How many are fences (8 bytes), left by garbage collection.
    pub fenced: usize,
    /// How many are past the manifest's fold point, so a query still reads
    /// them. Without a manifest, all of them.
    pub tail: usize,
    /// Runs of missing seqs as inclusive `(from, to)` pairs, from 1 up to the
    /// higher of the last seq listed and the fold point.
    pub gaps: Vec<(u64, u64)>,
}

/// One directory under `segments/`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SegmentReport {
    /// The segment id, `<lo>-<hi>` (see [`segment`]).
    pub id: String,
    /// First WAL seq the segment covers.
    pub lo: u64,
    /// Last WAL seq the segment covers.
    pub hi: u64,
    /// Total stored size of the objects in the directory.
    pub bytes: u64,
    /// Whether the manifest lists it, and if not, whether [`Namespace::gc`]
    /// would take it.
    ///
    /// [`Namespace::gc`]: crate::Namespace::gc
    pub state: SegmentState,
    /// The index the manifest names for this segment, if it names one.
    pub index: Option<IndexInfo>,
    /// Generations of index objects in the directory that the manifest does
    /// not name, oldest first. [`Namespace::gc`](crate::Namespace::gc) deletes
    /// them of a live segment once they are old enough.
    pub stray_indexes: Vec<u32>,
    /// Whether the directory has a `sparse.bin`, so some document in the
    /// segment carries a sparse vector.
    pub has_sparse_data: bool,
    /// The sparse index the manifest names for this segment, if it names one.
    pub sparse_index: Option<SparseIndexInfo>,
    /// Generations of sparse index objects in the directory that the manifest
    /// does not name, oldest first. [`Namespace::gc`](crate::Namespace::gc)
    /// deletes them of a live segment once they are old enough.
    pub stray_sparse_indexes: Vec<u32>,
    /// The attribute index the manifest names for this segment, if it names
    /// one.
    pub attr_index: Option<AttrIndexInfo>,
    /// Generations of attribute index objects in the directory that the
    /// manifest does not name, oldest first.
    /// [`Namespace::gc`](crate::Namespace::gc) deletes them of a live segment
    /// once they are old enough.
    pub stray_attr_indexes: Vec<u32>,
    /// Staging objects (`staging-<nonce>.bin`) in the directory: what a
    /// streamed write left when it stopped before deleting one.
    /// Not a problem; [`Namespace::gc`](crate::Namespace::gc) deletes them
    /// once they are old enough.
    pub staging_objects: usize,
}

/// An index the manifest names, and whether its object is there.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexInfo {
    /// The generation, as `index-<generation>.bin`.
    pub generation: u32,
    /// The algorithm's name in the manifest, such as `sq8`.
    pub kind: String,
    /// The object's stored size, or `None` if it is not in the directory (also
    /// a problem).
    pub bytes: Option<u64>,
}

/// A sparse index the manifest names, and whether its object is there.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SparseIndexInfo {
    /// The generation, as `sparse-idx-<generation>.bin`.
    pub generation: u32,
    /// The object's stored size, or `None` if it is not in the directory (also
    /// a problem).
    pub bytes: Option<u64>,
}

/// An attribute index the manifest names, and whether its object is there.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AttrIndexInfo {
    /// The generation, as `attr-idx-<generation>.bin`.
    pub generation: u32,
    /// The object's stored size, or `None` if it is not in the directory (also
    /// a problem).
    pub bytes: Option<u64>,
}

/// How a segment on disk stands against the manifest.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SegmentState {
    /// The manifest lists it.
    Live,
    /// Not live, and superseded: garbage collection deletes it once it is old
    /// enough. The rule is the one [`Namespace::gc`](crate::Namespace::gc)
    /// uses.
    Dead,
    /// Not live and not superseded. A compaction that has not published yet
    /// looks like this, and so does one that stopped before it did. It is also
    /// what GC leaves alone at `1-<folded>`.
    Orphan,
}

impl fmt::Display for SegmentState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(match self {
            Self::Live => "live",
            Self::Dead => "dead",
            Self::Orphan => "orphan",
        })
    }
}

/// Inspect the namespace `namespace` in `store`. See the module docs.
///
/// A manifest or schema object that is missing or cannot be decoded is
/// reported in [`Report::problems`], not returned as an error, so the rest of
/// the namespace still shows.
///
/// # Errors
///
/// - [`ItzmaError::InvalidNamespaceId`] for a malformed id.
/// - [`ItzmaError::Storage`] if the store fails.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> itzma_core::Result<()> {
/// use std::sync::Arc;
///
/// use itzma_core::{Metric, Namespace, Schema};
/// use object_store::memory::InMemory;
///
/// let store = Arc::new(InMemory::new());
/// Namespace::create(store.clone(), "docs", Schema::new(2, Metric::L2)).await?;
///
/// let report = itzma_core::inspect::report(&*store, "docs").await?;
/// assert!(report.problems.is_empty());
/// assert_eq!(report.wal.objects, 0);
/// # Ok(())
/// # }
/// ```
pub async fn report(store: &dyn ObjectStore, namespace: &str) -> Result<Report> {
    validate_id(namespace)?;
    let mut problems = Vec::new();

    let manifest = match manifest::load(store, namespace).await {
        Ok((manifest, _)) => Some(manifest),
        Err(e) => {
            problems.push(unreadable("manifest", e)?);
            None
        }
    };
    let schema = match schema_object::read(store, namespace).await {
        Ok(schema) => schema,
        Err(e) => {
            problems.push(unreadable("schema.json", e)?);
            None
        }
    };
    if let (Some(manifest), Some(schema)) = (&manifest, &schema) {
        if &manifest.schema != schema {
            problems.push(format!(
                "schema.json holds {schema:?} but the manifest holds {:?}",
                manifest.schema
            ));
        }
    }

    let prefix = Path::from(format!("{namespace}/segments"));
    let listing: Vec<ObjectMeta> = store.list(Some(&prefix)).try_collect().await?;
    let found = gc::group(&prefix, listing);
    let wal_objects = gc::list_wal(store, namespace).await?;

    let folded = manifest.as_ref().map_or(0, |m| m.wal_folded_through);
    let wal = summarize_wal(
        wal_objects.iter().map(|o| (o.seq, o.size)).collect(),
        folded,
    );
    for &(from, to) in &wal.gaps {
        problems.push(if from == to {
            format!("WAL seq {from} is missing")
        } else {
            format!("WAL seqs {from} through {to} are missing")
        });
    }

    let mut segments = Vec::new();
    if let Some(manifest) = &manifest {
        if let Err(e) = segment::validate_chain(&manifest.live_segments, folded) {
            problems.push(e.to_string());
        }
        for id in &manifest.live_segments {
            let data = segment::segment_path(namespace, id);
            let there = found
                .get(id)
                .is_some_and(|f| f.objects.iter().any(|(path, _)| *path == data));
            if !there {
                problems.push(format!("live segment {id} has no data object"));
            }
        }
        let live: HashSet<&str> = manifest.live_segments.iter().map(String::as_str).collect();
        segments = found
            .iter()
            .map(|(id, f)| {
                segment_report(
                    id,
                    f,
                    folded,
                    &live,
                    &manifest.indexes,
                    &manifest.sparse_indexes,
                    &manifest.attr_indexes,
                )
            })
            .collect();
        for report in &segments {
            if let Some(index) = &report.index {
                if index.bytes.is_none() {
                    problems.push(format!(
                        "segment {} is listed with index generation {} but has no such index object",
                        report.id, index.generation
                    ));
                }
            }
            if let Some(index) = &report.sparse_index {
                if index.bytes.is_none() {
                    problems.push(format!(
                        "segment {} is listed with sparse index generation {} but has no such object",
                        report.id, index.generation
                    ));
                }
            }
            if let Some(index) = &report.attr_index {
                if index.bytes.is_none() {
                    problems.push(format!(
                        "segment {} is listed with attribute index generation {} but has no such object",
                        report.id, index.generation
                    ));
                }
            }
        }
        for id in manifest.indexes.keys() {
            if !live.contains(id.as_str()) {
                problems.push(format!(
                    "the manifest lists an index for {id}, which is not a live segment"
                ));
            }
        }
        for id in manifest.sparse_indexes.keys() {
            if !live.contains(id.as_str()) {
                problems.push(format!(
                    "the manifest lists a sparse index for {id}, which is not a live segment"
                ));
            }
        }
        for id in manifest.attr_indexes.keys() {
            if !live.contains(id.as_str()) {
                problems.push(format!(
                    "the manifest lists an attribute index for {id}, which is not a live segment"
                ));
            }
        }
    } else {
        segments.extend(found.iter().map(|(id, f)| {
            segment_report(
                id,
                f,
                0,
                &HashSet::new(),
                &BTreeMap::new(),
                &BTreeMap::new(),
                &BTreeMap::new(),
            )
        }));
    }

    Ok(Report {
        namespace: namespace.to_string(),
        schema,
        manifest,
        wal,
        segments,
        problems,
    })
}

/// A missing or undecodable object becomes a problem line. A failure of the
/// store itself is not something to describe, so it is returned.
fn unreadable(what: &str, err: ItzmaError) -> Result<String> {
    match err {
        ItzmaError::NamespaceNotFound(_) => Ok(format!("{what} is missing")),
        ItzmaError::Serde { .. }
        | ItzmaError::UnsupportedFormat { .. }
        | ItzmaError::WalFormatTooNew { .. }
        | ItzmaError::SegmentFormatTooNew { .. } => Ok(format!("{what} cannot be read: {err}")),
        other => Err(other),
    }
}

fn segment_report(
    id: &str,
    found: &Found,
    folded: u64,
    live: &HashSet<&str>,
    indexes: &BTreeMap<String, IndexEntry>,
    sparse_indexes: &BTreeMap<String, SparseIndexEntry>,
    attr_indexes: &BTreeMap<String, AttrIndexEntry>,
) -> SegmentReport {
    let state = if live.contains(id) {
        SegmentState::Live
    } else if gc::is_dead(folded, live, id, found) {
        SegmentState::Dead
    } else {
        SegmentState::Orphan
    };
    let named = indexes.get(id).filter(|_| state == SegmentState::Live);
    let index = named.map(|entry| IndexInfo {
        generation: entry.generation,
        kind: entry.kind.clone(),
        bytes: found
            .indexes
            .iter()
            .find(|object| object.generation == entry.generation)
            .map(|object| object.size),
    });
    let mut stray_indexes: Vec<u32> = found
        .indexes
        .iter()
        .map(|object| object.generation)
        .filter(|generation| Some(*generation) != named.map(|entry| entry.generation))
        .collect();
    stray_indexes.sort_unstable();
    let named_sparse = sparse_indexes
        .get(id)
        .filter(|_| state == SegmentState::Live);
    let sparse_index = named_sparse.map(|entry| SparseIndexInfo {
        generation: entry.generation,
        bytes: found
            .sparse_indexes
            .iter()
            .find(|object| object.generation == entry.generation)
            .map(|object| object.size),
    });
    let mut stray_sparse_indexes: Vec<u32> = found
        .sparse_indexes
        .iter()
        .map(|object| object.generation)
        .filter(|generation| Some(*generation) != named_sparse.map(|entry| entry.generation))
        .collect();
    stray_sparse_indexes.sort_unstable();
    let named_attr = attr_indexes.get(id).filter(|_| state == SegmentState::Live);
    let attr_index = named_attr.map(|entry| AttrIndexInfo {
        generation: entry.generation,
        bytes: found
            .attr_indexes
            .iter()
            .find(|object| object.generation == entry.generation)
            .map(|object| object.size),
    });
    let mut stray_attr_indexes: Vec<u32> = found
        .attr_indexes
        .iter()
        .map(|object| object.generation)
        .filter(|generation| Some(*generation) != named_attr.map(|entry| entry.generation))
        .collect();
    stray_attr_indexes.sort_unstable();
    let has_sparse_data = found
        .objects
        .iter()
        .any(|(path, _)| path.filename() == Some("sparse.bin"));
    SegmentReport {
        id: id.to_string(),
        lo: found.lo,
        hi: found.hi,
        bytes: found.objects.iter().map(|(_, size)| size).sum(),
        state,
        index,
        stray_indexes,
        has_sparse_data,
        sparse_index,
        stray_sparse_indexes,
        attr_index,
        stray_attr_indexes,
        staging_objects: found.staging.len(),
    }
}

/// Summarize `(seq, size)` pairs against the fold point. Pure: no storage.
fn summarize_wal(mut objects: Vec<(u64, u64)>, folded: u64) -> WalReport {
    objects.sort_unstable();
    let mut gaps = Vec::new();
    let mut prev = 0u64;
    for &(seq, _) in &objects {
        if seq > prev.saturating_add(1) {
            gaps.push((prev + 1, seq - 1));
        }
        prev = prev.max(seq);
    }
    if folded > prev {
        gaps.push((prev + 1, folded));
    }
    WalReport {
        objects: objects.len(),
        first: objects.first().map(|&(seq, _)| seq),
        last: objects.last().map(|&(seq, _)| seq),
        bytes: objects.iter().map(|&(_, size)| size).sum(),
        fenced: objects
            .iter()
            .filter(|&&(_, size)| size == wal_codec::FENCE_LEN as u64)
            .count(),
        tail: objects.iter().filter(|&&(seq, _)| seq > folded).count(),
        gaps,
    }
}

impl fmt::Display for Report {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "namespace {}", self.namespace)?;
        match &self.schema {
            Some(schema) => writeln!(f, "schema.json  dim {}, {:?}", schema.dim, schema.metric)?,
            None => writeln!(f, "schema.json  none")?,
        }
        match &self.manifest {
            Some(m) => writeln!(
                f,
                "manifest     wal_format {}, segment_format {}, folded through {}, {} live segments",
                m.wal_format,
                m.segment_format,
                m.wal_folded_through,
                m.live_segments.len()
            )?,
            None => writeln!(f, "manifest     none")?,
        }
        let wal = &self.wal;
        write!(f, "wal          {} objects", wal.objects)?;
        if let (Some(first), Some(last)) = (wal.first, wal.last) {
            write!(f, ", seq {first} through {last}")?;
        }
        writeln!(
            f,
            ", {} bytes, {} fenced, {} in the tail",
            wal.bytes, wal.fenced, wal.tail
        )?;
        writeln!(f, "segments     {} on disk", self.segments.len())?;
        for s in &self.segments {
            write!(f, "  {}  {:<6}  {} bytes", s.id, s.state, s.bytes)?;
            if let Some(index) = &s.index {
                match index.bytes {
                    Some(bytes) => {
                        write!(
                            f,
                            "  index {} {} {bytes} bytes",
                            index.generation, index.kind
                        )?;
                    }
                    None => write!(f, "  index {} {} missing", index.generation, index.kind)?,
                }
            }
            if !s.stray_indexes.is_empty() {
                write!(f, "  unnamed indexes {:?}", s.stray_indexes)?;
            }
            if s.has_sparse_data {
                write!(f, "  sparse")?;
            }
            if let Some(index) = &s.sparse_index {
                match index.bytes {
                    Some(bytes) => write!(f, "  sparse index {} {bytes} bytes", index.generation)?,
                    None => write!(f, "  sparse index {} missing", index.generation)?,
                }
            }
            if !s.stray_sparse_indexes.is_empty() {
                write!(f, "  unnamed sparse indexes {:?}", s.stray_sparse_indexes)?;
            }
            if let Some(index) = &s.attr_index {
                match index.bytes {
                    Some(bytes) => {
                        write!(f, "  attribute index {} {bytes} bytes", index.generation)?
                    }
                    None => write!(f, "  attribute index {} missing", index.generation)?,
                }
            }
            if !s.stray_attr_indexes.is_empty() {
                write!(f, "  unnamed attribute indexes {:?}", s.stray_attr_indexes)?;
            }
            if s.staging_objects > 0 {
                write!(f, "  staging objects {}", s.staging_objects)?;
            }
            writeln!(f)?;
        }
        if self.problems.is_empty() {
            writeln!(f, "problems     none")
        } else {
            writeln!(f, "problems     {}", self.problems.len())?;
            for problem in &self.problems {
                writeln!(f, "  {problem}")?;
            }
            Ok(())
        }
    }
}

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

    fn seqs(list: &[(u64, u64)], folded: u64) -> WalReport {
        summarize_wal(list.to_vec(), folded)
    }

    #[test]
    fn an_empty_wal_has_no_gaps() {
        assert_eq!(seqs(&[], 0), WalReport::default());
    }

    #[test]
    fn a_contiguous_run_has_no_gaps() {
        let r = seqs(&[(1, 100), (2, 50), (3, 8)], 2);
        assert_eq!(r.objects, 3);
        assert_eq!((r.first, r.last), (Some(1), Some(3)));
        assert_eq!(r.bytes, 158);
        assert_eq!(r.fenced, 1);
        assert_eq!(r.tail, 1);
        assert!(r.gaps.is_empty());
    }

    #[test]
    fn the_input_order_does_not_matter() {
        let r = seqs(&[(3, 20), (1, 20), (2, 20)], 0);
        assert!(r.gaps.is_empty());
        assert_eq!((r.first, r.last), (Some(1), Some(3)));
    }

    #[test]
    fn a_missing_start_and_a_missing_middle_are_gaps() {
        let r = seqs(&[(3, 20), (4, 20), (8, 20)], 0);
        assert_eq!(r.gaps, vec![(1, 2), (5, 7)]);
    }

    #[test]
    fn a_fold_point_past_the_last_object_is_a_gap() {
        let r = seqs(&[(1, 20), (2, 20)], 5);
        assert_eq!(r.gaps, vec![(3, 5)]);
        assert_eq!(r.tail, 0);
    }

    #[test]
    fn a_fold_point_at_the_last_object_is_not() {
        assert!(seqs(&[(1, 20), (2, 20)], 2).gaps.is_empty());
    }

    #[test]
    fn a_seq_listed_twice_is_not_a_gap() {
        let r = seqs(&[(1, 20), (1, 20), (2, 20)], 0);
        assert!(r.gaps.is_empty());
    }
}