-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-15586: Clean shutdown detection - server side #14706
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
mumrah
merged 24 commits into
apache:trunk
from
CalvinLiu7947:ELR-ak-unclean-shutdown-server-side
Apr 4, 2024
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
cf035f5
Unclean shutdown - Server side
CalvinLiu7947 38aa224
Minor
CalvinLiu7947 2a00681
Address comments
CalvinLiu7947 9994d45
Minor
CalvinLiu7947 525a74c
Address comments
CalvinLiu7947 f5ac59d
Minor
CalvinLiu7947 9242fa9
Minor
CalvinLiu7947 4484ffd
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 b955bda
Minor
CalvinLiu7947 178712f
Empty commit
CalvinLiu7947 01352be
Empty commit
CalvinLiu7947 ffa75a9
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 fd8842e
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 350ecd4
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 02958b1
Address comments
CalvinLiu7947 3eb7696
Fix UTs
CalvinLiu7947 58eeb7d
Address comments
CalvinLiu7947 fe8e116
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 5f850a3
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 e406e0f
Address comments
CalvinLiu7947 c0eedce
Minor
CalvinLiu7947 d030708
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 18dd798
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 cb7e41a
Merge branch 'trunk' into ELR-ak-unclean-shutdown-server-side
CalvinLiu7947 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
162 changes: 162 additions & 0 deletions
162
metadata/src/main/java/org/apache/kafka/controller/BrokersToElrs.java
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 |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.kafka.controller; | ||
|
|
||
| import org.apache.kafka.common.Uuid; | ||
| import org.apache.kafka.metadata.Replicas; | ||
| import org.apache.kafka.timeline.SnapshotRegistry; | ||
| import org.apache.kafka.timeline.TimelineHashMap; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| import static org.apache.kafka.metadata.Replicas.NONE; | ||
|
|
||
| public class BrokersToElrs { | ||
| private final SnapshotRegistry snapshotRegistry; | ||
|
|
||
| // It maps from the broker id to the topic id partitions if the partition has ELR. | ||
| private final TimelineHashMap<Integer, TimelineHashMap<Uuid, int[]>> elrMembers; | ||
|
CalvinLiu7947 marked this conversation as resolved.
|
||
|
|
||
| BrokersToElrs(SnapshotRegistry snapshotRegistry) { | ||
| this.snapshotRegistry = snapshotRegistry; | ||
| this.elrMembers = new TimelineHashMap<>(snapshotRegistry, 0); | ||
| } | ||
|
|
||
| /** | ||
| * Update our records of a partition's ELR. | ||
| * | ||
| * @param topicId The topic ID of the partition. | ||
| * @param partitionId The partition ID of the partition. | ||
| * @param prevElr The previous ELR, or null if the partition is new. | ||
| * @param nextElr The new ELR, or null if the partition is being removed. | ||
| */ | ||
|
|
||
| void update(Uuid topicId, int partitionId, int[] prevElr, int[] nextElr) { | ||
| int[] prev; | ||
| if (prevElr == null) { | ||
| prev = NONE; | ||
| } else { | ||
| prev = Replicas.clone(prevElr); | ||
| Arrays.sort(prev); | ||
| } | ||
| int[] next; | ||
| if (nextElr == null) { | ||
| next = NONE; | ||
| } else { | ||
| next = Replicas.clone(nextElr); | ||
| Arrays.sort(next); | ||
| } | ||
|
|
||
| int i = 0, j = 0; | ||
| while (true) { | ||
| if (i == prev.length) { | ||
| if (j == next.length) { | ||
| break; | ||
| } | ||
| int newReplica = next[j]; | ||
| add(newReplica, topicId, partitionId); | ||
| j++; | ||
| } else if (j == next.length) { | ||
| int prevReplica = prev[i]; | ||
| remove(prevReplica, topicId, partitionId); | ||
| i++; | ||
| } else { | ||
| int prevReplica = prev[i]; | ||
| int newReplica = next[j]; | ||
| if (prevReplica < newReplica) { | ||
| remove(prevReplica, topicId, partitionId); | ||
| i++; | ||
| } else if (prevReplica > newReplica) { | ||
| add(newReplica, topicId, partitionId); | ||
| j++; | ||
| } else { | ||
| i++; | ||
| j++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void removeTopicEntryForBroker(Uuid topicId, int brokerId) { | ||
| Map<Uuid, int[]> topicMap = elrMembers.get(brokerId); | ||
| if (topicMap != null) { | ||
| topicMap.remove(topicId); | ||
| } | ||
| } | ||
|
|
||
| private void add(int brokerId, Uuid topicId, int newPartition) { | ||
| TimelineHashMap<Uuid, int[]> topicMap = elrMembers.get(brokerId); | ||
| if (topicMap == null) { | ||
| topicMap = new TimelineHashMap<>(snapshotRegistry, 0); | ||
| elrMembers.put(brokerId, topicMap); | ||
| } | ||
| int[] partitions = topicMap.get(topicId); | ||
| int[] newPartitions; | ||
| if (partitions == null) { | ||
| newPartitions = new int[1]; | ||
| } else { | ||
| newPartitions = new int[partitions.length + 1]; | ||
| System.arraycopy(partitions, 0, newPartitions, 0, partitions.length); | ||
| } | ||
| newPartitions[newPartitions.length - 1] = newPartition; | ||
| topicMap.put(topicId, newPartitions); | ||
| } | ||
|
|
||
| private void remove(int brokerId, Uuid topicId, int removedPartition) { | ||
| TimelineHashMap<Uuid, int[]> topicMap = elrMembers.get(brokerId); | ||
| if (topicMap == null) { | ||
| throw new RuntimeException("Broker " + brokerId + " has no elrMembers " + | ||
| "entry, so we can't remove " + topicId + ":" + removedPartition); | ||
| } | ||
| int[] partitions = topicMap.get(topicId); | ||
| if (partitions == null) { | ||
| throw new RuntimeException("Broker " + brokerId + " has no " + | ||
| "entry in elrMembers for topic " + topicId); | ||
| } | ||
| if (partitions.length == 1) { | ||
| if (partitions[0] != removedPartition) { | ||
| throw new RuntimeException("Broker " + brokerId + " has no " + | ||
| "entry in elrMembers for " + topicId + ":" + removedPartition); | ||
| } | ||
| topicMap.remove(topicId); | ||
| if (topicMap.isEmpty()) { | ||
| elrMembers.remove(brokerId); | ||
| } | ||
| } else { | ||
| int[] newPartitions = new int[partitions.length - 1]; | ||
| int j = 0; | ||
| for (int i = 0; i < partitions.length; i++) { | ||
| int partition = partitions[i]; | ||
| if (partition != removedPartition) { | ||
| newPartitions[j++] = partition; | ||
| } | ||
| } | ||
| topicMap.put(topicId, newPartitions); | ||
| } | ||
| } | ||
|
|
||
| BrokersToIsrs.PartitionsOnReplicaIterator partitionsWithBrokerInElr(int brokerId) { | ||
| Map<Uuid, int[]> topicMap = elrMembers.get(brokerId); | ||
| if (topicMap == null) { | ||
| topicMap = Collections.emptyMap(); | ||
| } | ||
| return new BrokersToIsrs.PartitionsOnReplicaIterator(topicMap, false); | ||
| } | ||
| } | ||
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
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.
To help review this file, you can refer to the BrokersToIsrs.java
The reasons not to modify the BrokersToIsrs are:
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 guess the alternative would be to extend the BrokersToIsrs to include a ELR_FLAG similar to the LEADER_FLAG. I agree this would probably be complex to implement. If I recall from the KIP, the ELR is normally empty, so maybe having this parallel data structure isn't so bad (since it will normally not be populated)?
@cmccabe since you worked on
BrokersToIsrsoriginally, do you have any opinion here?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.
It looks like that in order to work correctly, we need to make sure that ISR and ELR are always disjoint sets (otherwise we'd generate multiple records for the same partition). Can we add a comment about this?
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.
Updated the comment in the ReplicationControlManager.
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.
If we keeping this as a second data structure and we need to ensure BrokersToIsrs and BrokersToElrs are disjoint, I think we need some guards/checks. For example, it seems we are always updating both data structures
If we need to ensure a replica is not in the ISR and ELR for correctness, we should have a check.