Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/src/main/java/kafka/server/share/SharePartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ public CompletableFuture<Void> maybeInitialize() {
throwable = new IllegalStateException(String.format("Failed to initialize the share partition %s-%s", groupId, topicIdPartition));
return;
}

if (stateBatch.lastOffset() < stateBatch.firstOffset()) {
log.error("Invalid state batch found for the share partition: {}-{}. The first offset: {}"
+ " is less than the last offset of the batch: {}.", groupId, topicIdPartition,
stateBatch.firstOffset(), stateBatch.lastOffset());
throwable = new IllegalStateException(String.format("Failed to initialize the share partition %s-%s", groupId, topicIdPartition));
return;
}

if (gapStartOffset == -1 && stateBatch.firstOffset() > previousBatchLastOffset + 1) {
gapStartOffset = previousBatchLastOffset + 1;
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/java/kafka/server/share/SharePartitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,26 @@ public void testMaybeInitializeAndAcquireWithMultipleBatchesPriorStartOffset() {
assertEquals(3, sharePartition.deliveryCompleteCount());
}

@Test
public void testMaybeInitializeWithInvalidOffsetInBatch() {
Persister persister = Mockito.mock(Persister.class);
ReadShareGroupStateResult readShareGroupStateResult = Mockito.mock(ReadShareGroupStateResult.class);
Mockito.when(readShareGroupStateResult.topicsData()).thenReturn(List.of(
new TopicData<>(TOPIC_ID_PARTITION.topicId(), List.of(
PartitionFactory.newPartitionAllData(0, 3, 5L, Errors.NONE.code(), Errors.NONE.message(),
List.of(
new PersisterStateBatch(5L, 10L, RecordState.AVAILABLE.id, (short) 2),
new PersisterStateBatch(11L, 10L, RecordState.ARCHIVED.id, (short) 3)))))));
Mockito.when(persister.readState(Mockito.any())).thenReturn(CompletableFuture.completedFuture(readShareGroupStateResult));
SharePartition sharePartition = SharePartitionBuilder.builder().withPersister(persister).build();

CompletableFuture<Void> result = sharePartition.maybeInitialize();
assertTrue(result.isDone());
assertTrue(result.isCompletedExceptionally());
assertFutureThrows(IllegalStateException.class, result);
assertEquals(SharePartitionState.FAILED, sharePartition.partitionState());
}

@Test
public void testAcquireSingleRecord() throws InterruptedException {
SharePartition sharePartition = SharePartitionBuilder.builder()
Expand Down