Skip to content

KAFKA-20341: Clarify return semantics of assignment(), subscription(), and metrics() in consumer Javadoc#21849

Open
DL1231 wants to merge 1 commit intoapache:trunkfrom
DL1231:KAFKA-20341
Open

KAFKA-20341: Clarify return semantics of assignment(), subscription(), and metrics() in consumer Javadoc#21849
DL1231 wants to merge 1 commit intoapache:trunkfrom
DL1231:KAFKA-20341

Conversation

@DL1231
Copy link
Collaborator

@DL1231 DL1231 commented Mar 22, 2026

Improves Javadoc to clarify the return semantics of three methods:

  • assignment() and subscription(): return an immutable
    snapshot
    at the time of the call. - metrics(): returns an
    unmodifiable live view. The Javadoc now documents this clearly;
    the implementation is unchanged from the original behavior.

@github-actions github-actions bot added triage PRs from the community consumer clients small Small PRs labels Mar 22, 2026
acquireAndEnsureOpen();
try {
return Collections.unmodifiableSet(subscriptions.assignedPartitions());
return Set.copyOf(subscriptions.assignedPartitions());
Copy link
Member

Choose a reason for hiding this comment

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

The subscriptions.assignedPartitions() already returns a new collection. Making another one won't make memory walk the plank, but it is definitely unnecessary, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

same goes to ClassicKafkaConsumer too.

* <p>The returned map is an unmodifiable live view of the metrics. Changes to the underlying
* metrics will be reflected in the returned map.
*
* @return An unmodifiable live view of the map of metrics currently maintained by the consumer
Copy link
Member

Choose a reason for hiding this comment

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

For the sake of consistency, could we apply these excellent javadocs to Admin and Producer too?

@@ -1400,6 +1408,11 @@ public Uuid clientInstanceId(Duration timeout) {

/**
* Get the metrics kept by the consumer
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:indentation issue

* <p>The returned map is an unmodifiable live view of the metrics. Changes to the underlying
* metrics will be reflected in the returned map.
*
* @return An unmodifiable live view of the map of metrics currently maintained by the consumer
Copy link
Contributor

Choose a reason for hiding this comment

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

ShareConsumerImpl.metrics() uses Map.copyOf(). unmodifiable live view looks incorrect here.

Copy link
Member

Choose a reason for hiding this comment

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

we've reverted the change to use Collections.unmodifiableMap. see #21832

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks @chia7712.

acquireAndEnsureOpen();
try {
return Collections.unmodifiableSet(this.subscriptions.assignedPartitions());
return Set.copyOf(this.subscriptions.assignedPartitions());
Copy link
Contributor

Choose a reason for hiding this comment

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

assignment() and subscription() should both follow the same snapshot rule. any reason for not changing subscription() here.

Copy link
Contributor

Choose a reason for hiding this comment

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

@chia7712 comment applies here too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clients consumer small Small PRs triage PRs from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants