junrao commented on code in PR #23844:
URL: https://github.com/apache/kafka/pull/23844#discussion_r4416215907
##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/ProducerBatch.java:
##########
@@ -419,7 +420,7 @@ private void
assignProducerStateToBatches(Deque<ProducerBatch> batches) {
ProducerIdAndEpoch producerIdAndEpoch = new
ProducerIdAndEpoch(producerId(), producerEpoch());
for (ProducerBatch newBatch : batches) {
newBatch.setProducerState(producerIdAndEpoch, sequence,
isTransactional());
- sequence += newBatch.recordCount;
+ sequence =
DefaultRecordBatch.incrementSequence(sequence, newBatch.recordCount);
Review Comment:
Claude found the following issue.
```
Main issue: the split pieces are queued in the wrong order
I checked this by adding a throwaway test to SenderTest in a temporary
checkout of the PR. It sets an idempotent producer's next sequence to
Integer.MAX_VALUE, sends a 2-record batch, answers MESSAGE_TOO_LARGE, then
keeps answering OUT_OF_ORDER_SEQUENCE_NUMBER:
first send base sequence: 2147483647
first split piece sent has base sequence: 0
after OUT_OF_ORDER_SEQUENCE_NUMBER #1, in flight: 1, next sent base
sequence: 0
after OUT_OF_ORDER_SEQUENCE_NUMBER #2, in flight: 1, next sent base
sequence: 0
after OUT_OF_ORDER_SEQUENCE_NUMBER #3, in flight: 1, next sent base
sequence: 0
The piece with MAX_VALUE is never sent. On a real broker the last sequence
is MAX_VALUE - 1, so sequence 0 is rejected as out of order every time. Here's
why:
Re-enqueue order: splitAndReenqueue puts the pieces back through
insertInSequenceOrder, which compares raw base sequences
(RecordAccumulator.java:670, 680). The piece with sequence 0 sorts ahead of
MAX_VALUE.
In-flight order: in-flight batches are ordered by raw baseSequence
(TxnPartitionEntry.java:65), so firstInFlightSequence is 0.
Drain gate: a batch that already has a sequence is only sent if it matches
firstInFlightSequence (RecordAccumulator.java:971). So only the sequence-0
piece ever goes out.
Retry decision: after OUT_OF_ORDER_SEQUENCE_NUMBER, isNextSequence computes
0 - (MAX_VALUE - 1) != 1, so the batch counts as "not the next batch" and is
retried as is.
This repeats until delivery.timeout.ms. Then both pieces fail with
TimeoutException, and the producer either bumps its epoch (idempotent) or hits
an abortable error (transactional). Without the patch the same thing happens,
just with the piece stuck at Integer.MIN_VALUE instead of 0.
For the fix, I'd order by a sequence that doesn't wrap: store a long next to
each batch's int sequence, and sort the in-flight set and insertInSequenceOrder
by it.
Use a per-partition counter that never wraps. Every producer epoch starts
its sequences at 0, so a long that starts at 0 and only ever grows is exactly
"records assigned so far in this epoch". The int written on the wire is that
long modulo 2^31:
// Wire sequences use [0, Integer.MAX_VALUE] and wrap to 0, which matches
DefaultRecordBatch.incrementSequence
static int toWireSequence(long sequence) {
return (int) (sequence & Integer.MAX_VALUE);
}
Make the long the only thing that's stored and incremented, and always
derive the int from it. Then the two can never disagree.
Where it's generated. These are the four places that set a batch's sequence
today:
Where | Today | With a long
-- | -- | --
Drain (RecordAccumulator.java:1042) | transactionManager.sequenceNumber(tp),
then incrementSequenceNumber | TxnPartitionEntry keeps long nextSequence
(starting at 0) instead of the int, and hands it out the same way
Split (ProducerBatch.java:422) | parent's base plus record counts | parent's
long plus record counts. Plain addition is safe, so no incrementSequence is
needed
Epoch reset (TxnPartitionEntry.java:119) | renumber in-flight batches from 0
| same, as longs, and reset nextSequence to the total
Failed batch (TxnPartitionEntry.java:142-150) | baseSequence() <
baseSequence, then subtract | compare and subtract the longs. The "going
negative" check then means something even after a wrap
In ProducerBatch, keep the long next to the builder's int:
private long sequence = RecordBatch.NO_SEQUENCE; // doesn't wrap, for
ordering only
public void setProducerState(ProducerIdAndEpoch producerIdAndEpoch, long
baseSequence, boolean isTransactional) {
this.sequence = baseSequence;
recordsBuilder.setProducerState(producerIdAndEpoch.producerId,
producerIdAndEpoch.epoch,
toWireSequence(baseSequence), isTransactional);
}
resetProducerState changes the same way.
What then compares the long:
PRODUCER_BATCH_COMPARATOR: .thenComparingLong(ProducerBatch::sequence)
instead of the raw int.
insertInSequenceOrder (RecordAccumulator.java:670, 680).
firstInFlightSequence versus the head batch in
shouldStopDrainBatchesForPartition (:965-971). Compare the longs there, or keep
comparing ints. Once the order is right, both give the same answer.
lastAckedSequence, maybeUpdateLastAckedSequence (TxnPartitionEntry.java:128)
and isNextSequence (TransactionManager.java:913-914). Tracking the last
acknowledged value as a long fixes both wrap problems found earlier.
ProducerBatch.lastSequence() (:588-590) also uses raw int addition. It feeds
maybeUpdateLastAckedSequence, so derive it from the long too.
A long can't overflow in practice: that would take 2^63 records in one epoch.
The resulting order matches the broker's. Its check accepts 0 right after
MAX_VALUE, and the long puts the batch that goes on the wire as 0 directly
after the MAX_VALUE one.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]