Skip to content

fix(collector): shard snapshot bootstrap across N producers and pin covered=0 fail-closed invariant (#857) - #858

Merged
proerror77 merged 2 commits into
mainfrom
codex/collector-snapshot-parallel-replay
Aug 12, 2026
Merged

proerror77 merged 2 commits into
mainfrom
codex/collector-snapshot-parallel-replay

Conversation

@proerror77

@proerror77 proerror77 commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Change

  • Snapshot bootstrap now runs across N concurrent producers (SNAPSHOT_PRODUCERS, default 8) instead of one sequential REST producer. Active symbols are sharded round-robin; a tokio::sync::broadcast channel fans the "all websocket shards connected" notification out to every producer (subscribers register synchronously in the spawn loop before any task polls, so no notification is missed). The rotation barrier accounts for shards + N producers, and StreamCoverageVerified is emitted once by the lead producer. This removes the all-or-nothing bottleneck where a single RTT-bound producer (~2 symbols/s) could not sync 1377 symbols inside a short segment.
  • Added a regression guard pinning the fail-closed invariant: a segment that closes before every declared symbol syncs yields has_replay_safe_checkpoint=false, covered_symbol_count=0, and verify_binance_market_tape_for_strict_gate rejects it.

Issue relationship

Refs #857

Validation

  • cargo test -p hft-collector --features collector-binance --locked --bin binance-lob-archiver → 103 passed.
  • cargo clippy -p hft-collector --all-targets --features collector-binance --no-deps --locked -- -D warnings → clean.
  • git diff --check → clean.
  • Full crate test: 331 passed; 4 pre-existing binance_fee_upload failures on macOS (/var symlink "directory has an indirect ancestor"), unrelated to this change and green on Linux CI.

Runtime impact and rollback

  • New env var SNAPSHOT_PRODUCERS (default 8). Default config is the intended production path; no manifest/schema change. Rollback = revert this commit (restores the single-producer path).

Summary by CodeRabbit

  • New Features

    • Added configurable parallel snapshot collection with round-robin symbol distribution.
    • Added coordinated rate limiting across snapshot producers.
    • Improved coordination between snapshot producers and websocket coverage notifications.
    • Added coverage verification during snapshot rotation.
  • Bug Fixes

    • Improved synchronization handling when websocket coverage is incomplete.
    • Improved notification delivery across concurrent snapshot processes.
  • Tests

    • Expanded coverage for producer partitioning, shared rate limiting, notification fan-out, and synchronization scenarios.

…overed=0 fail-closed invariant (#857)

The all-or-nothing checkpoint gate, combined with a single sequential REST
snapshot producer (RTT-bound to ~2 symbols/s), cannot sync 1377 symbols inside
a short shadow segment. Every segment then closes with has_replay_safe_checkpoint
=false and covered=0, which the strict verifier rejects after a 37-minute CI +
install + soak.

- Add SNAPSHOT_PRODUCERS (default 8); shard active_symbols() round-robin across
  N producers and fan the "all websocket shards connected" notification out over
  a tokio broadcast channel. Subscribers register synchronously in the spawn
  loop before any task polls, so no connection notification is missed.
- Account every snapshot producer in the rotation barrier (shards + N); emit
  StreamCoverageVerified once from the lead producer.
- Pin the fail-closed invariant with a regression guard: a segment that closes
  before every declared symbol syncs yields has_replay_safe_checkpoint=false,
  covered_symbol_count=0, and verify_binance_market_tape_for_strict_gate rejects
  it.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 69a48409-c249-4b23-8ec2-957a80a74d95

📥 Commits

Reviewing files that changed from the base of the PR and between 7b1f839 and c597640.

📒 Files selected for processing (1)
  • rust_hft/tools/collector/src/bin/binance-lob-archiver.rs

📝 Walkthrough

Walkthrough

The Binance LOB archiver now supports configurable parallel snapshot producers. It partitions symbols across producers, broadcasts websocket coverage notifications, shares REST rate limiting, synchronizes all producers during rotation, and expands coordination tests.

Changes

Parallel snapshot production

Layer / File(s) Summary
Producer configuration and symbol partitioning
rust_hft/tools/collector/src/bin/binance-lob-archiver.rs
The archiver reads SNAPSHOT_PRODUCERS, caps the count against active symbols, and assigns symbols with round-robin partitioning.
Websocket coverage, rate limiting, and rotation coordination
rust_hft/tools/collector/src/bin/binance-lob-archiver.rs
Session startup creates one task per symbol shard. Producers receive broadcast websocket coverage notifications and share one REST rate limiter. Rotation barriers count completion across all producers.
Assigned snapshot processing and validation
rust_hft/tools/collector/src/bin/binance-lob-archiver.rs
produce_snapshots processes caller-provided symbols. Tests cover partitioning, synchronization, websocket behavior, retries, pauses, and exclusions.

Estimated code review effort: 4 (Complex) | ~60 minutes

Mergeability Score: ⚪ Minimal · up to c5976

The PR parallelizes snapshot bootstrap and adds a fail-closed regression guard; based on the supplied validation, no actionable merge-blocking risk remains.

Sequence Diagram(s)

sequenceDiagram
  participant WebsocketReaders
  participant BroadcastChannel
  participant LeadSnapshotProducer
  participant SnapshotProducers
  participant RotationBarrier
  WebsocketReaders->>BroadcastChannel: Publish verified stream coverage
  BroadcastChannel->>LeadSnapshotProducer: Deliver coverage notification
  LeadSnapshotProducer->>SnapshotProducers: Start assigned snapshot work
  SnapshotProducers->>RotationBarrier: Report completion and synchronize rotation
Loading

Possibly related issues

Possibly related PRs

  • proerror77/monday#127 — Modifies websocket-readiness coordination that this PR extends to multiple producers.
  • proerror77/monday#126 — Modifies produce_snapshots and snapshot-completion signaling in the same archiver.
  • proerror77/monday#358 — Modifies rotation barriers and producer coordination extended by this PR.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main changes: parallel snapshot producers and the fail-closed coverage invariant.
Description check ✅ Passed The description includes all required sections and provides clear change details, issue linkage, validation results, and rollback information.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/collector-snapshot-parallel-replay

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rust_hft/tools/collector/src/bin/binance-lob-archiver.rs`:
- Around line 1254-1266: Aggregate InitialSnapshotsComplete events from every
snapshot producer before initializing sync_deadline; currently the first shard
completion starts the timeout prematurely. Update the event-handling flow around
produce_snapshots_after_streams_connect and sync_deadline to track
expected_shards completions, then begin the deadline only after all shards
report completion. Add a multi-producer test covering one delayed shard.
- Around line 3342-3346: Replace the per-producer interval created near the
snapshot request loop with one shared rate limiter initialized before all
producers start, and have every producer acquire permission from it before
issuing a REST snapshot request. Ensure SNAPSHOT_REQUESTS_PER_SECOND is enforced
globally across producers, then add a test that verifies aggregate request
pacing rather than per-producer pacing.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 28f5f091-eb15-42db-82c4-cdbd7502832a

📥 Commits

Reviewing files that changed from the base of the PR and between 0e913bc and 7b1f839.

📒 Files selected for processing (1)
  • rust_hft/tools/collector/src/bin/binance-lob-archiver.rs

Comment thread rust_hft/tools/collector/src/bin/binance-lob-archiver.rs Outdated
Comment thread rust_hft/tools/collector/src/bin/binance-lob-archiver.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7b1f839b22

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rust_hft/tools/collector/src/bin/binance-lob-archiver.rs Outdated
Comment thread rust_hft/tools/collector/src/bin/binance-lob-archiver.rs Outdated
Comment thread rust_hft/tools/collector/src/bin/binance-lob-archiver.rs Outdated
… limit

- Count InitialSnapshotsComplete across every producer before arming the
  sync deadline, so a fast shard cannot start the clock early.
- Replace per-producer interval sleeps with one shared tokio::time::Interval
  so N shards cannot multiply SNAPSHOT_REQUESTS_PER_SECOND by N.
- Subscribe snapshot producers before spawning websocket tasks so an early
  coverage notification cannot be dropped into an empty broadcast set.
@proerror77
proerror77 merged commit f679d51 into main Aug 12, 2026
42 checks passed
@proerror77
proerror77 deleted the codex/collector-snapshot-parallel-replay branch August 12, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant