use of org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET in project kafka by apache.
the class FetcherTest method testOffsetValidationWithGivenEpochOffset.
private void testOffsetValidationWithGivenEpochOffset(int leaderEpoch, long endOffset, OffsetResetStrategy offsetResetStrategy) {
buildFetcher(offsetResetStrategy);
assignFromUser(singleton(tp0));
Map<String, Integer> partitionCounts = new HashMap<>();
partitionCounts.put(tp0.topic(), 4);
final int epochOne = 1;
final long initialOffset = 5;
metadata.updateWithCurrentRequestVersion(RequestTestUtils.metadataUpdateWithIds("dummy", 1, Collections.emptyMap(), partitionCounts, tp -> epochOne, topicIds), false, 0L);
// Offset validation requires OffsetForLeaderEpoch request v3 or higher
Node node = metadata.fetch().nodes().get(0);
apiVersions.update(node.idString(), NodeApiVersions.create());
Metadata.LeaderAndEpoch leaderAndEpoch = new Metadata.LeaderAndEpoch(metadata.currentLeader(tp0).leader, Optional.of(epochOne));
subscriptions.seekUnvalidated(tp0, new SubscriptionState.FetchPosition(initialOffset, Optional.of(epochOne), leaderAndEpoch));
fetcher.validateOffsetsIfNeeded();
consumerClient.poll(time.timer(Duration.ZERO));
assertTrue(subscriptions.awaitingValidation(tp0));
assertTrue(client.hasInFlightRequests());
client.respond(offsetsForLeaderEpochRequestMatcher(tp0, epochOne, epochOne), prepareOffsetsForLeaderEpochResponse(tp0, Errors.NONE, leaderEpoch, endOffset));
consumerClient.poll(time.timer(Duration.ZERO));
if (offsetResetStrategy == OffsetResetStrategy.NONE) {
LogTruncationException thrown = assertThrows(LogTruncationException.class, () -> fetcher.validateOffsetsIfNeeded());
assertEquals(singletonMap(tp0, initialOffset), thrown.offsetOutOfRangePartitions());
if (endOffset == UNDEFINED_EPOCH_OFFSET || leaderEpoch == UNDEFINED_EPOCH) {
assertEquals(Collections.emptyMap(), thrown.divergentOffsets());
} else {
OffsetAndMetadata expectedDivergentOffset = new OffsetAndMetadata(endOffset, Optional.of(leaderEpoch), "");
assertEquals(singletonMap(tp0, expectedDivergentOffset), thrown.divergentOffsets());
}
assertTrue(subscriptions.awaitingValidation(tp0));
} else {
fetcher.validateOffsetsIfNeeded();
assertFalse(subscriptions.awaitingValidation(tp0));
}
}
Aggregations