Repository navigation
fix(ingest): event de-duplication idempotency, file rotation deduplication, and bounded eventLog - #3004
Open
isasadiq wants to merge 1 commit into
Open
fix(ingest): event de-duplication idempotency, file rotation deduplication, and bounded eventLog#3004isasadiq wants to merge 1 commit into
isasadiq wants to merge 1 commit into
Conversation
…and bounded eventLog
|
@isasadiq Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This branch has not been deployed
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.
Closes #2905
Summary
Resolves the check-then-act race, rotation amnesia, process-then-record inconsistency, and O(file) ingest bottleneck in
POST /api/v1/eventsby introducing an atomic SQLite-backedEventIdLedger, multi-file rotation-aware deduplication inFileRawEventStore, and bounded memory ring-buffer inEventProcessor.eventLog.Claim Key Decision & Justification
We evaluated using the event
id(event.id) versus the blockchain natural key(tx_hash, event_index). We choseevent.idas the primary ledger claim key for the following reasons:POST /api/v1/eventsreturns per-item response entries keyed byid({ id, status: "processed" | "duplicate" | "failed" | "rejected" }).routes/v1/index.ts,getStableEventId(event)derives${event.txHash}:${event.eventIndex}whenever an explicitevent.idis not supplied, naturally collapsing to the chain-guaranteed uniqueness key while supporting explicit IDs and unit test fixtures (claim("e1")).processed_events(id TEXT PRIMARY KEY)provides O(1) indexed lookups and atomicINSERT ... ON CONFLICT DO NOTHING.Claim / Process / Complete State Machine
How the Four Failure Modes are Closed
Race Condition (Check-then-act):
hasEventId(id)thenprocessEvent(event)thenstoreEvents([event])with awaits in between allowed concurrent callers with identical events to both evaluatehasEventIdas false and execute duplicate side effects.EventIdLedger.claim(id)executes an atomic SQLiteINSERT INTO processed_events (id, status, claimed_at) VALUES (?, 'claimed', ?) ON CONFLICT(id) DO NOTHING. Exactly one concurrent caller receives"claimed"(changes === 1), and all concurrent or subsequent callers receive"duplicate".Process-then-record Inconsistency:
processEventsucceeded butstoreEventsthrew, the claim wasn't recorded. A subsequent indexer retry would re-execute side effects.processEventsucceeds,eventIdLedger.complete(id)transitions the event status to'completed'BEFORE disk archive write. IfstoreEventsthrows, the HTTP route reports failure, but subsequent retries observe"duplicate"and bypass re-execution of side effects. IfprocessEventthrows,eventIdLedger.release(id)removes the claim so the failed event can be retried cleanly.Rotation Amnesia:
FileRawEventStore.hasEventId()only read the activeevents.jsonlfile. When files rotated toevents-<timestamp>.jsonl, past IDs were forgotten.FileRawEventStore.hasEventId()now scans across all rotated generation files (events*.jsonl) with an in-memoryseenIdscache, ensuring duplicate detection persists across file rotations.O(file) per Event Scan:
hasEventId()read and JSON-parsed the entire active archive file on each event (up to 100 MB per event).processed_eventsSQLite table in O(1) time. Archive writes maintain a persistentFileHandleand check size before writing, removing redundant disk stats and file system re-reads.Unbounded Memory (
eventProcessor.eventLog):eventLogto a configurable ring buffer (default 10,000 entries) viasetMaxLogEntries()and automatic slicing, preventing unbounded heap growth while maintaining cursor-based pagination inlistEvents().Test Suite Output & Performance Benchmark