-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-5758: Don't fail fetch request if replica is no longer a follower for a partition #3954
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
Closed
ijuma
wants to merge
9
commits into
apache:trunk
from
ijuma:kafka-5758-dont-fail-fetch-request-if-replica-is-not-follower
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
09ece7e
KAFKA-5758: Don't fail fetch request if replica is no longer a follow…
ijuma 5361233
Centralise some of the offline partition handling
ijuma 4eb1774
Remove unnecessary tryCompleteDelayedProduce
ijuma a533a7d
Add test
ijuma 011cdf7
Return updated hw in the immediate return case
ijuma b2e52fe
Fix test failures in `GroupMetadataManagerTest`
ijuma 46c9397
Tweak `ReplicaManager.fetchMessages` to make it clearer and remove re…
ijuma 45f52b3
Tweak `lowWatermarkIfLeader` to be a bit more direct.
ijuma babd8a0
Return empty fetch response if follower is not one of the assigned re…
ijuma 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock | |
| import com.yammer.metrics.core.Gauge | ||
| import kafka.admin.AdminUtils | ||
| import kafka.api.LeaderAndIsr | ||
| import kafka.common.NotAssignedReplicaException | ||
| import kafka.controller.KafkaController | ||
| import kafka.log.LogConfig | ||
| import kafka.log.{LogAppendInfo, LogConfig} | ||
| import kafka.metrics.KafkaMetricsGroup | ||
| import kafka.server._ | ||
| import kafka.utils.CoreUtils.{inReadLock, inWriteLock} | ||
|
|
@@ -264,37 +263,31 @@ class Partition(val topic: String, | |
| } | ||
|
|
||
| /** | ||
| * Update the log end offset of a certain replica of this partition | ||
| * Update the the follower's state in the leader based on the last fetch request. See | ||
| * [[kafka.cluster.Replica#updateLogReadResult]] for details. | ||
| * | ||
| * @return true if the leader's log start offset or high watermark have been updated | ||
| */ | ||
| def updateReplicaLogReadResult(replicaId: Int, logReadResult: LogReadResult) { | ||
| getReplica(replicaId) match { | ||
| case Some(replica) => | ||
| // No need to calculate low watermark if there is no delayed DeleteRecordsRequest | ||
| val oldLeaderLW = if (replicaManager.delayedDeleteRecordsPurgatory.delayed > 0) lowWatermarkIfLeader else -1L | ||
| replica.updateLogReadResult(logReadResult) | ||
| val newLeaderLW = if (replicaManager.delayedDeleteRecordsPurgatory.delayed > 0) lowWatermarkIfLeader else -1L | ||
| // check if the LW of the partition has incremented | ||
| // since the replica's logStartOffset may have incremented | ||
| val leaderLWIncremented = newLeaderLW > oldLeaderLW | ||
| // check if we need to expand ISR to include this replica | ||
| // if it is not in the ISR yet | ||
| val leaderHWIncremented = maybeExpandIsr(replicaId, logReadResult) | ||
|
|
||
| // some delayed operations may be unblocked after HW or LW changed | ||
| if (leaderLWIncremented || leaderHWIncremented) | ||
| tryCompleteDelayedRequests() | ||
|
|
||
| debug("Recorded replica %d log end offset (LEO) position %d." | ||
| .format(replicaId, logReadResult.info.fetchOffsetMetadata.messageOffset)) | ||
| case None => | ||
| throw new NotAssignedReplicaException(("Leader %d failed to record follower %d's position %d since the replica" + | ||
| " is not recognized to be one of the assigned replicas %s for partition %s.") | ||
| .format(localBrokerId, | ||
| replicaId, | ||
| logReadResult.info.fetchOffsetMetadata.messageOffset, | ||
| assignedReplicas.map(_.brokerId).mkString(","), | ||
| topicPartition)) | ||
| } | ||
| def updateReplicaLogReadResult(replica: Replica, logReadResult: LogReadResult): Boolean = { | ||
| val replicaId = replica.brokerId | ||
| // No need to calculate low watermark if there is no delayed DeleteRecordsRequest | ||
| val oldLeaderLW = if (replicaManager.delayedDeleteRecordsPurgatory.delayed > 0) lowWatermarkIfLeader else -1L | ||
| replica.updateLogReadResult(logReadResult) | ||
| val newLeaderLW = if (replicaManager.delayedDeleteRecordsPurgatory.delayed > 0) lowWatermarkIfLeader else -1L | ||
| // check if the LW of the partition has incremented | ||
| // since the replica's logStartOffset may have incremented | ||
| val leaderLWIncremented = newLeaderLW > oldLeaderLW | ||
| // check if we need to expand ISR to include this replica | ||
| // if it is not in the ISR yet | ||
| val leaderHWIncremented = maybeExpandIsr(replicaId, logReadResult) | ||
|
|
||
| val result = leaderLWIncremented || leaderHWIncremented | ||
| // some delayed operations may be unblocked after HW or LW changed | ||
| if (result) | ||
| tryCompleteDelayedRequests() | ||
|
|
||
| debug(s"Recorded replica $replicaId log end offset (LEO) position ${logReadResult.info.fetchOffsetMetadata.messageOffset}.") | ||
| result | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -305,7 +298,9 @@ class Partition(val topic: String, | |
| * even if its log end offset is >= HW. However, to be consistent with how the follower determines | ||
| * whether a replica is in-sync, we only check HW. | ||
| * | ||
| * This function can be triggered when a replica's LEO has incremented | ||
| * This function can be triggered when a replica's LEO has incremented. | ||
| * | ||
| * @return true if the high watermark has been updated | ||
| */ | ||
| def maybeExpandIsr(replicaId: Int, logReadResult: LogReadResult): Boolean = { | ||
| inWriteLock(leaderIsrUpdateLock) { | ||
|
|
@@ -314,7 +309,7 @@ class Partition(val topic: String, | |
| case Some(leaderReplica) => | ||
| val replica = getReplica(replicaId).get | ||
| val leaderHW = leaderReplica.highWatermark | ||
| if(!inSyncReplicas.contains(replica) && | ||
| if (!inSyncReplicas.contains(replica) && | ||
| assignedReplicas.map(_.brokerId).contains(replicaId) && | ||
| replica.logEndOffset.offsetDiff(leaderHW) >= 0) { | ||
| val newInSyncReplicas = inSyncReplicas + replica | ||
|
|
@@ -421,8 +416,10 @@ class Partition(val topic: String, | |
| def lowWatermarkIfLeader: Long = { | ||
| if (!isLeaderReplicaLocal) | ||
| throw new NotLeaderForPartitionException("Leader not local for partition %s on broker %d".format(topicPartition, localBrokerId)) | ||
| assignedReplicas.filter(replica => | ||
| replicaManager.metadataCache.isBrokerAlive(replica.brokerId)).map(_.logStartOffset).reduceOption(_ min _).getOrElse(0L) | ||
| val logStartOffsets = assignedReplicas.collect { | ||
| case replica if replicaManager.metadataCache.isBrokerAlive(replica.brokerId) => replica.logStartOffset | ||
| } | ||
| CoreUtils.min(logStartOffsets, 0L) | ||
|
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. Wouldn't this return 0 in most cases ?
Member
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. No, it only returns |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -485,7 +482,7 @@ class Partition(val topic: String, | |
| laggingReplicas | ||
| } | ||
|
|
||
| def appendRecordsToLeader(records: MemoryRecords, isFromClient: Boolean, requiredAcks: Int = 0) = { | ||
| def appendRecordsToLeader(records: MemoryRecords, isFromClient: Boolean, requiredAcks: Int = 0): LogAppendInfo = { | ||
| val (info, leaderHWIncremented) = inReadLock(leaderIsrUpdateLock) { | ||
| leaderReplicaIfLocal match { | ||
| case Some(leaderReplica) => | ||
|
|
||
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
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.
This seems to be an existing problem, but it seems that we only need to call tryCompleteDelayedRequests() when the HW increments.
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.
I think
tryCompleteDelayedDeleteRecordsis affected byleaderLWIncremented.We could make the
tryCompletecalls more specific depending on what changed, if you think that's worth it.