diff --git a/ddp_lib/dataset_indexes.py b/ddp_lib/dataset_indexes.py index a25d1b4..f3b02fa 100644 --- a/ddp_lib/dataset_indexes.py +++ b/ddp_lib/dataset_indexes.py @@ -13,6 +13,7 @@ KINDS = {'REFERENCE': 'references', 'POPULATION': 'populations', 'CLEANED_TABLE': 'cleaned_tables'} DEPENDENCIES = {'tables_ids': 'REFERENCE', 'population_ids': 'POPULATION', 'cleaned_table_ids': 'CLEANED_TABLE'} +_ANY_DATA_TOKEN = object() @dataclass @@ -345,10 +346,12 @@ def reconcile(self, target, connection=None, *, finish=True, mode=IndexCreationM ) raise - def repair(self, target): + def repair(self, target, *, expected_token=_ANY_DATA_TOKEN): """Retry committed data in post-load mode; manual repair stays online.""" with self.guard(target) as connection: document = target.collection.find_one({"_id": target.id}) or {} + if expected_token is not _ANY_DATA_TOKEN and document.get('index_data_token') != expected_token: + raise lifecycle.IndexRepairSuperseded("This repair is outdated. Indexes were not changed because a newer data load owns the table.") if document.get("index_data_ready") is False: raise lifecycle.IndexRepairError( "The current data attempt has not committed; resume its data job" diff --git a/ddp_lib/queue_identity.py b/ddp_lib/queue_identity.py new file mode 100644 index 0000000..c2f694c --- /dev/null +++ b/ddp_lib/queue_identity.py @@ -0,0 +1,48 @@ +"""Active queue identities shared by API producers, Sync and rollout recovery.""" + +from uuid import uuid4 + +from pymongo import ReturnDocument +from pymongo.errors import DuplicateKeyError + +REPAIR_ACTION = "RECONCILE_INDEXES" + + +def active_job_key(data): + # Retain the deployed data-job key. Only repair receives a separate lane. + key = f"{data.get('schema_name')}:{data['target_id']}" + return key + ":" + REPAIR_ACTION if data.get("action") == REPAIR_ACTION else key + + +def enqueue(collection, data): + """Atomically deduplicate data jobs and repair jobs independently. + + All producers must be upgraded together. Move an old repair key with a CAS + before upsert, so an existing repair cannot absorb a new data refresh. + Conflicting legacy repair documents fail closed for operator review. + """ + collection.create_index( + "active_key", + unique=True, + partialFilterExpression={"active_key": {"$exists": True}}, + ) + data = dict(data) + data.setdefault("_id", uuid4().hex) + base = active_job_key(dict(data, action=None)) + collection.update_one( + {"active_key": base, "action": REPAIR_ACTION}, + {"$set": {"active_key": base + ":" + REPAIR_ACTION}}, + ) + data["active_key"] = active_job_key(data) + try: + return collection.find_one_and_update( + {"active_key": data["active_key"]}, + {"$setOnInsert": data}, + upsert=True, + return_document=ReturnDocument.AFTER, + ) + except DuplicateKeyError: + existing = collection.find_one({"active_key": data["active_key"]}) + if existing is None: + raise + return existing diff --git a/ddp_lib/secondary_indexes.py b/ddp_lib/secondary_indexes.py index 4813195..e811c26 100644 --- a/ddp_lib/secondary_indexes.py +++ b/ddp_lib/secondary_indexes.py @@ -42,6 +42,10 @@ def __init__(self, message, result=None): self.result = result +class IndexRepairSuperseded(IndexRepairError): + """A queued repair belongs to a data version that has been replaced.""" + + def catalog_signature(physical, definition): # Compare index definitions independently of their names. signature = parse_index(physical.definition).signature