Repository navigation
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
Conversation
…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
8 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Schema::pending_reindexlists the fields whose data aDestructivechange discarded.Engine::update_fieldremoved 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
ce5f3baffor both kinds of field:Integerfield, not stored, becomes aFloat(Destructive:a1-a4lose its data),storedis turned on,b1-b4are written, andindexedis turned off (aReindex). The field leftpending_reindex, though a range on it still finds 4 of the 8 documents.Destructive),b1-b4are written, andmgoes from 16 to 8 (aReindex). The field leftpending_reindex, thougha1-a4have 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.lexical/index/inverted/liveness.rs:LexicalLiveness::may_hold_live_below(id), whether a document belowidmay be live, fromlive_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_reindexremoves 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:
Engine::commitcalled,CommitPolicy::EveryDocs's among them;update_fieldthat rebuilds a field, before it saves the schema;settle_pending_reindex_on_open).CommitPolicy::Interval's background commits do not check: the timer thread has no schema.Opening: a field in
pending_reindexwithout 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_fieldand 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 newschema_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.pending_reindexparagraphs ofconcepts/schema_and_fields.mdandlaurus-cli/commands.md(en/ja) say when a field leaves, and when the engine checks.Changes from the plan
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 thatCommitPolicy::EveryDocsmakes now check too.update_fieldchecks only when it rebuilds a field, not on aMetadataOnlychange; the docs say so.What a user sees (
!)Schemagains a public field, so a struct literal of it needspending_reindex_below(as the server and CLI did).schema.tomlthat 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 whoseschema.tomllists a field inpending_reindex.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.a1-a4is old, and leaves once all are put again and committed; it also leaves once they are deleted and committed, not before the commit.a1-a4are put again.b4is old, so the id is not one too small, and leaves once every document is put again.a1-a4again is not enough;b1-b4lost the data too.engine.rs: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.build_drops_pending_reindex_names_of_fields_not_in_the_schema: the index now holds a document written before, sotitlestays 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 belowidmay be live" bymay_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.schema_unknown_keys_test.rs: the fixture holds an id, andpending_reindex_belowis 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:
may_hold_live_belowignores deletions and membershipliveness.rstestdelete_fieldkeeps the idtry_writetakes the schema lockengine.rstestNot 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_reindexvrecordedSo 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;embeddings-all,-D warnings, and onlaurus-wasmforwasm32-unknown-unknown;scripts/check-rustdoc.py,check-ci-filters.py,check-job-timeouts.py(with their tests) andcheck-binding-snippets.py;laurus(embeddings-all,pq-fastscan) 4,269 passed and 28 ignored,laurus-server334,laurus-mcp53,laurus-cli89.The bindings, aarch64, Windows and the index interop are left to CI.
Out of scope
Destructive. Next.CommitPolicy::Interval's background commits do not check; documented.