Fix duplicate _id generation by using MongoDB-managed _id and _key-based upserts#442
Open
FestinBiju wants to merge 1 commit into
Open
Fix duplicate _id generation by using MongoDB-managed _id and _key-based upserts#442FestinBiju wants to merge 1 commit into
FestinBiju wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request removes application-managed MongoDB _id generation from the LiveTiming ingestor document model, relying instead on MongoDB’s native _id handling while keeping a stable _key to support idempotent writes/upserts and avoid restart-related ID collisions (issue #439).
Changes:
- Removed the custom timestamp/counter-based
_idgeneration logic (sync + async). - Updated
Document.to_mongo_doc_*serialization to stop injecting_idand to document MongoDB-managed_id, while still adding_key.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
49
to
51
| mongo_doc = self.__dict__ | ||
| mongo_doc["_key"] = self._get_key_str() | ||
| mongo_doc["_id"] = _generate_mongo_id_sync() | ||
| return mongo_doc |
Comment on lines
56
to
58
| mongo_doc = self.__dict__ | ||
| mongo_doc["_key"] = self._get_key_str() | ||
| mongo_doc["_id"] = await _generate_mongo_id_async() | ||
| return mongo_doc |
Owner
|
Hello @FestinBiju, This issue is pretty complex though. Let's continue the discussion here: #439 (comment) |
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 fixes duplicate document ID collisions reported in #439 by removing custom
_idgeneration logic that depended on timestamp + in-memory counter behavior.Instead of overriding MongoDB
_id, writes now rely on MongoDB-managed_idvalues and use a stable logical key (_key) for idempotent upserts.Fixes #439
Root Cause
The previous
_idstrategy was not restart-safe:_idwas derived from the current millisecond plus an in-memory counter.What Changed
_idassignment in ingestion and write paths._keyinstead of_id._key(or the appropriate compound logical key where required).upsert: true.Why This Is Safe
_idcollisions.Verification Performed
_idcollisions._key._keyor the intended compound key.Notes on Migration / Backfill
If duplicate records already exist for
_key, unique-index creation may fail until the data is cleaned up.Recommended rollout:
_key-based upserts.Risk / Impact
Low to moderate — this change touches the write/upsert path and indexing.
The main risk is pre-existing duplicate logical keys during unique-index creation. This is mitigated through the deduplication step described above.