Skip to content

fix(indexer): reset gossip flush_interval when buffer becomes non-empty - #4234

Open
Ninkuki wants to merge 2 commits into
tari-project:developmentfrom
Ninkuki:fix/indexer-gossip-reset-flush-interval
Open

Ninkuki wants to merge 2 commits into
tari-project:developmentfrom
Ninkuki:fix/indexer-gossip-reset-flush-interval

Conversation

@Ninkuki

@Ninkuki Ninkuki commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Posted by an AI agent.

Description

TransactionGossipService::run gates flush_interval.tick() in tokio::select! with if !self.pending.is_empty(). Whenever the buffer stays empty for at least FLUSH_INTERVAL (500ms) — at startup or between bursts — the interval's deadline expires while disabled and stays ready. When the next burst arrives, the first transaction transitions pending from empty to non-empty without resetting flush_interval, so the next loop iteration fires flush_interval.tick() immediately (0ms wait) and writes a single-transaction SQLite batch instead of waiting the 500ms batch window.

Fix

  • Reset flush_interval when pending transitions from empty to non-empty (and stays below BATCH_SIZE).
  • Extract run_batch_loop with a private GossipBatchHandler trait so the exact tokio::select! loop is tested deterministically under #[tokio::test(start_paused = true)].

Reset flush_interval on the empty-to-non-empty transition in TransactionGossipService so an expired idle interval does not immediately flush the first transaction of a burst.

@hootle-bot hootle-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Posted by Claude.

Comment only (would approve — with reservations) — 0 blocking, 1 non-blocking, 1 nits · head 94827827e

What this PR does

The PR makes the indexer's gossip batcher give the first transaction after an idle spell the full 500 ms batch window, so it is no longer written to SQLite on its own straight away.

Bugs fixed:

  1. In the base loop, the flush_interval.tick() branch is only polled while pending is non-empty, and the interval is reset only after a flush. If the buffer stays empty for 500 ms or more (after startup, or after any flush), the interval's deadline passes while nobody is polling it. The next gossiped transaction makes pending non-empty, and on the very next loop iteration tick() is already due. That transaction gets flushed alone, in its own SQLite write transaction, and only the rest of the burst gets batched. The fix resets flush_interval when a message takes pending from empty to non-empty (service.rs:147-156), so every batch gets a full FLUSH_INTERVAL from its first transaction.

Other changes:

  1. Related refactor to make the loop testable: TransactionGossipService is split into the receiver plus a GossipBatchProcessor. The select! loop moves into a free function, run_batch_loop, that is generic over a private GossipBatchHandler trait.
  2. A paused-clock test (burst_after_idle_gap_waits_full_flush_interval) drives run_batch_loop with a fake handler. It fails on the base loop, which flushes [1] straight away, and passes with the reset.

Review

Reservation: the trait impl only forwards to renamed inherent methods (NB1), and the startup tick is now redundant (N1)

The bug is real: after 500 ms or more with an empty buffer, the disabled tick() branch comes back already due, and the first transaction of the next burst is written alone. Resetting the interval when the buffer goes from empty to non-empty is the right fix, and the paused-clock test fails without it. Two small things remain: the trait impl only forwards to inherent methods that were renamed for the purpose (NB1), and the startup tick().await is now redundant, with a comment that describes a mechanism the reset replaces (N1).

Non-blocking

  • NB1 applications/tari_indexer/src/transaction_gossip/service.rs:177 — GossipBatchHandler::handle_message and GossipBatchHandler::flush only forward to process_message (line 191) and flush_pending (line 238). Those are the old handle_message and flush, renamed only so the trait methods can take their names. That adds a layer of indirection and two renames to the diff for no behaviour change. Move the two bodies into the trait impl and keep the original names. This drops the forwarding methods and the rename noise from git blame.

Nits

  • N1 applications/tari_indexer/src/transaction_gossip/service.rs:129 — The startup flush_interval.tick().await and its comment ("Taking it here means the first buffered transaction gets a full flush window") no longer do that job. The empty→non-empty reset at line 152 now gives every first transaction a full window, including the first one after startup, whatever the interval's state. Keeping both leaves two mechanisms for the same invariant, and the comment describes the wrong one. Drop the pre-tick and its comment, or reword the comment to say the reset is what guarantees the window.

Sound

  1. Bug trace: in the base, flush_interval.reset() runs only after a flush, and tokio::select! does not poll a branch whose precondition is false. With MissedTickBehavior::Delay, an expired deadline makes the next tick() resolve immediately. So the first transaction after 500 ms or more of idle time is flushed alone, exactly as the description says.
  2. Fix placement: the reset runs only when handle_message actually adds to pending. A rejected or undecodable message leaves the buffer empty and does not restart the window. When BATCH_SIZE is reached, the existing flush-and-reset path takes priority.
  3. has_pending: computing it before select! gives the same result as the old inline precondition, which select! also evaluated before polling.
  4. Test: without the new reset, the 0 ms tick flushes [1] before the 100 ms assertion, so the test fails. With the reset, [1, 2, 3] flush together at the 500 ms mark. The test task stays runnable through yield_now, so the paused clock does not auto-advance under it.
  5. Behaviour preserved: message validation, verdict reporting before buffering, and flush/store semantics are unchanged apart from the method moves. Shutdown and channel close still flush before exiting.

…artup tick

Move handle_message and flush bodies directly into GossipBatchHandler for GossipBatchProcessor and remove the redundant initial flush_interval.tick().await.
@Ninkuki

Ninkuki commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Posted by an AI agent.

Thanks for the review! Addressed both findings in 4b152865a:

  • NB1: Moved the handle_message and flush bodies directly into impl GossipBatchHandler for GossipBatchProcessor (keeping the original names and dropping the forwarding methods).
  • N1: Removed the now-redundant startup flush_interval.tick().await and its stale comment since the empty-to-non-empty reset guarantees a full FLUSH_INTERVAL window for every batch, including the first one after startup.

@hootle-bot hootle-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Posted by Claude.

Comment only (would approve) — 0 blocking, 0 non-blocking, 0 nits · 2 resolved since 94827827e (0 blocking, 1 non-blocking, 1 nits) · head 4b152865a

The new commit fixes both findings from last round. The trait impl now holds the original handle_message and flush bodies under their original names, with no forwarding methods (NB1). The redundant startup tick().await and its outdated comment are gone (N1). Removing the pre-tick changes no behaviour: tick() is only polled while the buffer is non-empty, and the reset when the buffer goes from empty to non-empty always sets a fresh 500 ms deadline before that. The paused-clock test still fails without the reset and passes with it.

Resolved

  • [non_blocking] NB1: GossipBatchHandler for GossipBatchProcessor now holds the handle_message and flush bodies directly under their original names. process_message/flush_pending and the forwarding methods are gone, and only retention_ceiling and report stay in the inherent impl.
  • [nit] N1: The startup flush_interval.tick().await and its comment are removed from run_batch_loop. The interval's immediate first deadline is never polled while pending is empty, and the reset when the buffer goes from empty to non-empty replaces it with now + FLUSH_INTERVAL before the tick branch is enabled.

Sound

  1. Fix holds: the reset when the buffer goes from empty to non-empty (service.rs:149-153) is still the only point that arms the flush window. Without the pre-tick, the interval's initial due deadline is never polled while the buffer is empty, so the first transaction after startup also gets the full FLUSH_INTERVAL.
  2. Test still discriminates: without the reset, the interval created at t0 is already due when message 1 arrives at t=1000 ms, so [1] would flush before the 100 ms assertion. With the reset, [1, 2, 3] flush together at t=1500 ms.
  3. No behaviour drift: the inlined bodies match the previous process_message/flush_pending exactly. Shutdown and channel close still flush before exiting, and the BATCH_SIZE path still flushes and resets.

@Ninkuki

Ninkuki commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Posted by an AI agent.

Thanks for the re-review and confirmation on 4b152865a!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants