Skip to content

KAFKA-19183: Remove kafka.utils.Pool and refactor usages #19531

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

RaphaelFakhri
Copy link

Problem

The kafka.utils.Pool class is a thin Scala wrapper around java.util.concurrent.ConcurrentHashMap. While it provides a convenient getAndMaybePut method, the underlying functionality is largely replicated by ConcurrentHashMap's standard methods, particularly computeIfAbsent.

Analysis / Motivation

As discussed in KAFKA-19183, the Pool class adds an unnecessary layer of abstraction. The primary addition over raw ConcurrentHashMap is the getAndMaybePut method, which can be effectively replaced with ConcurrentHashMap.computeIfAbsent.

While Pool allows defining a default value factory at construction, analysis showed this feature is only used in a limited number of locations. These usages can be easily refactored to pass the value creation logic explicitly to computeIfAbsent at the call site.

Removing the Pool class:

  • Simplifies the codebase
  • Reduces maintenance overhead
  • Promotes the use of standard, well-understood Java concurrent collections

Changes

Removal of Files

  • kafka/utils/Pool.scalaRemoved
  • kafka/utils/PoolTest.scalaRemoved (tested the now-removed Pool class)

Refactoring of Usages

  • All instances of kafka.utils.Pool have been replaced with java.util.concurrent.ConcurrentHashMap.

  • Calls to Pool.getAndMaybePut(key, createValue) have been replaced with:

    concurrentHashMap.computeIfAbsent(key, _ => createValue)
  • Calls to Pool.getAndMaybePut(key) (which used the constructor factory) have been replaced with:

    concurrentHashMap.computeIfAbsent(key, k => valueFactory(k))

    where valueFactory is now locally defined or accessible in scope.

  • Calls accessing collection views (.keys, .values, .iterator) have been updated to use Java API methods (.keySet(), .values(), .entrySet().iterator()) followed by .asScala from scala.jdk.CollectionConverters to maintain the Scala collection interface where necessary.

  • Other minor method name updates:

    • .size.size()
    • .putIfNotExists.putIfAbsent
  • The addLoadedTransactionsToCache method signature in TransactionStateManager has been updated to accept ConcurrentHashMap instead of Pool.

Testing

  • PoolTest.scala was removed.
  • Existing unit and integration tests for the components that previously used Pool (e.g., FetcherLagStats, TransactionStateManager, ReplicaManager) provide sufficient coverage for the refactored code paths.
  • No new tests were added, as the core concurrent map functionality is provided by the JDK.

Compatibility

  • This change is internal refactoring and does not affect any public APIs or external compatibility.

Checklist

  • Code follows the coding style.
  • Documentation is updated. (Not applicable for this internal refactoring)
  • Unit tests are updated/added (Removed PoolTest, relying on existing component tests)
  • Integration tests are updated/added. (Not applicable, existing tests cover functionality)
  • KIP has been created or exists for this change. (KAFKA-19183 serves as the discussion/motivation)
  • Release notes are updated. (Not applicable for this internal refactoring)

@github-actions github-actions bot added triage PRs from the community core Kafka Broker labels Apr 22, 2025
@FrankYang0529
Copy link
Member

Hi @RaphaelFakhri, I will handle the issue KAFKA-19183. Could you check with Jira owner before creating PR next time? Thanks.

@frankvicky
Copy link
Contributor

Hi @RaphaelFakhri
As @FrankYang0529 said, if you are willing to contribute, please refer to the contributing guide.
If you are willing to handle an issue that already has an assignee, please make sure you have asked the original assignee first.

Copy link

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-approved core Kafka Broker needs-attention triage PRs from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants