[
https://issues.apache.org/jira/browse/KAFKA-21605?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148344#comment-18148344
]
Chia-Ping Tsai commented on KAFKA-21605:
----------------------------------------
{code:java}
/**
* The broker retains the metadata of the last {@link
ProducerStateEntry#NUM_BATCHES_TO_RETAIN} batches per
* producer so that a retry of any of them is answered with the original
offsets. The producer state snapshot
* only persists the last batch though, so after a restart, clean or not, a
retry of the older ones is rejected
* with OUT_OF_ORDER_SEQUENCE_NUMBER even though the records are in the log.
*/
@ParameterizedTest(name = "testRetryOfRetainedBatchAfterRestart with
uncleanWithoutSnapshot: {0}")
@ValueSource(booleans = {false, true})
public void testRetryOfRetainedBatchAfterRestart(boolean
uncleanWithoutSnapshot) throws IOException {
LogConfig logConfig = new LogTestUtils.LogConfigBuilder().build();
UnifiedLog log = createLog(logDir, logConfig);
long pid = 1L;
short epoch = 0;
int numBatches = ProducerStateEntry.NUM_BATCHES_TO_RETAIN;
IntFunction<MemoryRecords> batchWithSequence = seq ->
LogTestUtils.records(
List.of(new SimpleRecord(mockTime.milliseconds(), ("key-" +
seq).getBytes(), ("value-" + seq).getBytes())),
pid, epoch, seq, 0L); List<Long> offsets = new
ArrayList<>();
for (int seq = 0; seq < numBatches; seq++) {
offsets.add(log.appendAsLeader(batchWithSequence.apply(seq),
0).firstOffset());
}
long logEndOffset = log.logEndOffset();
assertEquals(numBatches, logEndOffset); // Before the restart a
retry of any of the retained batches is recognised as a duplicate.
for (int seq = 0; seq < numBatches; seq++) {
assertEquals(offsets.get(seq),
log.appendAsLeader(batchWithSequence.apply(seq), 0).firstOffset());
}
assertEquals(logEndOffset, log.logEndOffset()); // Closing the
log writes a producer state snapshot at the log end offset.
log.close();
UnifiedLog reloaded;
if (uncleanWithoutSnapshot) {
// Unclean restart without any snapshot: the segment is recovered
by scanning the log, but
// LogLoader.recoverSegment() does that with a temporary
ProducerStateManager and writes a snapshot at
// the end of the segment, which the real ProducerStateManager then
loads. So this goes through a
// snapshot as well.
LogTestUtils.deleteProducerSnapshotFiles(logDir);
reloaded = createLog(logDir, logConfig, 0L, 0L, brokerTopicStats,
mockTime.scheduler,
mockTime, producerStateManagerConfig, false,
Optional.empty(), false);
} else {
// Clean restart: the producer state comes from the snapshot alone.
reloaded = createLog(logDir, logConfig, 0L, logEndOffset,
brokerTopicStats, mockTime.scheduler,
mockTime, producerStateManagerConfig, true,
Optional.empty(), false);
}
assertEquals(logEndOffset, reloaded.logEndOffset()); // The same
retries after the restart.
List<String> outcomes = new ArrayList<>();
for (int seq = 0; seq < numBatches; seq++) {
try {
long offset =
reloaded.appendAsLeader(batchWithSequence.apply(seq), 0).firstOffset();
outcomes.add("seq " + seq + " -> duplicate of offset " +
offset);
} catch (OutOfOrderSequenceException e) {
outcomes.add("seq " + seq + " -> OUT_OF_ORDER_SEQUENCE_NUMBER");
}
}
System.out.println("uncleanWithoutSnapshot=" + uncleanWithoutSnapshot +
": " + outcomes);
assertEquals(logEndOffset, reloaded.logEndOffset(), "A retry must never
be appended again"); List<String> allRecognised = new ArrayList<>();
for (int seq = 0; seq < numBatches; seq++) {
allRecognised.add("seq " + seq + " -> duplicate of offset " +
offsets.get(seq));
}
assertEquals(allRecognised, outcomes);
}
{code}
> Producer state snapshot should recover 5 states
> ------------------------------------------------
>
> Key: KAFKA-21605
> URL: https://issues.apache.org/jira/browse/KAFKA-21605
> Project: Kafka
> Issue Type: Bug
> Reporter: Chia-Ping Tsai
> Assignee: Chia-Ping Tsai
> Priority: Minor
>
>
> {code:java}
> public static void writeSnapshot(File file, Map<Long, ProducerStateEntry>
> entries, boolean sync) throws IOException {
> ProducerSnapshot producerSnapshot = new ProducerSnapshot();
> List<ProducerSnapshot.ProducerEntry> producerEntries = new
> ArrayList<>(entries.size());
> for (Map.Entry<Long, ProducerStateEntry> producerIdEntry :
> entries.entrySet()) {
> Long producerId = producerIdEntry.getKey();
> ProducerStateEntry entry = producerIdEntry.getValue();
> ProducerSnapshot.ProducerEntry producerEntry = new
> ProducerSnapshot.ProducerEntry()
> .setProducerId(producerId)
> .setEpoch(entry.producerEpoch())
> .setLastSequence(entry.lastSeq())
> .setLastOffset(entry.lastDataOffset())
> .setOffsetDelta(entry.lastOffsetDelta())
> .setTimestamp(entry.lastTimestamp())
> .setCoordinatorEpoch(entry.coordinatorEpoch())
>
> .setCurrentTxnFirstOffset(entry.currentTxnFirstOffset().orElse(-1L));
> producerEntries.add(producerEntry);
> }
> {code}
> The snapshot only contains the last batch, but the idempotent mechanism
> expects the server to remember the last five. Hence, a server will lose the
> other four once it restarts, and the retry requests will get an out of order
> sequence error.
>
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)