LeaderTerm

object LeaderTerm

A leader term orders the leaders of a database's replica log, so that a superseded leader's writes are discarded on the read side (#5817).

The election counter in the low bits comes from the transport's leader election — a Kafka consumer-group generationId, or an in-process counter for the local logs — so it orders elections within one incarnation of that election mechanism, but does not survive the mechanism being reset. On Kafka that means the consumer group being deleted and recreated, which committed offsets hold off for offsets.retention.minutes past the group emptying but do not prevent (#5904); the local logs' counter dies with the process. The epoch is the operator's declaration that such a reset has happened.

It has to be declared rather than derived from the log. The leader this fence exists to stop is precisely one that can still reach the log while being out of touch with whatever elects leaders — so it can still read. An epoch taken from the log (its end offset, the persisted boundary term, the block index) would let that leader observe the progress of the leader which legitimately superseded it and thereby claim a higher term, inverting the fence.

Packed like a xtdb.types.MessageId and for the same reason: with the epoch in the top bits, plain numeric ordering is already lexicographic over (epoch, election), so every comparison site — the follower's fence, the persisted block boundary, the proto field — stays a Long.

Properties

Link copied to clipboard
const val NONE: Long = 0

The unset term, and the bottom of the ordering: carried by a record written before terms existed. Never fenced, so a mixed-version window doesn't discard a not-yet-upgraded leader's writes — proto3's scalar default yields this for those records for free.

Functions

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Renders a term for humans — the packed value on its own is unreadable.

Link copied to clipboard
fun of(termEpoch: Int, election: Long): Long