-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-19567: Added the check for underlying partition being the leader in delayedShareFetch tryComplete method #20280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a3a2f79
594d79a
fcd4a79
4d6c123
3b0faae
b6716e8
de22d8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,13 +153,24 @@ public void testDelayedShareFetchTryCompleteReturnsFalseDueToNonAcquirablePartit | |
| when(sp0.canAcquireRecords()).thenReturn(false); | ||
| when(sp1.canAcquireRecords()).thenReturn(false); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| ReplicaManager replicaManager = mock(ReplicaManager.class); | ||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
|
|
||
| ShareGroupMetrics shareGroupMetrics = new ShareGroupMetrics(new MockTime()); | ||
| Uuid fetchId = Uuid.randomUuid(); | ||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
| .withShareGroupMetrics(shareGroupMetrics) | ||
| .withFetchId(fetchId) | ||
| .withReplicaManager(replicaManager) | ||
| .build()); | ||
|
|
||
| when(sp0.maybeAcquireFetchLock(fetchId)).thenReturn(true); | ||
|
|
@@ -218,6 +229,15 @@ public void testTryCompleteWhenMinBytesNotSatisfiedOnFirstFetch() { | |
|
|
||
| PartitionMaxBytesStrategy partitionMaxBytesStrategy = mockPartitionMaxBytes(Set.of(tp0)); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
|
|
||
| Time time = mock(Time.class); | ||
| when(time.hiResClockMs()).thenReturn(100L).thenReturn(110L); | ||
| ShareGroupMetrics shareGroupMetrics = new ShareGroupMetrics(time); | ||
|
|
@@ -287,6 +307,15 @@ public void testTryCompleteWhenMinBytesNotSatisfiedOnSubsequentFetch() { | |
| mockTopicIdPartitionFetchBytes(replicaManager, tp0, hwmOffsetMetadata); | ||
| BiConsumer<SharePartitionKey, Throwable> exceptionHandler = mockExceptionHandler(); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
|
|
@@ -580,6 +609,19 @@ public void testForceCompleteTriggersDelayedActionsQueue() { | |
| List<DelayedOperationKey> delayedShareFetchWatchKeys = new ArrayList<>(); | ||
| topicIdPartitions1.forEach(topicIdPartition -> delayedShareFetchWatchKeys.add(new DelayedShareFetchGroupKey(groupId, topicIdPartition.topicId(), topicIdPartition.partition()))); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p2 = mock(Partition.class); | ||
| when(p2.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
| when(replicaManager.getPartitionOrException(tp2.topicPartition())).thenReturn(p2); | ||
|
|
||
| Uuid fetchId1 = Uuid.randomUuid(); | ||
| DelayedShareFetch delayedShareFetch1 = DelayedShareFetchTest.DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch1) | ||
|
|
@@ -737,6 +779,12 @@ public void testExceptionInMinBytesCalculation() { | |
| when(time.hiResClockMs()).thenReturn(100L).thenReturn(110L).thenReturn(170L); | ||
| ShareGroupMetrics shareGroupMetrics = new ShareGroupMetrics(time); | ||
| Uuid fetchId = Uuid.randomUuid(); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
|
|
||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
|
|
@@ -881,10 +929,18 @@ public void testLocksReleasedAcquireException() { | |
| BROKER_TOPIC_STATS); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| ReplicaManager replicaManager = mock(ReplicaManager.class); | ||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
|
|
||
| DelayedShareFetch delayedShareFetch = DelayedShareFetchTest.DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
| .withFetchId(fetchId) | ||
| .withReplicaManager(replicaManager) | ||
| .build(); | ||
|
|
||
| when(sp0.maybeAcquireFetchLock(fetchId)).thenReturn(true); | ||
|
|
@@ -1263,6 +1319,19 @@ public void testRemoteStorageFetchTryCompleteReturnsFalse() { | |
| when(remoteLogManager.asyncRead(any(), any())).thenReturn(mock(Future.class)); | ||
| when(replicaManager.remoteLogManager()).thenReturn(Option.apply(remoteLogManager)); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p2 = mock(Partition.class); | ||
| when(p2.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
| when(replicaManager.getPartitionOrException(tp2.topicPartition())).thenReturn(p2); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
|
|
@@ -1288,6 +1357,70 @@ public void testRemoteStorageFetchTryCompleteReturnsFalse() { | |
| delayedShareFetch.lock().unlock(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoteStorageFetchPartitionLeaderChanged() { | ||
| ReplicaManager replicaManager = mock(ReplicaManager.class); | ||
| TopicIdPartition tp0 = new TopicIdPartition(Uuid.randomUuid(), new TopicPartition("foo", 0)); | ||
|
|
||
| SharePartition sp0 = mock(SharePartition.class); | ||
|
|
||
| when(sp0.canAcquireRecords()).thenReturn(true); | ||
|
|
||
| LinkedHashMap<TopicIdPartition, SharePartition> sharePartitions = new LinkedHashMap<>(); | ||
| sharePartitions.put(tp0, sp0); | ||
|
|
||
| ShareFetch shareFetch = new ShareFetch(FETCH_PARAMS, "grp", Uuid.randomUuid().toString(), | ||
| new CompletableFuture<>(), List.of(tp0), BATCH_SIZE, MAX_FETCH_RECORDS, | ||
| BROKER_TOPIC_STATS); | ||
|
|
||
| when(sp0.nextFetchOffset()).thenReturn(10L); | ||
|
|
||
| // Fetch offset does not match with the cached entry for sp0, hence, a replica manager fetch will happen for sp0. | ||
| when(sp0.fetchOffsetMetadata(anyLong())).thenReturn(Optional.empty()); | ||
|
|
||
| // Mocking remote storage read result for tp0. | ||
| doAnswer(invocation -> buildLocalAndRemoteFetchResult(Set.of(), Set.of(tp0))).when(replicaManager).readFromLog(any(), any(), any(ReplicaQuota.class), anyBoolean()); | ||
|
|
||
| // Remote fetch related mocks. Remote fetch object does not complete within tryComplete in this mock. | ||
| RemoteLogManager remoteLogManager = mock(RemoteLogManager.class); | ||
| when(remoteLogManager.asyncRead(any(), any())).thenReturn(mock(Future.class)); | ||
| when(replicaManager.remoteLogManager()).thenReturn(Option.apply(remoteLogManager)); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(false); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
| .withReplicaManager(replicaManager) | ||
| .withPartitionMaxBytesStrategy(mockPartitionMaxBytes(Set.of(tp0))) | ||
| .withFetchId(fetchId) | ||
| .build()); | ||
|
|
||
| // All the topic partitions are acquirable. | ||
| when(sp0.maybeAcquireFetchLock(fetchId)).thenReturn(true); | ||
|
|
||
| // Mock the behaviour of replica manager such that remote storage fetch completion timer task completes on adding it to the watch queue. | ||
| doAnswer(invocationOnMock -> { | ||
| TimerTask timerTask = invocationOnMock.getArgument(0); | ||
| timerTask.run(); | ||
| return null; | ||
| }).when(replicaManager).addShareFetchTimerRequest(any()); | ||
|
|
||
| assertFalse(delayedShareFetch.isCompleted()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the delayed share fetch complete?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. The DelayedShareFetch should complete, but only after tryComplete is called. I have added an assertTrue for this after tryComplete is called to make the test better. |
||
| assertTrue(delayedShareFetch.tryComplete()); | ||
| assertTrue(delayedShareFetch.isCompleted()); | ||
| // Remote fetch object gets created for delayed share fetch object. | ||
| assertNotNull(delayedShareFetch.pendingRemoteFetches()); | ||
| // Verify the locks are released for local log read topic partitions tp0. | ||
| Mockito.verify(delayedShareFetch, times(1)).releasePartitionLocks(Set.of(tp0)); | ||
| assertTrue(delayedShareFetch.lock().tryLock()); | ||
| delayedShareFetch.lock().unlock(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoteStorageFetchTryCompleteThrowsException() { | ||
| ReplicaManager replicaManager = mock(ReplicaManager.class); | ||
|
|
@@ -1516,6 +1649,16 @@ public void testRemoteStorageFetchRequestCompletionOnFutureCompletionFailure() { | |
| when(replicaManager.remoteLogManager()).thenReturn(Option.apply(remoteLogManager)); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
|
|
||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
|
|
@@ -1586,6 +1729,12 @@ public void testRemoteStorageFetchRequestCompletionOnFutureCompletionSuccessfull | |
| when(replicaManager.remoteLogManager()).thenReturn(Option.apply(remoteLogManager)); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
|
|
||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
|
|
@@ -1679,6 +1828,19 @@ public void testRemoteStorageFetchRequestCompletionAlongWithLocalLogRead() { | |
| }).when(remoteLogManager).asyncRead(any(), any()); | ||
| when(replicaManager.remoteLogManager()).thenReturn(Option.apply(remoteLogManager)); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p2 = mock(Partition.class); | ||
| when(p2.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
| when(replicaManager.getPartitionOrException(tp2.topicPartition())).thenReturn(p2); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
|
|
@@ -1761,6 +1923,16 @@ public void testRemoteStorageFetchHappensForAllTopicPartitions() { | |
| when(replicaManager.remoteLogManager()).thenReturn(Option.apply(remoteLogManager)); | ||
|
|
||
| Uuid fetchId = Uuid.randomUuid(); | ||
|
|
||
| Partition p0 = mock(Partition.class); | ||
| when(p0.isLeader()).thenReturn(true); | ||
|
|
||
| Partition p1 = mock(Partition.class); | ||
| when(p1.isLeader()).thenReturn(true); | ||
|
|
||
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we add a new test case that validates the added functionality?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. I have made the required updates and added a test case for the remote fetch throwing a |
||
|
|
||
| DelayedShareFetch delayedShareFetch = spy(DelayedShareFetchBuilder.builder() | ||
| .withShareFetchData(shareFetch) | ||
| .withSharePartitions(sharePartitions) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need to handle the exception below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. Yep, I have added another catch block here for the new NotLeaderException thrown.