Skip to content

[feature][broker] Journaling bucket tracker: per-topic body-copy experiment scaffold - #45

Open
nodece wants to merge 5 commits into
masterfrom
journaling-bucket-tracker
Open

nodece wants to merge 5 commits into
masterfrom
journaling-bucket-tracker

Conversation

@nodece

@nodece nodece commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Motivation

Delayed messages stay in the data ledgers and due delivery replays them by position — a random read per message once the backlog outgrows the caches (load test: read:consume = 1, ~14k random IOPS per bookie, consumption capped by the disk). The body-copy design fixes that with a copy of every delayed entry in a deliverAt-ordered store, so due reads become sequential, at the price of writing each delayed entry twice. Before building the whole thing, this PR delivers the write side of that trade as a per-topic selectable experiment, so both variants can run and be measured on one broker.

Modifications

  • DelayedDeliveryTracker.addEntry(ledgerId, entryId, deliverAt, entryData) — a default no-op that receives the serialized entry at the only point where the broker still holds the buffer (the dispatcher's delayed-entry filter, AbstractBaseDispatcher). Trackers that don't store copies pay nothing; the dispatcher forwards via a new trackDelayedDelivery overload whose default delegates to the existing three-argument variant.
  • JournalingBucketTrackerFactory extends the bucket factory and routes by topic name: topics ending in -dj get the journaling tracker, everything else keeps the regular bucket tracker untouched. One broker, both implementations, same traffic: produce to mytopic and mytopic-dj and compare.
  • JournalingBucketDelayedDeliveryTracker extends the bucket tracker with the delivery side unchanged, and additionally frames and appends every delayed entry into a BookKeeper journal ledger (arrival order, 24-byte header: deliverAt | dataLedgerId | dataEntryId, 50k entries per ledger, rollover by count). Journal failures are counted and never affect delivery.
  • The bucket factory's fields and newTracker0 become protected so custom factories in subpackages can build their own trackers.

Deliberate limitations (scaffold scope)

  • The journal is write-only: due delivery still replays from the data ledgers. Switching the delivery read to the journal is the follow-up, gated on the write-side measurements (journal bytes/latency vs. the random-read savings).
  • Not durable across restarts: journal ledgers are not recorded in cursor properties, and a restart leaves them behind. Experiment scaffold, not production.

Testing

  • JournalingBucketTrackerTest: journal failure path is counted and delivery tracking is unaffected; framed header size contract.
  • 3 consecutive rounds of org.apache.pulsar.broker.delayed.* + all *DispatcherMultipleConsumers* suites green (routing, dispatcher overload and the existing trackers are regression-covered).
  • checkstyle / spotless clean.

Select with:

delayedDeliveryTrackerFactoryClassName=org.apache.pulsar.broker.delayed.bucket.journal.JournalingBucketTrackerFactory

Assisted-by: Claude Code

…riment scaffold

Delayed messages stay in the data ledgers, and due delivery replays them
by position, which is a random read per message once the backlog exceeds
the caches. The body-copy design answers that with a copy of every
delayed entry in a deliverAt-ordered store, so the due reads become
sequential — at the price of writing every delayed entry twice.

This is the write-side scaffold of that design, selectable per topic so
both variants can be measured on one broker:

- DelayedDeliveryTracker.addEntry(ledgerId, entryId, deliverAt, entryData):
  a default no-op that receives the serialized entry at the only point
  where the broker holds the buffer (the dispatcher's delayed-entry
  filter). Trackers that don't store copies pay nothing.
- JournalingBucketTrackerFactory extends the bucket factory and routes by
  topic name: topics ending in -dj get a JournalingBucketDelayedDeliveryTracker,
  everything else keeps the regular bucket tracker untouched.
- JournalingBucketDelayedDeliveryTracker extends the bucket tracker
  unchanged on the delivery side and additionally frames and appends every
  delayed entry into a BookKeeper journal ledger (arrival order, 50k entries
  per ledger, own client). Journal failures are counted and never affect
  delivery.
- The bucket factory's fields and newTracker0 become protected so custom
  factories in subpackages can build their own trackers.

Deliberate limitations of the scaffold: the journal is write-only (due
delivery still replays from the data ledgers), and it is not durable
across restarts (the ledgers are not recorded in cursor properties).
Switching the delivery read to the journal and journal lifecycle
management are the follow-ups, gated on the write-side measurements.

Assisted-by: Claude Code
@nodece
nodece force-pushed the journaling-bucket-tracker branch from 1638c77 to 662bd21 Compare November 27, 2026 02:37
…ially

The journaling tracker's due path now reads from the time-window journals
instead of replaying positions from the data ledgers, so the dispatch
round performs no random read for journaled entries:

- JournalStorage routes copies by delivery time window (60s), one ledger
  per window, arrival order within it. The drain reads each window's
  ledger sequentially from a saved offset and holds at the first record
  that is not due yet, so the reads are always sequential and the
  disorder a consumer observes is bounded by the window width.
- JournalingBucketDelayedDeliveryTracker.readDueAndReplay drives the
  dispatch round: the drain's records are served when the inherited index
  still tracks their position (removeIndexBit is the single pending-truth
  gate, so neither path can double-deliver) and the cursor has not
  acknowledged them, wrapped with their original positions. What the
  index still reports as due after the drain — failed or still-in-flight
  journal copies — is replayed from the data ledgers through the regular
  cursor path and merged into the same completion. A journal read
  failure falls the whole round back to the regular replay.
- PersistentDispatcherMultipleConsumers dispatches journaling trackers
  through readDueAndReplay instead of getScheduledMessages plus
  asyncReplayEntries, keeping the round's scheduling and error recovery
  identical to the regular path.
- removeIndexBit becomes protected so the journaling tracker can gate on
  the inherited index.

Not durable across restarts: the journal ledgers and drain offsets are
not recorded, so a restart falls back to the inherited delivery entirely.

Assisted-by: Claude Code
@nodece

nodece commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Read side is in: due delivery now reads from the journals sequentially

The second commit (5b4f7c94f54) completes the experiment: the dispatch round for a journaling tracker no longer replays positions from the data ledgers. Instead:

  1. Time-window journals (60s per window, one ledger per window): copies are routed by deliverAt at the only point the broker holds the buffer. Within a window the arrival order is preserved.

  2. Sequential drain: each window ledger is read from a saved offset in batches, holding at the first record not yet due — so the reads are always sequential, and the within-window disorder a consumer sees is bounded by the window width.

  3. Index-gated delivery: a drained record is served only when the inherited bucket index still tracks its position (removeIndexBit is the gate) and the cursor has not acknowledged it. The index bitmap remains the single source of pending truth — the journal cannot double-deliver, and entries are wrapped with their original positions, so acks/cursors/redelivery are unchanged.

  4. Fallback for the unjournaled: whatever the index still reports as due after the drain (failed or in-flight copies) is replayed from the data ledgers through the regular cursor path, merged into the same completion. Journal read failure falls the whole round back to the regular replay.

  5. Dispatcher branch: PersistentDispatcherMultipleConsumers dispatches journaling trackers through readDueAndReplay instead of getScheduledMessages + asyncReplayEntries, with identical scheduling and error recovery.

Known limitation: not durable across restarts (drain offsets and journal ledgers are not recorded). A restart falls back entirely to the inherited delivery.

Verification: 3 consecutive rounds of org.apache.pulsar.broker.delayed.* + all *DispatcherMultipleConsumers* suites green on the rebased branch. checkstyle/spotless clean.

The dispatcher dispatched journaling trackers through an instanceof check on
the concrete class. Extract the capability into a dedicated interface:

- DueEntryReader: a delayed delivery tracker that reads the due entries —
  the serialized message bodies — itself, instead of reporting positions for
  the dispatcher to replay from the data ledgers. Named after what the
  callback receives (ManagedLedger Entry, not a decoded Message) and what
  the operation is (a sequential read).
- readDueEntries(callback, ctx, maxMessages): the tracker captures its own
  cursor at construction from the public DelayedDeliveryContext, so the
  interface carries no dispatcher-owned dependencies.
- The dispatcher checks instanceof DueEntryReader and no longer imports the
  journal package; the journaling tracker implements the interface with the
  method previously named readDueAndReplay.

No behavior change: 3 rounds of the delayed delivery and dispatcher suites
green.

Assisted-by: Claude Code
… on clear/close

The journal ledgers were never deleted — a disk leak at 4.3GB per 60s window
per subscription. Three lifecycle fixes:

- readDue deletes a window ledger when the window has expired (its end time
  is past the cutoff) and the drain is past its last entry: no future read
  can come from that ledger, regardless of ack state. The deletion is
  asynchronous and failure-tolerant (a retry happens after the next restart,
  which starts from offset 0 and re-drains everything anyway).
- clear() overrides the inherited reset to also delete every journal ledger
  and reset the drain state, so a subscription reset does not leave stale
  copies behind.
- close() deletes all journal ledgers before closing the BookKeeper client,
  so a broker shutdown does not leave orphans (the journal is not durable
  across restarts by design).

Assisted-by: Claude Code
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