-
Notifications
You must be signed in to change notification settings - Fork 15.1k
KAFKA-20297: Cleanup org.apache.kafka.common.utils.CollectionUtils
#21818
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
98a9db1
5c081f1
4aea5f0
05ad35d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,11 +22,10 @@ | |
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import javax.security.auth.callback.Callback; | ||
|
|
||
| import static org.apache.kafka.common.utils.CollectionUtils.subtractMap; | ||
|
|
||
| /** | ||
| * A {@code Callback} for use by the {@code SaslServer} implementation when it | ||
| * needs to validate the SASL extensions for the OAUTHBEARER mechanism | ||
|
|
@@ -90,6 +89,12 @@ public Map<String, String> ignoredExtensions() { | |
| return Collections.unmodifiableMap(subtractMap(subtractMap(inputExtensions.map(), invalidExtensions), validatedExtensions)); | ||
|
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. The original logic will create map repeatedly. Could you use for-loop to streamline it? public Map<String, String> ignoredExtensions() {
Map<String, String> ignored = new HashMap<>();
for (Map.Entry<String, String> entry : inputExtensions.map().entrySet()) {
String key = entry.getKey();
if (!invalidExtensions.containsKey(key) && !validatedExtensions.containsKey(key)) {
ignored.put(key, entry.getValue());
}
}
return Collections.unmodifiableMap(ignored);
}
Contributor
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. I thought the |
||
| } | ||
|
|
||
| private static Map<String, String> subtractMap(Map<String, String> minuend, Map<String, String> subtrahend) { | ||
| return minuend.entrySet().stream() | ||
| .filter(entry -> !subtrahend.containsKey(entry.getKey())) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| } | ||
|
|
||
| /** | ||
| * Validates a specific extension in the original {@code inputExtensions} map | ||
| * @param extensionName - the name of the extension which was validated | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,6 @@ | |
| import org.apache.kafka.common.protocol.Errors; | ||
| import org.apache.kafka.common.requests.DescribeProducersRequest; | ||
| import org.apache.kafka.common.requests.DescribeProducersResponse; | ||
| import org.apache.kafka.common.utils.CollectionUtils; | ||
| import org.apache.kafka.common.utils.LogContext; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -120,7 +119,7 @@ public void testBuildRequest() { | |
| int brokerId = 3; | ||
| DescribeProducersRequest.Builder request = handler.buildBatchedRequest(brokerId, topicPartitions); | ||
|
|
||
| List<DescribeProducersRequestData.TopicRequest> topics = request.data.topics(); | ||
| DescribeProducersRequestData.TopicRequestCollection topics = request.data.topics(); | ||
|
|
||
| assertEquals(Set.of("foo", "bar"), topics.stream() | ||
| .map(DescribeProducersRequestData.TopicRequest::name) | ||
|
|
@@ -308,7 +307,7 @@ private DescribeProducersResponse describeProducersResponse( | |
| ) { | ||
| DescribeProducersResponseData response = new DescribeProducersResponseData(); | ||
| Map<String, Map<Integer, PartitionResponse>> partitionResponsesByTopic = | ||
| CollectionUtils.groupPartitionDataByTopic(partitionResponses); | ||
| groupPartitionDataByTopic(partitionResponses); | ||
|
|
||
| for (Map.Entry<String, Map<Integer, PartitionResponse>> topicEntry : partitionResponsesByTopic.entrySet()) { | ||
| String topic = topicEntry.getKey(); | ||
|
|
@@ -327,4 +326,12 @@ private DescribeProducersResponse describeProducersResponse( | |
| return new DescribeProducersResponse(response); | ||
| } | ||
|
|
||
| private static Map<String, Map<Integer, PartitionResponse>> groupPartitionDataByTopic( | ||
|
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. we don't use such complex structure in tests actually. Maybe we could use |
||
| Map<TopicPartition, PartitionResponse> partitionResponses | ||
| ) { | ||
| return partitionResponses.entrySet().stream() | ||
| .collect(Collectors.groupingBy( | ||
| e -> e.getKey().topic(), | ||
| Collectors.toMap(e -> e.getKey().partition(), Map.Entry::getValue))); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,6 @@ | |
| import org.apache.kafka.common.PartitionInfo; | ||
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.common.test.api.Flaky; | ||
| import org.apache.kafka.common.utils.CollectionUtils; | ||
| import org.apache.kafka.common.utils.Utils; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
|
|
@@ -36,6 +35,7 @@ | |
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
|
|
@@ -1428,6 +1428,11 @@ private String pad(int num, int digits) { | |
| return "0".repeat(Math.max(0, digits - iDigits)) + num; | ||
| } | ||
|
|
||
| private static Map<String, List<Integer>> groupPartitionsByTopic(Collection<TopicPartition> partitions) { | ||
| return partitions.stream() | ||
| .collect(Collectors.groupingBy(TopicPartition::topic, Collectors.mapping(TopicPartition::partition, Collectors.toList()))); | ||
| } | ||
|
|
||
| protected static List<String> topics(String... topics) { | ||
| return Arrays.asList(topics); | ||
| } | ||
|
|
@@ -1522,8 +1527,8 @@ protected void verifyValidityAndBalance(Map<String, Subscription> subscriptions, | |
| if (Math.abs(len - otherLen) <= 1) | ||
| continue; | ||
|
|
||
| Map<String, List<Integer>> map = CollectionUtils.groupPartitionsByTopic(partitions); | ||
| Map<String, List<Integer>> otherMap = CollectionUtils.groupPartitionsByTopic(otherPartitions); | ||
| Map<String, List<Integer>> map = groupPartitionsByTopic(partitions); | ||
|
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. we could streamline the test. Set<String> otherTopics = otherPartitions.stream()
.map(TopicPartition::topic)
.collect(Collectors.toSet());
for (TopicPartition tp : partitions) {
assertFalse(otherTopics.contains(tp.topic()),
"Error: Some partitions can be moved...");
} |
||
| Map<String, List<Integer>> otherMap = groupPartitionsByTopic(otherPartitions); | ||
|
|
||
| int moreLoaded = len > otherLen ? i : j; | ||
| int lessLoaded = len > otherLen ? j : i; | ||
|
|
||
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.
Could you inline it?