-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-13888: Addition of Information in DescribeQuorumResponse #12508
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
Merged
hachikuji
merged 17 commits into
apache:trunk
from
niket-goel:kafka-13888-describe-quorum-fields
Aug 19, 2022
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c5b4323
KAFKA-13888: Addition of Information in DescribeQuorumResponse
niket-goel 19af2c2
Merge branch 'trunk' into kafka-13888-describe-quorum-fields
niket-goel ddbfbea
Addressing Review Comments
niket-goel 0d70f91
More Refactoring based on review comments
niket-goel 73e034e
Styling Changes and some test improvement based on review
niket-goel 5de54cd
Removing unused variable from test method
niket-goel 9322538
Removing an unused argument from updateEndOffset
niket-goel 36f4f75
more refactoring
niket-goel 8630b3f
Merge branch 'trunk' into kafka-13888-describe-quorum-fields
niket-goel b1c2b1e
Removing check for Kafka-13940 in test as it has been fixed
niket-goel 9a55624
Fixing build failure and removing unnecessary catch
niket-goel 8a105d2
Fixing failure in DescribeQuorumRequestTest
niket-goel 0d79978
Merge branch 'trunk' into kafka-13888-describe-quorum-fields
niket-goel b00e66c
Fixing breaking test in KafkaRaftClientTest
niket-goel a12013c
Removing falky integration test
niket-goel 02dc7ad
Revert "Removing falky integration test"
niket-goel d1936ab
Return NOT_CONTROLLER if forwarded DescribeQuorum has NOT_LEADER error
niket-goel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| */ | ||
| package org.apache.kafka.raft; | ||
|
|
||
| import org.apache.kafka.common.message.DescribeQuorumResponseData; | ||
| import org.apache.kafka.common.utils.LogContext; | ||
| import org.apache.kafka.raft.internals.BatchAccumulator; | ||
| import org.slf4j.Logger; | ||
|
|
@@ -25,6 +26,7 @@ | |
| import org.apache.kafka.common.record.ControlRecordUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
|
|
@@ -205,10 +207,10 @@ private boolean updateHighWatermark() { | |
| /** | ||
| * Update the local replica state. | ||
| * | ||
| * See {@link #updateReplicaState(int, long, LogOffsetMetadata)} | ||
| * See {@link #updateReplicaState(int, long, LogOffsetMetadata, long)} | ||
| */ | ||
| public boolean updateLocalState(long fetchTimestamp, LogOffsetMetadata logOffsetMetadata) { | ||
| return updateReplicaState(localId, fetchTimestamp, logOffsetMetadata); | ||
| return updateReplicaState(localId, fetchTimestamp, logOffsetMetadata, logOffsetMetadata.offset); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -217,19 +219,34 @@ public boolean updateLocalState(long fetchTimestamp, LogOffsetMetadata logOffset | |
| * @param replicaId replica id | ||
| * @param fetchTimestamp fetch timestamp | ||
| * @param logOffsetMetadata new log offset and metadata | ||
| * @param leaderLogEndOffset current log end offset of the leader | ||
| * @return true if the high watermark is updated too | ||
| */ | ||
| public boolean updateReplicaState(int replicaId, | ||
| long fetchTimestamp, | ||
| LogOffsetMetadata logOffsetMetadata) { | ||
| public boolean updateReplicaState( | ||
| int replicaId, | ||
| long fetchTimestamp, | ||
| LogOffsetMetadata logOffsetMetadata, | ||
| long leaderLogEndOffset | ||
| ) { | ||
| // Ignore fetches from negative replica id, as it indicates | ||
| // the fetch is from non-replica. For example, a consumer. | ||
| if (replicaId < 0) { | ||
| return false; | ||
| } | ||
|
|
||
| ReplicaState state = getReplicaState(replicaId); | ||
| state.updateFetchTimestamp(fetchTimestamp); | ||
|
|
||
| // Only proceed with updating the states if the offset update is valid | ||
| verifyEndOffsetUpdate(state, logOffsetMetadata); | ||
|
|
||
| // Update the Last CaughtUp Time | ||
| if (logOffsetMetadata.offset >= leaderLogEndOffset) { | ||
| state.updateLastCaughtUpTimestamp(fetchTimestamp); | ||
|
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. We do these updates before the call to |
||
| } else if (logOffsetMetadata.offset >= state.lastFetchLeaderLogEndOffset.orElse(-1L)) { | ||
| state.updateLastCaughtUpTimestamp(state.lastFetchTimestamp.orElse(-1L)); | ||
| } | ||
|
|
||
| state.updateFetchTimestamp(fetchTimestamp, leaderLogEndOffset); | ||
| return updateEndOffset(state, logOffsetMetadata); | ||
| } | ||
|
|
||
|
|
@@ -246,8 +263,10 @@ private List<ReplicaState> followersByDescendingFetchOffset() { | |
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private boolean updateEndOffset(ReplicaState state, | ||
| LogOffsetMetadata endOffsetMetadata) { | ||
| private void verifyEndOffsetUpdate( | ||
| ReplicaState state, | ||
| LogOffsetMetadata endOffsetMetadata | ||
| ) { | ||
| state.endOffset.ifPresent(currentEndOffset -> { | ||
| if (currentEndOffset.offset > endOffsetMetadata.offset) { | ||
| if (state.nodeId == localId) { | ||
|
|
@@ -259,7 +278,11 @@ private boolean updateEndOffset(ReplicaState state, | |
| } | ||
| } | ||
| }); | ||
|
|
||
| } | ||
| private boolean updateEndOffset( | ||
| ReplicaState state, | ||
| LogOffsetMetadata endOffsetMetadata | ||
| ) { | ||
| state.endOffset = Optional.of(endOffsetMetadata); | ||
| state.hasAcknowledgedLeader = true; | ||
| return isVoter(state.nodeId) && updateHighWatermark(); | ||
|
|
@@ -290,22 +313,36 @@ private ReplicaState getReplicaState(int remoteNodeId) { | |
| return state; | ||
| } | ||
|
|
||
| Map<Integer, Long> getVoterEndOffsets() { | ||
| return getReplicaEndOffsets(voterStates); | ||
| List<DescribeQuorumResponseData.ReplicaState> quorumResponseVoterStates(long currentTimeMs) { | ||
| return quorumResponseReplicaStates(voterStates.values(), localId, currentTimeMs); | ||
| } | ||
|
|
||
| Map<Integer, Long> getObserverStates(final long currentTimeMs) { | ||
| List<DescribeQuorumResponseData.ReplicaState> quorumResponseObserverStates(long currentTimeMs) { | ||
| clearInactiveObservers(currentTimeMs); | ||
| return getReplicaEndOffsets(observerStates); | ||
| return quorumResponseReplicaStates(observerStates.values(), localId, currentTimeMs); | ||
| } | ||
|
|
||
| private static <R extends ReplicaState> Map<Integer, Long> getReplicaEndOffsets( | ||
| Map<Integer, R> replicaStates) { | ||
| return replicaStates.entrySet().stream() | ||
| .collect(Collectors.toMap(Map.Entry::getKey, | ||
| e -> e.getValue().endOffset.map( | ||
| logOffsetMetadata -> logOffsetMetadata.offset).orElse(-1L)) | ||
| ); | ||
| private static List<DescribeQuorumResponseData.ReplicaState> quorumResponseReplicaStates( | ||
| Collection<ReplicaState> state, | ||
| int leaderId, | ||
| long currentTimeMs | ||
| ) { | ||
| return state.stream().map(s -> { | ||
| final long lastCaughtUpTimestamp; | ||
| final long lastFetchTimestamp; | ||
| if (s.nodeId == leaderId) { | ||
| lastCaughtUpTimestamp = currentTimeMs; | ||
| lastFetchTimestamp = currentTimeMs; | ||
| } else { | ||
| lastCaughtUpTimestamp = s.lastCaughtUpTimestamp.orElse(-1); | ||
| lastFetchTimestamp = s.lastFetchTimestamp.orElse(-1); | ||
| } | ||
| return new DescribeQuorumResponseData.ReplicaState() | ||
| .setReplicaId(s.nodeId) | ||
| .setLogEndOffset(s.endOffset.map(md -> md.offset).orElse(-1L)) | ||
| .setLastCaughtUpTimestamp(lastCaughtUpTimestamp) | ||
| .setLastFetchTimestamp(lastFetchTimestamp); | ||
| }).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private void clearInactiveObservers(final long currentTimeMs) { | ||
|
|
@@ -323,19 +360,30 @@ private static class ReplicaState implements Comparable<ReplicaState> { | |
| final int nodeId; | ||
| Optional<LogOffsetMetadata> endOffset; | ||
| OptionalLong lastFetchTimestamp; | ||
| OptionalLong lastFetchLeaderLogEndOffset; | ||
| OptionalLong lastCaughtUpTimestamp; | ||
| boolean hasAcknowledgedLeader; | ||
|
|
||
| public ReplicaState(int nodeId, boolean hasAcknowledgedLeader) { | ||
| this.nodeId = nodeId; | ||
| this.endOffset = Optional.empty(); | ||
| this.lastFetchTimestamp = OptionalLong.empty(); | ||
| this.lastFetchLeaderLogEndOffset = OptionalLong.empty(); | ||
| this.lastCaughtUpTimestamp = OptionalLong.empty(); | ||
| this.hasAcknowledgedLeader = hasAcknowledgedLeader; | ||
| } | ||
|
|
||
| void updateFetchTimestamp(long currentFetchTimeMs) { | ||
| void updateFetchTimestamp(long currentFetchTimeMs, long leaderLogEndOffset) { | ||
| // To be resilient to system time shifts we do not strictly | ||
| // require the timestamp be monotonically increasing. | ||
| lastFetchTimestamp = OptionalLong.of(Math.max(lastFetchTimestamp.orElse(-1L), currentFetchTimeMs)); | ||
| lastFetchLeaderLogEndOffset = OptionalLong.of(leaderLogEndOffset); | ||
| } | ||
|
|
||
| void updateLastCaughtUpTimestamp(long lastCaughtUpTime) { | ||
| // This value relies on the fetch timestamp which does not | ||
| // require monotonicity | ||
| lastCaughtUpTimestamp = OptionalLong.of(Math.max(lastCaughtUpTimestamp.orElse(-1L), lastCaughtUpTime)); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -353,10 +401,12 @@ else if (!that.endOffset.isPresent()) | |
| @Override | ||
| public String toString() { | ||
| return String.format( | ||
| "ReplicaState(nodeId=%d, endOffset=%s, lastFetchTimestamp=%s, hasAcknowledgedLeader=%s)", | ||
| "ReplicaState(nodeId=%d, endOffset=%s, lastFetchTimestamp=%s, " + | ||
| "lastCaughtUpTimestamp=%s, hasAcknowledgedLeader=%s)", | ||
| nodeId, | ||
| endOffset, | ||
| lastFetchTimestamp, | ||
| lastCaughtUpTimestamp, | ||
| hasAcknowledgedLeader | ||
| ); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: if we're just re-throwing, we don't need to catch