Skip to content

fix(engine)!: keep a field in pending_reindex while a document that may have lost its data is live, instead of removing it after any lossless rebuild (#1890) - #1898

Merged
mosuka merged 1 commit into
mainfrom
fix/pending-reindex-after-lossless-reindex
Dec 5, 2026

Conversation

@mosuka

@mosuka mosuka commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Summary

Schema::pending_reindex lists the fields whose data a Destructive change discarded. Engine::update_field removed a field from it after any later rebuild that lost nothing, though such a rebuild re-derives the field from stored values and raw vectors, which the documents that lost the data do not have. Now a field stays there while a document that may have lost its data is live, and leaves once each such document has been deleted or put again and committed, or when the field is deleted.

Closes #1890

The bug

The investigation reproduced it on ce5f3baf for both kinds of field:

  • Lexical: an Integer field, not stored, becomes a Float (Destructive: a1-a4 lose its data), stored is turned on, b1-b4 are written, and indexed is turned off (a Reindex). The field left pending_reindex, though a range on it still finds 4 of the 8 documents.
  • Vector: an HNSW field's dimension goes from 2 to 3 (Destructive), b1-b4 are written, and m goes from 16 to 8 (a Reindex). The field left pending_reindex, though a1-a4 have no vector.

The rule

Each put gets a new internal id, larger than every id before it, and putting an id again deletes the old copy (DocumentLog::append_put). So the documents that may have lost a field's data are the live documents whose id is below the next id at the change. The change records that id with the field, and the field leaves once no live document is below it. LexicalLiveness::live_upper_bound (#1798) never counts fewer live documents than there are, so a field can leave late, never early. As in Vespa, whose reindexing status stays pending until every document has been processed, the field is reported until the documents are dealt with; here the user puts them again.

Changes

  • engine/schema.rs:
    • Schema::pending_reindex_below: BTreeMap<String, u64>, the id recorded with each field (#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]).
    • Schema::record_pending_reindex(name, below) records a field and keeps the larger id when it is recorded again.
    • The docs of both fields say when a field is recorded, when it leaves and when that is checked.
  • lexical/index/inverted/liveness.rs: LexicalLiveness::may_hold_live_below(id), whether a document below id may be live, from live_upper_bound.
  • engine.rs:
    • Recording: a destructive rebuild and a change between a lexical and a vector option record the field with next_doc_id(). Ingestion waits on the schema lock, so every document that lost the data has a smaller id.

    • A lossless rebuild no longer removes the field.

    • release_pending_reindex removes each field with no live document below its id. Holding the in-memory schema for writing, it removes a field only if its id is unchanged, so a field recorded again in the meantime stays.

    • When it runs:

      • after each Engine::commit called, CommitPolicy::EveryDocs's among them;
      • in an update_field that rebuilds a field, before it saves the schema;
      • when the engine is built, after recovery (settle_pending_reindex_on_open).

      CommitPolicy::Interval's background commits do not check: the timer thread has no schema.

    • Opening: a field in pending_reindex without an id, as one written before this change, gets the next id after recovery, which covers every document there is. The schema is saved if that changed it.

    • delete_field and the build's cleanup of names the schema does not have remove the id with the name.

    • Saving: every save of the schema goes through persist_current_schema, which copies the schema and saves the copy under a new schema_save_lock. A commit removes fields and saves without the schema lock, which a put holds, so it can save alongside a schema change; under the save lock an older copy cannot be saved after a newer one. A failed save after a commit or at open is logged, and the commit or the open succeeds: the saved schema keeps the field, which is checked again at the next open.

  • laurus-server/src/convert/schema.rs, laurus-cli/src/commands/create.rs: their struct literals get the new field, empty. The proto and the HTTP gateway still list names only; building an engine gives a listed field without an id the next one.
  • Docs: the pending_reindex paragraphs of concepts/schema_and_fields.md and laurus-cli/commands.md (en/ja) say when a field leaves, and when the engine checks.

Changes from the plan

  • A failed save after a commit is not undone in memory. The plan undid the removal. The memory then says what is true (no document that lost the data is live), and the saved schema errs on the safe side: the field is checked again at the next open.
  • The commit checks without the schema lock. The first version took the lock with try_write, which also fails while a put holds it for reading. A server that ingests all the time would rarely have checked. The new save lock orders the saves instead, as above. The commits that CommitPolicy::EveryDocs makes now check too.
  • update_field checks only when it rebuilds a field, not on a MetadataOnly change; the docs say so.
  • Four more tests than planned, and two existing tests changed (below).
  • The measurement uses an index of 100,000 documents, not T3's index of 8.

What a user sees (!)

  • Schema gains a public field, so a struct literal of it needs pending_reindex_below (as the server and CLI did).
  • A schema.toml that holds the new key cannot be read by an older laurus (deny_unknown_fields, fix(schema): an analyzer component's unknown key is ignored #1700). The key is written while a field is pending: by a destructive change, and by the first open of an index whose schema.toml lists a field in pending_reindex.
  • A field that stayed recorded only because no rebuild came after its loss now leaves once its documents are gone. An index with no live document leaves nothing recorded after a destructive change.

Tests

  • laurus/tests/engine_pending_reindex_test.rs (new): each test runs for a lexical and a vector field, and checks the schema the engine saved through its hook as well as the one in memory.
    • T1, T2: the investigation's sequences; the field stays after the lossless rebuild, and the lexical range still finds 4 documents.
    • T3: the field stays while one of a1-a4 is old, and leaves once all are put again and committed; it also leaves once they are deleted and committed, not before the commit.
    • T4: reopened with the saved schema, the field and its id stay, and the field leaves once a1-a4 are put again.
    • T5: a saved schema without the id (as before this change). Opening gives the field the next id and saves it. The field stays while b4 is old, so the id is not one too small, and leaves once every document is put again.
    • A field recorded twice keeps the later id: after a second destructive change, putting a1-a4 again is not enough; b1-b4 lost the data too.
    • A change of kind records an id, lexical to vector and back: the field leaves once the documents are put again.
    • Deleting the field removes its id, in memory and in the saved schema.
  • engine.rs:
    • New, a_commit_checks_pending_reindex_while_a_put_holds_the_schema_lock: a commit while the schema lock is held for reading, as a put holds it, removes the field and saves.
    • Changed, fix(engine): delete_field leaves the field's name in pending_reindex #1548's build_drops_pending_reindex_names_of_fields_not_in_the_schema: the index now holds a document written before, so title stays recorded; with no document it would leave at build. It also checks that the id of the dropped name goes.
  • liveness.rs: may_hold_live_below(id) agrees with "some id below id may be live" by may_be_live, over ranges with deletions, a set with a gap, a segment with every document deleted, a segment of unknown membership and no segment.
  • Changed, fix(schema): an analyzer component's unknown key is ignored #1700's schema_unknown_keys_test.rs: the fixture holds an id, and pending_reindex_below is listed with the maps keyed by names, where an unknown key is a field name.

RED

Each of these, put in alone, fails the tests named; the code was then restored and the same tests (497) passed again:

Put in Fails
a lossless rebuild removes the field again T1, T2
no check after a commit T3
opening gives an id one too small T5
may_hold_live_below ignores deletions and membership the liveness.rs test
a field recorded twice keeps the first id recorded twice
a change of kind records the name only change of kind
delete_field keeps the id deleting the field
the commit checks only when try_write takes the schema lock the new engine.rs test
a commit's removal is not saved T3, T4
opening does not save the id it gives T5

Not covered by a test: two saves racing each other under the save lock.

Measurements

The check after a commit takes a liveness snapshot while a field is recorded. A commit with nothing to commit returns before the commit steps, so timing one isolates the check. On evo-x2 (CPUs 8-15, release), on an index of 100,000 documents built by 10 commits of 10,000, the median of 2,000 such commits, 5 times:

pending_reindex Median per commit
empty 0.05 µs (all 5)
v recorded 1.08 µs (all 5)

So the check adds about 1 µs to a commit while a field is recorded, and nothing otherwise.

Checks

On evo-x2 (Linux x86_64), on the commit of this PR:

  • cargo fmt --all -- --check;
  • Clippy on the workspace, all targets, embeddings-all, -D warnings, and on laurus-wasm for wasm32-unknown-unknown;
  • scripts/check-rustdoc.py, check-ci-filters.py, check-job-timeouts.py (with their tests) and check-binding-snippets.py;
  • tests, all passed: laurus (embeddings-all,pq-fastscan) 4,269 passed and 28 ignored, laurus-server 334, laurus-mcp 53, laurus-cli 89.

The bindings, aarch64, Windows and the index interop are left to CI.

Out of scope

…ay have lost its data is live, instead of removing it after any lossless rebuild (#1890)

Schema::pending_reindex lists the fields whose data a destructive change
discarded. update_field removed a field from it after any later rebuild
that lost nothing, though a rebuild re-derives the field from stored
values and raw vectors, which the documents that lost the data do not
have.

A destructive change now records the field with the next internal
document id, in the new Schema::pending_reindex_below, keeping the larger
id when the field is recorded again. Every put gets a new, larger id, so
the documents that may have lost the data are the live ones below it.
The field leaves once LexicalLiveness::may_hold_live_below says none is
live: after each Engine::commit called (CommitPolicy::EveryDocs's
included), in an update_field that rebuilds a field, and when the engine
is built, which also gives a field recorded without an id the next one.
delete_field removes the id with the name.

Every save of the schema now copies and saves it under a new save lock,
so that a commit can remove fields and save without the schema lock,
which a put holds, and never leave an older copy on disk than a schema
change saved.

BREAKING CHANGE: Schema gains the public field pending_reindex_below, and
a schema.toml holding it cannot be read by an older laurus
(deny_unknown_fields). It is written while a field is pending.

Closes #1890
@mosuka
mosuka merged commit 838c581 into main Dec 5, 2026
29 checks passed
@mosuka
mosuka deleted the fix/pending-reindex-after-lossless-reindex branch December 5, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(engine): a lossless Reindex removes a field from pending_reindex though the documents that lost its data remain

1 participant