Class FencingToken
- All Implemented Interfaces:
Comparable<FencingToken>
Kafka's transaction coordinator mints the pair when a member calls
InitProducerId for the transactional.id of the role. The quorum picks
the values, every broker enforces them, and a write from an older pair fails with a
fenced error. A holder passes the token to every guarded write, and the broker checks
it. No clock takes part in that check.
The token is a pair, and compareTo(FencingToken) reads the producer id
first and the producer epoch second. The order is lexicographic. A producer epoch is
a short and it wraps. Kafka handles the exhaustion with a fresh producer id
and an epoch of zero. A comparison on the epoch alone ranks that fresh token below
the stale one, and the old leader keeps the role after about 32000 leadership
changes. Never compare the epoch on its own.
NO_EPOCH is the token of a role that no member has ever taken. It ranks
below every minted token.
Example
FencingToken wrapped = FencingToken.of(4, Short.MAX_VALUE);
FencingToken fresh = FencingToken.of(5, (short) 0);
assertThat(fresh.supersedes(wrapped)).isTrue();
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FencingTokenThe token of a role that no member has ever taken. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(FencingToken other) Orders two tokens lexicographically.booleanReports whether another object is a token with the same pair.inthashCode()Returns a hash of the pair.booleanminted()Reports whether this token names a minted leadership.static FencingTokenof(long producerId, short producerEpoch) Builds a token from a minted producer id and producer epoch.static FencingTokenParses theproducer_id:producer_epochform thattoString()writes.shortReturns the producer epoch the transaction coordinator minted.longReturns the producer id the transaction coordinator minted.booleansupersedes(FencingToken other) Reports whether this token ranks above another one.toString()Returns theproducer_id:producer_epochform.
-
Field Details
-
NO_EPOCH
The token of a role that no member has ever taken.Kafka writes
-1for "no producer", and that pair proves no leadership.of(long, short)rejects a negative value, so no minted token can equal this one and every minted token supersedes it.
-
-
Method Details
-
of
Builds a token from a minted producer id and producer epoch.- Parameters:
producerId- the producer id the transaction coordinator mintedproducerEpoch- the producer epoch the transaction coordinator minted- Returns:
- the fencing token of the pair
- Throws:
CoordinationException- if either value is negative
-
parse
Parses theproducer_id:producer_epochform thattoString()writes.- Parameters:
text- the two decimal numbers, separated by one colon- Returns:
- the fencing token the text names
- Throws:
CoordinationException- if the text does not carry that form, or if either number is negative or out of range
-
producerId
public long producerId()Returns the producer id the transaction coordinator minted.- Returns:
- the producer id, or
-1forNO_EPOCH
-
producerEpoch
public short producerEpoch()Returns the producer epoch the transaction coordinator minted.- Returns:
- the producer epoch, or
-1forNO_EPOCH
-
minted
public boolean minted()Reports whether this token names a minted leadership.- Returns:
- true for every token but
NO_EPOCH
-
compareTo
Orders two tokens lexicographically.The comparison reads the producer id first, and it reads the producer epoch only for two tokens of one producer id. Kafka allocates producer ids from a monotonic block allocator and resets the epoch to zero at every new id, so the pair stays monotonic where the epoch alone does not.
- Specified by:
compareToin interfaceComparable<FencingToken>- Parameters:
other- the token to compare against- Returns:
- a negative number when this token ranks below the other one, zero for two equal tokens, and a positive number when this token supersedes the other one
-
supersedes
Reports whether this token ranks above another one.- Parameters:
other- the token to compare against- Returns:
- true when this token supersedes the other one
-
equals
-
hashCode
-
toString
-