Support resume and purge modes for index recovery#453
Open
DylanWelzel wants to merge 6 commits into
Open
Conversation
- Introduced INDEX_MODES for better index handling options. - Enhanced _config method to include supported index modes. - Refactored mexists method to process IDs in batches for efficiency. - Added client closure logic to ensure proper resource management.
DylanWelzel
marked this pull request as ready for review
July 14, 2026 21:24
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
This PR adds first-class index recovery modes so a hub or frontend caller can recover from an interrupted or failed indexing job without having to manually clean up Elasticsearch state.
The branch has been updated with the latest
1.1.xchanges and this PR now targets1.1.x.The new supported modes are exposed by
index_managerand passed through the existing/indexcommand:index: create a fresh index and fail if it already exists, preserving the existing default behavior.resume: keep an existing index and index only documents that are missing from Elasticsearch.purge: delete an existing index first, then recreate and index from scratch.merge: preserve the existing merge behavior for hot/cold indexing flows.Why This Is Needed
The frontend needs to give operators a safe choice when a full index already exists or when a previous indexing run failed partway through.
Before this change, the default
indexpath intentionally failed if the target index already existed. That is the right default for avoiding accidental overwrite, but it made recovery awkward:This PR makes the recovery behavior explicit in the backend API so the frontend can present a clear
resumevspurgedecision when an index exists.What Changed
Index mode metadata
IndexManager.index_info()now includes anindex_modesdictionary describing the supported modes. This gives callers a stable backend-provided contract instead of hardcoding all mode labels and descriptions in the UI.Job state context
The index job state registrar now accepts extra context when a step starts. The pre-index step records the requested mode when the mode is not the default
indexmode.This helps build logs and UI state explain whether a recovery attempt was a resume, purge, or other non-default indexing operation.
Pre-index behavior
The existing pre-index mode handling is preserved and made easier for callers to discover:
indexrequires the target index not to exist.resumeandmergerequire the target index to already exist and skip index creation.purgedeletes the target index if present, then creates a new one.Resume implementation
The resume path now checks which IDs are already present in Elasticsearch and indexes only the missing IDs.
The existence check is batched so large indexing batches do not issue one huge IDs query or exceed common Elasticsearch search window constraints. The shared
_index_ids()helper also avoids mutatingself.idsduring resume.Client cleanup
Index, merge, and resume tasks now close their Elasticsearch and Mongo clients in
finallyblocks. This keeps process workers from holding unnecessary connections after a batch finishes or fails.Index lookup compatibility
IndexManager.get_indexes_by_name()now accepts either_meta.stats.totalor_meta.stats.total_documentswhen building/indexes_by_nameresults.This fixes a compatibility issue seen after a purge/reindex run where Elasticsearch contained the newly created index, but
/indexes_by_namereturned no result because the index metadata only hadtotal_documents. Without this fallback, the frontend could not rediscover the new index in dropdown lookups even though the index was present and populated in Elasticsearch.User Impact
Operators can now safely recover indexing from the frontend or API:
resumewhen an index partially completed and mappings/settings are still valid.purgewhen a clean rebuild is safer because mappings/settings changed or the index may be inconsistent.Existing callers that do not pass a mode continue to use the default
indexbehavior.The
/indexes_by_nameendpoint now also remains compatible with indexes whose metadata stores the count astotal_documents, so freshly purged/reindexed indexes continue to appear in frontend index selectors.Validation
origin/1.1.xintoresume-purge-logicwithout conflicts.su12: Elasticsearch haddisease_20260331_wentol7t, while/indexes_by_name?index_name=disease_20260331_wentol7t&env_name=su12_es8&limit=1returned an empty result because the metadata usedstats.total_documents.python3 -m py_compile biothings/hub/dataindex/indexer.py biothings/hub/dataindex/indexer_registrar.py biothings/hub/dataindex/indexer_task.pygit diff --check origin/1.1.x...resume-purge-logicNotes
This PR is intentionally scoped to the backend contract and indexing behavior. The frontend can consume the existing
/indexendpoint withmode: "resume"ormode: "purge"and can read mode descriptions from/index_manager.