adixitconfluent commented on code in PR #20815:
URL: https://github.com/apache/kafka/pull/20815#discussion_r2486740815
##########
core/src/test/java/kafka/server/share/SharePartitionTest.java:
##########
@@ -9893,6 +9895,98 @@ public void testRecordArchivedWithWriteStateRPCFailure()
throws InterruptedExcep
assertEquals(5, sharePartition.inFlightTerminalRecords());
}
+ @Test
+ public void testAckTypeToRecordStateMapping() {
+ // This test will help catch bugs if the map changes.
+ Map<Byte, RecordState> actualMap =
SharePartition.ackTypeToRecordStateMapping();
+ assertEquals(4, actualMap.size());
+
+ Map<Byte, RecordState> expected = Map.of(
+ (byte) 0, RecordState.ARCHIVED,
+ AcknowledgeType.ACCEPT.id, RecordState.ACKNOWLEDGED,
+ AcknowledgeType.RELEASE.id, RecordState.AVAILABLE,
+ AcknowledgeType.REJECT.id, RecordState.ARCHIVED
+ );
+
+ for (byte key : expected.keySet()) {
+ assertEquals(expected.get(key), actualMap.get(key));
+ }
+ }
+
+ @Test
+ public void testFetchAckTypeMapForBatch() {
+ ShareAcknowledgementBatch batch =
mock(ShareAcknowledgementBatch.class);
+ when(batch.acknowledgeTypes()).thenReturn(List.of((byte) -1));
+ assertThrows(IllegalArgumentException.class, () ->
SharePartition.fetchAckTypeMapForBatch(batch));
+ }
+
+ @Test
+ public void testRenewAcknowledgeWithCompleteBatchAck() throws
InterruptedException {
+ Persister persister = Mockito.mock(Persister.class);
+ SharePartition sharePartition = SharePartitionBuilder.builder()
+ .withState(SharePartitionState.ACTIVE)
+ .withDefaultAcquisitionLockTimeoutMs(ACQUISITION_LOCK_TIMEOUT_MS)
+ .withMaxDeliveryCount(2)
+ .withPersister(persister)
+ .build();
+
+ List<AcquiredRecords> records = fetchAcquiredRecords(sharePartition,
memoryRecords(0, 1), 1);
+ assertEquals(1, records.size());
+ assertEquals(records.get(0).firstOffset(),
records.get(0).lastOffset());
+ assertEquals(1, sharePartition.cachedState().size());
+ InFlightBatch batch = sharePartition.cachedState().get(0L);
+ AcquisitionLockTimerTask taskOrig =
batch.batchAcquisitionLockTimeoutTask();
+
+ sharePartition.acknowledge(MEMBER_ID, List.of(new
ShareAcknowledgementBatch(0, 0, List.of(AcknowledgeType.RENEW.id))));
+ assertTrue(taskOrig.isCancelled()); // Original acq lock cancelled.
+ assertNotEquals(taskOrig, batch.batchAcquisitionLockTimeoutTask()); //
Lock changes.
+ Mockito.verify(persister, Mockito.times(0)).writeState(Mockito.any());
// No persister call.
+ }
+
+ @Test
+ public void testRenewAcknowledgeWithPerOffsetAck() throws
InterruptedException {
+ Persister persister = Mockito.mock(Persister.class);
+ SharePartition sharePartition = SharePartitionBuilder.builder()
+ .withState(SharePartitionState.ACTIVE)
+ .withDefaultAcquisitionLockTimeoutMs(ACQUISITION_LOCK_TIMEOUT_MS)
+ .withMaxDeliveryCount(2)
+ .withPersister(persister)
+ .build();
+
+ List<AcquiredRecords> records = fetchAcquiredRecords(sharePartition,
memoryRecords(0, 2), 2);
+ assertEquals(1, records.size());
+ assertEquals(records.get(0).firstOffset() + 1,
records.get(0).lastOffset());
+ assertEquals(1, sharePartition.cachedState().size());
+ InFlightBatch batch = sharePartition.cachedState().get(0L);
+ assertEquals(RecordState.ACQUIRED, batch.batchState());
+ AcquisitionLockTimerTask taskOrig =
batch.batchAcquisitionLockTimeoutTask();
+
+ // For ACCEPT ack call.
+ WriteShareGroupStateResult writeShareGroupStateResult =
Mockito.mock(WriteShareGroupStateResult.class);
+
Mockito.when(writeShareGroupStateResult.topicsData()).thenReturn(List.of(
+ new TopicData<>(TOPIC_ID_PARTITION.topicId(), List.of(
+ PartitionFactory.newPartitionErrorData(0, Errors.NONE.code(),
Errors.NONE.message())))));
+
+
when(persister.writeState(Mockito.any())).thenReturn(CompletableFuture.completedFuture(writeShareGroupStateResult));
+
+ sharePartition.acknowledge(MEMBER_ID, List.of(new
ShareAcknowledgementBatch(0, 1,
+ List.of(AcknowledgeType.RENEW.id, AcknowledgeType.ACCEPT.id))));
+
+ assertTrue(taskOrig.isCancelled()); // Original acq lock cancelled.
+ assertNotEquals(taskOrig,
sharePartition.cachedState().get(0L).offsetState().get(0L).acquisitionLockTimeoutTask());
+ TestUtils.waitForCondition(() ->
sharePartition.cachedState().get(0L).offsetState() != null, "offset state not
populated");
Review Comment:
I don't think we need waitForCondition in this scenario, just
`assertNotNull(sharePartition.cachedState().get(0L).offsetState())` should be
enough
--
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]