Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ddp_lib/dataset_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
48 changes: 48 additions & 0 deletions ddp_lib/queue_identity.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions ddp_lib/secondary_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading