-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-9434: automated protocol for alterReplicaLogDirs #8311
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
mimaison
merged 23 commits into
apache:trunk
from
tombentley:KAFKA-9434-AlterReplicaLogDirs-automatedprotocol
Jun 4, 2020
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c218317
wip, need to fix KafkaApis/ReplicaManager
tombentley 9a33890
wip
tombentley 21f7375
Minor improvements
tombentley a427748
Review comments
tombentley f9069aa
review comments
tombentley d3fd28c
Add test for KafkaAdminClient.alterReplicaLogDirs
tombentley ff5e457
Add test for KafkaAdminClient.alterReplicaLogDirs
tombentley a4bcdc0
Add test for AlterLogDirs RPCs to RequestResponseTests
tombentley e889378
Suggested code by @dajac
tombentley 01a2980
Add KafkaApisTest.testAlterReplicaLogDirs()
tombentley eaf80dc
Fix test
tombentley 0fc3bc3
review comments
tombentley 1dffaa9
Review comments; change handling of unexpected responses
tombentley c82455f
Review comments 2
tombentley 4c5d1e6
Exception on partial response
tombentley e0fc9bf
Add extra test
tombentley 103a123
Two more tests
tombentley 0ebdc39
Code review
tombentley 3d1aa65
completeUnrealizedFutures
tombentley 6a794a6
Review comments + improvement
tombentley 7b6f3c0
review comment
tombentley 66f796c
TestUtils.assertFutureThrows
tombentley 2af415b
More TestUtils.assertFutureThrows
tombentley 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 |
|---|---|---|
|
|
@@ -69,6 +69,11 @@ | |
| import org.apache.kafka.common.internals.KafkaFutureImpl; | ||
| import org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData; | ||
| import org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic; | ||
| import org.apache.kafka.common.message.AlterReplicaLogDirsRequestData; | ||
| import org.apache.kafka.common.message.AlterReplicaLogDirsRequestData.AlterReplicaLogDir; | ||
| import org.apache.kafka.common.message.AlterReplicaLogDirsRequestData.AlterReplicaLogDirTopic; | ||
| import org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirPartitionResult; | ||
| import org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirTopicResult; | ||
| import org.apache.kafka.common.message.CreateAclsRequestData; | ||
| import org.apache.kafka.common.message.CreateAclsRequestData.AclCreation; | ||
| import org.apache.kafka.common.message.CreateAclsResponseData.AclCreationResult; | ||
|
|
@@ -234,6 +239,7 @@ | |
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.function.Function; | ||
| import java.util.function.Predicate; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.Collectors; | ||
|
|
@@ -1402,6 +1408,17 @@ int numPendingCalls() { | |
| return runnable.pendingCalls.size(); | ||
| } | ||
|
|
||
| /** | ||
| * Fail futures in the given stream which are not done. | ||
| * Used when a response handler expected a result for some entity but no result was present. | ||
| */ | ||
| private static <K, V> void completeUnrealizedFutures( | ||
| Stream<Map.Entry<K, KafkaFutureImpl<V>>> futures, | ||
| Function<K, String> messageFormatter) { | ||
| futures.filter(entry -> !entry.getValue().isDone()).forEach(entry -> | ||
| entry.getValue().completeExceptionally(new ApiException(messageFormatter.apply(entry.getKey())))); | ||
| } | ||
|
|
||
| @Override | ||
| public CreateTopicsResult createTopics(final Collection<NewTopic> newTopics, | ||
| final CreateTopicsOptions options) { | ||
|
|
@@ -1479,13 +1496,8 @@ public void handleResponse(AbstractResponse abstractResponse) { | |
| } | ||
| } | ||
| // The server should send back a response for every topic. But do a sanity check anyway. | ||
| for (Map.Entry<String, KafkaFutureImpl<TopicMetadataAndConfig>> entry : topicFutures.entrySet()) { | ||
| KafkaFutureImpl<TopicMetadataAndConfig> future = entry.getValue(); | ||
| if (!future.isDone()) { | ||
| future.completeExceptionally(new ApiException("The server response did not " + | ||
| "contain a reference to node " + entry.getKey())); | ||
| } | ||
| } | ||
| completeUnrealizedFutures(topicFutures.entrySet().stream(), | ||
| topic -> "The controller response did not contain a result for topic " + topic); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1552,13 +1564,8 @@ void handleResponse(AbstractResponse abstractResponse) { | |
| } | ||
| } | ||
| // The server should send back a response for every topic. But do a sanity check anyway. | ||
| for (Map.Entry<String, KafkaFutureImpl<Void>> entry : topicFutures.entrySet()) { | ||
| KafkaFutureImpl<Void> future = entry.getValue(); | ||
| if (!future.isDone()) { | ||
| future.completeExceptionally(new ApiException("The server response did not " + | ||
| "contain a reference to node " + entry.getKey())); | ||
| } | ||
| } | ||
| completeUnrealizedFutures(topicFutures.entrySet().stream(), | ||
| topic -> "The controller response did not contain a result for topic " + topic); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -2207,21 +2214,31 @@ public AlterReplicaLogDirsResult alterReplicaLogDirs(Map<TopicPartitionReplica, | |
| for (TopicPartitionReplica replica : replicaAssignment.keySet()) | ||
| futures.put(replica, new KafkaFutureImpl<>()); | ||
|
|
||
| Map<Integer, Map<TopicPartition, String>> replicaAssignmentByBroker = new HashMap<>(); | ||
| Map<Integer, AlterReplicaLogDirsRequestData> replicaAssignmentByBroker = new HashMap<>(); | ||
| for (Map.Entry<TopicPartitionReplica, String> entry: replicaAssignment.entrySet()) { | ||
| TopicPartitionReplica replica = entry.getKey(); | ||
| String logDir = entry.getValue(); | ||
| int brokerId = replica.brokerId(); | ||
| TopicPartition topicPartition = new TopicPartition(replica.topic(), replica.partition()); | ||
| if (!replicaAssignmentByBroker.containsKey(brokerId)) | ||
| replicaAssignmentByBroker.put(brokerId, new HashMap<>()); | ||
| replicaAssignmentByBroker.get(brokerId).put(topicPartition, logDir); | ||
| AlterReplicaLogDirsRequestData value = replicaAssignmentByBroker.computeIfAbsent(brokerId, | ||
|
Member
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. nit: Could we rename |
||
| key -> new AlterReplicaLogDirsRequestData()); | ||
| AlterReplicaLogDir alterReplicaLogDir = value.dirs().find(logDir); | ||
| if (alterReplicaLogDir == null) { | ||
| alterReplicaLogDir = new AlterReplicaLogDir(); | ||
| alterReplicaLogDir.setPath(logDir); | ||
| value.dirs().add(alterReplicaLogDir); | ||
| } | ||
| AlterReplicaLogDirTopic alterReplicaLogDirTopic = alterReplicaLogDir.topics().find(replica.topic()); | ||
| if (alterReplicaLogDirTopic == null) { | ||
| alterReplicaLogDirTopic = new AlterReplicaLogDirTopic().setName(replica.topic()); | ||
| alterReplicaLogDir.topics().add(alterReplicaLogDirTopic); | ||
| } | ||
| alterReplicaLogDirTopic.partitions().add(replica.partition()); | ||
| } | ||
|
|
||
| final long now = time.milliseconds(); | ||
| for (Map.Entry<Integer, Map<TopicPartition, String>> entry: replicaAssignmentByBroker.entrySet()) { | ||
| for (Map.Entry<Integer, AlterReplicaLogDirsRequestData> entry: replicaAssignmentByBroker.entrySet()) { | ||
| final int brokerId = entry.getKey(); | ||
| final Map<TopicPartition, String> assignment = entry.getValue(); | ||
| final AlterReplicaLogDirsRequestData assignment = entry.getValue(); | ||
|
|
||
| runnable.call(new Call("alterReplicaLogDirs", calcDeadlineMs(now, options.timeoutMs()), | ||
| new ConstantNodeIdProvider(brokerId)) { | ||
|
|
@@ -2234,20 +2251,27 @@ public AlterReplicaLogDirsRequest.Builder createRequest(int timeoutMs) { | |
| @Override | ||
| public void handleResponse(AbstractResponse abstractResponse) { | ||
| AlterReplicaLogDirsResponse response = (AlterReplicaLogDirsResponse) abstractResponse; | ||
| for (Map.Entry<TopicPartition, Errors> responseEntry: response.responses().entrySet()) { | ||
| TopicPartition tp = responseEntry.getKey(); | ||
| Errors error = responseEntry.getValue(); | ||
| TopicPartitionReplica replica = new TopicPartitionReplica(tp.topic(), tp.partition(), brokerId); | ||
| KafkaFutureImpl<Void> future = futures.get(replica); | ||
| if (future == null) { | ||
| handleFailure(new IllegalStateException( | ||
| "The partition " + tp + " in the response from broker " + brokerId + " is not in the request")); | ||
| } else if (error == Errors.NONE) { | ||
| future.complete(null); | ||
| } else { | ||
| future.completeExceptionally(error.exception()); | ||
| for (AlterReplicaLogDirTopicResult topicResult: response.data().results()) { | ||
| for (AlterReplicaLogDirPartitionResult partitionResult: topicResult.partitions()) { | ||
| TopicPartitionReplica replica = new TopicPartitionReplica( | ||
| topicResult.topicName(), partitionResult.partitionIndex(), brokerId); | ||
| KafkaFutureImpl<Void> future = futures.get(replica); | ||
| if (future == null) { | ||
| log.warn("The partition {} in the response from broker {} is not in the request", | ||
| new TopicPartition(topicResult.topicName(), partitionResult.partitionIndex()), | ||
| brokerId); | ||
| } else if (partitionResult.errorCode() == Errors.NONE.code()) { | ||
| future.complete(null); | ||
| } else { | ||
| future.completeExceptionally(Errors.forCode(partitionResult.errorCode()).exception()); | ||
| } | ||
| } | ||
| } | ||
| // The server should send back a response for every replica. But do a sanity check anyway. | ||
| completeUnrealizedFutures( | ||
| futures.entrySet().stream().filter(entry -> entry.getKey().brokerId() == brokerId), | ||
| replica -> "The response from broker " + brokerId + | ||
| " did not contain a result for replica " + replica); | ||
| } | ||
| @Override | ||
| void handleFailure(Throwable throwable) { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.