-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8768: DeleteRecords request/response automated protocol #7957
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 3 commits into
apache:trunk
from
tombentley:KAFKA-8768-DeleteRecords-automated-protocol
Mar 13, 2020
Merged
Changes from 2 commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,11 @@ | |
| import org.apache.kafka.common.message.DeleteAclsResponseData.DeleteAclsFilterResult; | ||
| import org.apache.kafka.common.message.DeleteAclsResponseData.DeleteAclsMatchingAcl; | ||
| import org.apache.kafka.common.message.DeleteGroupsRequestData; | ||
| import org.apache.kafka.common.message.DeleteRecordsRequestData; | ||
| import org.apache.kafka.common.message.DeleteRecordsRequestData.DeleteRecordsPartition; | ||
| import org.apache.kafka.common.message.DeleteRecordsRequestData.DeleteRecordsTopic; | ||
| import org.apache.kafka.common.message.DeleteRecordsResponseData; | ||
| import org.apache.kafka.common.message.DeleteRecordsResponseData.DeleteRecordsTopicResult; | ||
| import org.apache.kafka.common.message.DeleteTopicsRequestData; | ||
| import org.apache.kafka.common.message.DeleteTopicsResponseData.DeletableTopicResult; | ||
| import org.apache.kafka.common.message.DescribeGroupsRequestData; | ||
|
|
@@ -2469,19 +2474,30 @@ void handleResponse(AbstractResponse abstractResponse) { | |
| Cluster cluster = response.cluster(); | ||
|
|
||
| // Group topic partitions by leader | ||
| Map<Node, Map<TopicPartition, Long>> leaders = new HashMap<>(); | ||
| Map<Node, Map<String, DeleteRecordsTopic>> leaders = new HashMap<>(); | ||
| for (Map.Entry<TopicPartition, RecordsToDelete> entry: recordsToDelete.entrySet()) { | ||
| KafkaFutureImpl<DeletedRecords> future = futures.get(entry.getKey()); | ||
| TopicPartition topicPartition = entry.getKey(); | ||
| KafkaFutureImpl<DeletedRecords> future = futures.get(topicPartition); | ||
|
|
||
| // Fail partitions with topic errors | ||
| Errors topicError = errors.get(entry.getKey().topic()); | ||
| if (errors.containsKey(entry.getKey().topic())) { | ||
| Errors topicError = errors.get(topicPartition.topic()); | ||
| if (errors.containsKey(topicPartition.topic())) { | ||
| future.completeExceptionally(topicError.exception()); | ||
| } else { | ||
| Node node = cluster.leaderFor(entry.getKey()); | ||
| Node node = cluster.leaderFor(topicPartition); | ||
| if (node != null) { | ||
| leaders.computeIfAbsent(node, key -> new HashMap<>()).put(entry.getKey(), | ||
| entry.getValue().beforeOffset()); | ||
| if (!leaders.containsKey(node)) | ||
| leaders.put(node, new HashMap<>()); | ||
| Map<String, DeleteRecordsTopic> deletionsForLeader = leaders.get(node); | ||
| DeleteRecordsTopic deleteRecords = deletionsForLeader.get(topicPartition.topic()); | ||
|
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. Same here, I think |
||
| if (deleteRecords == null) { | ||
| deleteRecords = new DeleteRecordsTopic() | ||
| .setName(topicPartition.topic()); | ||
| deletionsForLeader.put(topicPartition.topic(), deleteRecords); | ||
| } | ||
| deleteRecords.partitions().add(new DeleteRecordsPartition() | ||
| .setPartitionIndex(topicPartition.partition()) | ||
| .setOffset(entry.getValue().beforeOffset())); | ||
| } else { | ||
| future.completeExceptionally(Errors.LEADER_NOT_AVAILABLE.exception()); | ||
| } | ||
|
|
@@ -2490,36 +2506,45 @@ void handleResponse(AbstractResponse abstractResponse) { | |
|
|
||
| final long deleteRecordsCallTimeMs = time.milliseconds(); | ||
|
|
||
| for (final Map.Entry<Node, Map<TopicPartition, Long>> entry : leaders.entrySet()) { | ||
| final Map<TopicPartition, Long> partitionDeleteOffsets = entry.getValue(); | ||
| for (final Map.Entry<Node, Map<String, DeleteRecordsTopic>> entry : leaders.entrySet()) { | ||
| final Map<String, DeleteRecordsTopic> partitionDeleteOffsets = entry.getValue(); | ||
| final int brokerId = entry.getKey().id(); | ||
|
|
||
| runnable.call(new Call("deleteRecords", deadline, | ||
| new ConstantNodeIdProvider(brokerId)) { | ||
|
|
||
| @Override | ||
| DeleteRecordsRequest.Builder createRequest(int timeoutMs) { | ||
| return new DeleteRecordsRequest.Builder(timeoutMs, partitionDeleteOffsets); | ||
| return new DeleteRecordsRequest.Builder(new DeleteRecordsRequestData() | ||
| .setTimeoutMs(timeoutMs) | ||
| .setTopics(new ArrayList<>(partitionDeleteOffsets.values()))); | ||
| } | ||
|
|
||
| @Override | ||
| void handleResponse(AbstractResponse abstractResponse) { | ||
| DeleteRecordsResponse response = (DeleteRecordsResponse) abstractResponse; | ||
| for (Map.Entry<TopicPartition, DeleteRecordsResponse.PartitionResponse> result: response.responses().entrySet()) { | ||
|
|
||
| KafkaFutureImpl<DeletedRecords> future = futures.get(result.getKey()); | ||
| if (result.getValue().error == Errors.NONE) { | ||
| future.complete(new DeletedRecords(result.getValue().lowWatermark)); | ||
| } else { | ||
| future.completeExceptionally(result.getValue().error.exception()); | ||
| for (DeleteRecordsTopicResult topicResult: response.data().topics()) { | ||
| for (DeleteRecordsResponseData.DeleteRecordsPartitionResult partitionResult : topicResult.partitions()) { | ||
| KafkaFutureImpl<DeletedRecords> future = futures.get(new TopicPartition(topicResult.name(), partitionResult.partitionIndex())); | ||
| if (partitionResult.errorCode() == Errors.NONE.code()) { | ||
| future.complete(new DeletedRecords(partitionResult.lowWatermark())); | ||
| } else { | ||
| future.completeExceptionally(Errors.forCode(partitionResult.errorCode()).exception()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| void handleFailure(Throwable throwable) { | ||
| Stream<KafkaFutureImpl<DeletedRecords>> callFutures = | ||
| partitionDeleteOffsets.keySet().stream().map(futures::get); | ||
| partitionDeleteOffsets.values().stream().flatMap( | ||
| recordsToDelete1 -> { | ||
| Stream<TopicPartition> topicPartitionStream = recordsToDelete1.partitions().stream().map(partitionsToDelete -> | ||
|
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. Can we return that directly without creating a variable? |
||
| new TopicPartition(recordsToDelete1.name(), partitionsToDelete.partitionIndex())); | ||
| return topicPartitionStream; | ||
| } | ||
| ).map(futures::get); | ||
| completeAllExceptionally(callFutures, throwable); | ||
| } | ||
| }, deleteRecordsCallTimeMs); | ||
|
|
||
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.
I think we can
computeIfAbsent()here. It will replacecontainsKey,putandget.