Skip to content

MINOR: Cleanup Core Module- Scala Modules (2/n) #19510

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
merged 9 commits into from
May 28, 2025

Conversation

sjhajharia
Copy link
Contributor

@sjhajharia sjhajharia commented Apr 18, 2025

Now that Kafka Brokers support Java 17, this PR makes some changes in
core module. The changes in this PR are limited to only the Scala files
in the Core module's tests. The unit tests module is still pending. It
shall follow next. The changes mostly include:

  • Collections.emptyList(), Collections.singletonList() and
    Arrays.asList() are replaced with List.of()
  • Collections.emptyMap() and Collections.singletonMap() are replaced
    with Map.of()
  • Collections.singleton() is replaced with Set.of()

To be clear, the directories being targeted in this PR are:
KafkaApisTest.scala

Reviewers: PoAn Yang [email protected], Ming-Yen Chung [email protected], Yung [email protected], TengYao Chi [email protected], Ken Huang [email protected]

@github-actions github-actions bot added triage PRs from the community core Kafka Broker tests Test fixes (including flaky tests) labels Apr 18, 2025
Copy link
Member

@FrankYang0529 FrankYang0529 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjhajharia, I think we can do more in this PR.

  1. mutable.HashSet -> util.Set.of
    val resources = new mutable.HashSet[String]
    resources.add("test1")
    resources.add("test2")
  2. util.HashMap -> util.Map.of
    val erroneousPartitions: util.Map[TopicIdPartition, ShareFetchResponseData.PartitionData] = new util.HashMap()
    erroneousPartitions.put(
    tp2,
    new ShareFetchResponseData.PartitionData()
    .setPartitionIndex(1)
    .setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code)
    )
    erroneousPartitions.put(
    tp3,
    new ShareFetchResponseData.PartitionData()
    .setPartitionIndex(0)
    .setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code)
    )

@sjhajharia
Copy link
Contributor Author

Thanks @FrankYang0529
Adding in the changes you suggested.

@github-actions github-actions bot removed the triage PRs from the community label Apr 19, 2025
Copy link
Contributor

@frankvicky frankvicky left a 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 also remove the operations of asScala followed by asJava.
For example:

actions.asScala.map { action =>
acls.getOrElse(action.resourcePattern.name, AuthorizationResult.DENIED)
}.asJava

@sjhajharia
Copy link
Contributor Author

Thanks @frankvicky
Adding in the changes in a subsequent commit. Pls review when possible!

@frankvicky frankvicky requested a review from chia7712 April 21, 2025 02:27
Copy link
Collaborator

@m1a2st m1a2st left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this patch, LGTM

@sjhajharia
Copy link
Contributor Author

Hey @chia7712 ,
Gentle reminder for the PR here!

Copy link
Contributor

@frankvicky frankvicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjhajharia : LGTM assuming CI pass

@sjhajharia
Copy link
Contributor Author

Thanks @frankvicky
The build looks good to me

@@ -9332,7 +9316,7 @@ class KafkaApisTest extends Logging {
assertEquals(clusterId, describeClusterResponse.data.clusterId)
assertEquals(8096, describeClusterResponse.data.clusterAuthorizedOperations)
assertEquals(util.Set.copyOf(metadataCache.getAliveBrokerNodes(plaintextListener)),
describeClusterResponse.nodes.asScala.values.toSet.asJava)
new util.HashSet(describeClusterResponse.nodes.values))
Copy link
Collaborator

@Yunyung Yunyung May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Left one minor.

Suggested change
new util.HashSet(describeClusterResponse.nodes.values))
util.Set.copyOf(describeClusterResponse.nodes.values))

Copy link
Collaborator

@mingyen066 mingyen066 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup.

There are many occurrences of this pattern:

val topicNames = new util.HashMap[Uuid, String]()
topicNames.put(topicId1, "foo1")
topicNames.put(topicId2, "foo2")

But in many of those cases, no additional topics are added later, so they can all be replaced with:

val topicNames = util.Map.of(topicId1, "foo1", topicId2, "foo2")

For example:
https://github.com/apache/kafka/blob/trunk/core/src/test/scala/unit/kafka/server/KafkaApisTest.scala#L7166C2-L7168C37

@@ -9332,7 +9316,7 @@ class KafkaApisTest extends Logging {
assertEquals(clusterId, describeClusterResponse.data.clusterId)
assertEquals(8096, describeClusterResponse.data.clusterAuthorizedOperations)
assertEquals(util.Set.copyOf(metadataCache.getAliveBrokerNodes(plaintextListener)),
describeClusterResponse.nodes.asScala.values.toSet.asJava)
new util.HashSet(describeClusterResponse.nodes.values))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use util.Set.copyOf ?

@sjhajharia
Copy link
Contributor Author

Thanks @mingyen066 and @Yunyung for the review
I have added the suggestions. Please re-review

@sjhajharia sjhajharia requested review from Yunyung and mingyen066 May 24, 2025 04:46
@sjhajharia
Copy link
Contributor Author

Gentle reminder
@frankvicky

Copy link
Collaborator

@mingyen066 mingyen066 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@frankvicky frankvicky merged commit 0fd7ed5 into apache:trunk May 28, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-approved core Kafka Broker tests Test fixes (including flaky tests)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants