Read-ahead and fetch
Queues

Read-ahead and fetch

How a queue seat reads its range, keeps records just ahead of their deal, and fetches the rest again.

A seat holds an entry for every message it has read, but the record itself only just ahead of its deal. Its read-ahead aims at half a second of what it deals, so a seat's record memory follows its worker's pace. A record the seat could not keep is fetched again just before its deal, and a seat that is behind its range reads each record once.

Where it lives

File in internal/queueWhat it holds
readahead.goThe read-ahead's target, the pacing of the read, and which records are kept
demand.goThe rate a seat deals at, which sets the target
fetch.goRecord fetches: when, what, and through which read
pages.goCold pages reserved from the node's window budget
internal/daemon/recordfetch.goThe node's record reads, for its own seats and for peers

The data

HeldCharged toReleased when
A window entry per message read and not settledThe seat's window, from the moment it is readThe message is settled
A record kept ahead of its dealThe read-ahead, inside the windowIt is sent to a worker, or given back
A record fetched for an entryThe window, or the fetch pool for a seat with nothing to dealIt is sent, or dropped
The rest of a read's batchThe window, as the batch in handThe seat takes it
A cold page from object storageThe window budget, beside the windowIts records are handed to the seat

The read-ahead target

The seat follows the bytes it deals a second, smoothed with a half-life of 1 second. Its target is that rate times half a second, at least 64 KiB, and at most the cap: the smaller of ReadAheadBytes (1 MiB) and half the seat's limit. In the node's last eighth, the cap shrinks to what that eighth grants the seat.

internal/queue/demand.go
func (d *demand) target(now time.Time, limit int64) int64 {
	want := int64(d.rateAt(now) * aheadLead.Seconds())

	return max(min(want, limit), min(aheadFloor, limit))
}

A seat dealing 4 MiB a second aims at 2 MiB of records, then holds to its 1 MiB cap. A seat whose worker takes one small message a second holds 64 KiB. Until its read first reports the end of the range, a seat aims at its cap.

The read

Ask for a piece, up to the room under the target Records An entry for each; keep records that fit Credit Fetch records for the next entries, in deal order Records Claim, with its record Sent: let the record go, keep the entry Range read Seat Fetcher Worker
A seat's records: read just ahead of the deal, fetched again when the read could not keep them.
  1. The seat subscribes to its range from its ack frontier. Each batch from the read is about 256 KiB. The seat asks for a piece no larger than the room under its target, and holds the rest of the batch in hand.
  2. It makes an entry for every message in the piece. It keeps a message's record only while the seat has a worker, every earlier message of the same key holds its record, and the record fits under the target. It drops the other records with the batch. A kept record that outlives the turn its batch came in is copied out of the batch, so it pins nothing else.
  3. The read is paced. It waits while a message the seat may deal now lacks its record, or while the read-ahead is at its target. It refills a quarter of the target at a time, about once every 125 ms of dealing. A seat behind its range therefore reads each record once and fetches nothing.
  4. If the read has stood held for HoldFull (10 seconds) with nothing dealt, it reads on to fill its window, so a seat whose worker stopped still learns what its range holds.

A seat with no worker drops its read-ahead, releases its cold page, and reads no further.

The record fetch

A record fetch reads the range again for the records of the entries the seat would deal next. A seat fetches when all of these hold:

internal/queue/fetch.go
func (s *Seat) fetchDue() bool {
	if !s.reading || s.fetching || s.fetchStalled || s.lost != nil || len(s.takers) == 0 {
		return false
	}

	if !s.mayClaim(0) {
		return false
	}

	if !s.win.wantsRecords(s.opts.Clock().Add(s.opts.FrontierBatch)) {
		return false
	}

	return s.creditPast(max(s.win.dealableRecords()-s.opts.FetchRecords, 0))
}

The seat is reading, has no fetch out, and its last fetch did not fail. It may claim, so it is not held to its share on a contended node. Ready entries it would deal next lack their records, counting keys whose wait ends within 250 ms. And its workers hold more unspent credit of the range's class than the records it holds, less one fetch. With no credit held it fetches nothing, so a worker that stops granting credit stops the seat's reads.

A fetch gathers positions in deal order, round after round: each key's quantum of bytes per round, or one message per key on a key-ordered queue. It looks at up to 1,024 entries, and stops at 256 records, 1 MiB of their sizes, or the room for their charges. It joins positions across gaps of up to 256 KiB, reading and dropping the records in a gap. Each read is at most 256 records and 1 MiB. A fetch makes up to 8 reads at once, and each read has 3 seconds to answer.

When another node leads the range, each read is one call to that node's ReadStreamRecords, under this node's credential. The leader serves it from its tail window cache, then its cold path, and caps an answer at 500 records and 1 MiB. Otherwise the node reads the range itself. A node serves at most 64 record reads at once, for its own seats and for peers, and a read past that waits. A read for a peer takes no local slot.

Each record that comes back is matched to its entry. Positions a read did not reach are asked for again by the next fetch. A read below the range's trim point returns the trim point: the seat checks the purge point first, then expires the entries whose records it does not hold, counted as record_gone. Any other failure stalls the fetch until the next save, and the seat deals what it holds meanwhile.

Sending and handing back

A dealt record goes with its delivery until it is sent: written to the work stream, put into a one-shot claim's answer, or dropped when its session closes. The seat then lets the record go, and the claim keeps only its entry. A record dealt in the turn its batch came in is not copied, so until it is sent the seat counts that whole batch.

A message retried in place, after a nack, a lapsed claim, or an unattempted hand-back, goes back to its key's head without its record. The next fetch reads it once.

Giving back at a save

Every 250 ms, at its save, a seat checks what it holds against what the node can spare:

  1. Above its share while another seat wants, it gives back read-ahead, the latest in deal order first, then ready entries, as the window budget describes.
  2. With no credit held, it releases what it holds from the fetch pool.
  3. It lets go of the pool's records for keys at their cap, and fetches them again once the key can be dealt.
  4. It lets its cold page go if it has no worker, or has dealt nothing from the page for HoldFull while the node is past three quarters of its budget.

Crash points

Nothing on this page is stored. A node that stops loses its read-ahead, fetches, and entries. The next seat reads the range again from the saved ack frontier, so a crash costs reads, never messages.

Concurrency

One goroutine owns the seat's read-ahead and entries. The fetcher answers on its own goroutine, and the seat matches results to entries on its own. Fetch reads draw on the node's 64 read slots, shared with peers. Cold pages are reserved under a lock, first come, first served.

Bounds and signals

BoundValue
Read-aheadHalf a second of dealing, from 64 KiB to 1 MiB, at most half the seat's limit
A read's batchAbout 256 KiB
One fetch1 per seat, up to 8 reads, 256 records and 1 MiB each, 3 seconds each
Record reads a node serves64 at once
A leader's answer to a peer500 records and 1 MiB

Signals:

  • nova.queue.deals, by record: kept or fetched
  • nova.queue.record_fetch.reads, nova.queue.record_fetch.records, nova.queue.record_fetch.failed
  • nova.queue.read_ahead.target and nova.queue.window.read_again.reads
  • nova.read.slot_waits
  • nova.queue.messages.expired, with cause="record_gone"
  • Log queue: a seat could not fetch its messages' records; it deals what it holds until the store answers
  • Log queue: a seat that dealt nothing from its cold page let it go; the star needs the room

Tests

  • The target and pacing: TestTheTargetIsTheRateDealtTimesHalfASecond in demand_test.go; TestASeatBehindItsRangeReadsEachRecordOnce and TestAStalledSeatReadsOnToItsWindowAndIsolatesAFlood in paced_test.go.
  • Fetches: TestCreditGatesTheFetch, TestTheStarBoundsTheReadsInFlight, TestDealOrderStandsWithEveryRecordFetched in fetch_test.go; TestAFetchJoinsSmallGapsIntoOneRead in coalesce_test.go.
  • The pool: TestTheFetchPoolKeepsSeatsDealing in share_test.go; TestAKeyAtItsCapIsNotFetchedFromThePool in windowbudget_test.go.

Go deeper

On this page