Skip to content
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

KAFKA-12469: Deprecated and corrected topic metrics for consumer (KIP-1109) #18232

Merged
merged 7 commits into from
Dec 31, 2024

Conversation

apoorvmittal10
Copy link
Collaborator

The PR implements the behaviour defined in KIP-1109. It corrects the consumer topic and topic-partition metrics, while deprecating the incorrect ones.

https://cwiki.apache.org/confluence/display/KAFKA/KIP-1109%3A+Unifying+Kafka+Consumer+Topic+Metrics

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

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

@apoorvmittal10 : Thanks for the PR. Left a few comments.

// Remove deprecated metrics.
metrics.removeSensor(deprecatedMetricName(partitionRecordsLagMetricName(tp)));
metrics.removeSensor(deprecatedMetricName(partitionRecordsLeadMetricName(tp)));
metrics.removeMetric(partitionPreferredReadReplicaMetricNameDeprecated(tp));
Copy link
Contributor

Choose a reason for hiding this comment

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

partitionPreferredReadReplicaMetricNameDeprecated => deprecatedPartitionPreferredReadReplicaMetricName to be consistent with other names?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

metricsManager.recordBytesFetched(topicName2, 1);
// Another 8 metrics gets registered as deprecated metrics should be reported for topicName2.
Copy link
Contributor

Choose a reason for hiding this comment

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

gets => get

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

// 5 new metrics shall be registered.
assertEquals(5, metrics.metrics().size() - additionalRegisteredMetricsSize);

// Remove 1 topic which has deprecated metrics as well.
Copy link
Contributor

Choose a reason for hiding this comment

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

1 topic => 1 partition

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@@ -181,6 +195,67 @@ void maybeUpdateAssignment(SubscriptionState subscription) {
}
}

@Deprecated // To be removed in Kafka 5.0 release.
private void maybeRecordDeprecatedBytesFetched(String name, String topic, int bytes) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be useful to follow the approach used in https://github.com/apache/kafka/pull/11302/files to add "deprecated" in the description of the metric name and update the ops doc about the deprecation.

Copy link
Collaborator Author

@apoorvmittal10 apoorvmittal10 Dec 19, 2024

Choose a reason for hiding this comment

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

As the metrics with a specfic tag has been deprectaed hence it's hard to specify same metric as deprecated. Hence I have added Note to consumer topic metrics. For consumer metrics ops need not to ne updated as ops.html uses metrics description to generate details on kafka-site, I have verified that the added Note can be viewed on kafka-site locally. Please let me know if you think it should be handled differently.

Copy link
Member

Choose a reason for hiding this comment

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

Generally speaking, we should always include a note explaining the deprecation. This should include the first version it was deprecated, when it's expected to be removed and what should be used instead.

Copy link
Collaborator Author

@apoorvmittal10 apoorvmittal10 Jan 2, 2025

Choose a reason for hiding this comment

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

Sure, so currently below Note has been added to metrics.

Current:

"Note: For topic names with periods (.), an additional metric with underscores is emitted.
However, the periods replaced metric is deprecated.
Please use the metric with actual topic name instead."

Example:

Screenshot 2025-01-02 at 11 21 27

Should we change it to:

"Note: For topic names with periods (.), an additional metric with underscores is emitted.
However, the periods replaced metric is deprecated since Kafka 4.1 and shall be removed in Kafka 5.0.
Please use the metric with actual topic name instead."

wdyt?

@github-actions github-actions bot removed the triage PRs from the community label Dec 17, 2024
Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@apoorvmittal10 thanks for this patch!

.build();
maybeRecordDeprecatedPartitionLag(name, tp, lag);

Sensor recordsLag = new SensorBuilder(metrics, name, () -> Map.of("topic", tp.topic(), "partition", String.valueOf(tp.partition())))
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could you please consider placing "topic" before "partition"? See the following screenshot:
Screenshot From 2024-12-18 17-46-17

That can be addressed by using Utils.mkMap

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Make sense, I earlier considered it but didn't do as previous tags were not ordered. I have added the change.

metricsManager.recordPartitionLag(tp3, 4);
metricsManager.recordPartitionLead(tp3, 2);

int additionalRegisteredMetricsSize = metrics.metrics().size();
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be sensible to assert the number of additional registered metrics too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I haven't asserted them on purpose as above tests already validated them.

metricsManager.maybeUpdateAssignment(subscriptionState);
// For tp2, 14 metrics will be unregistered. 3 for partition lag, 3 for partition lead, 1 for
// preferred read replica and similarly 7 deprecated metrics.
assertEquals(-9, metrics.metrics().size() - additionalRegisteredMetricsSize);
Copy link
Member

Choose a reason for hiding this comment

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

This -9 is needlessly obscure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have added a comment and toggled the check, please let me know if it's better now.


import static org.apache.kafka.clients.consumer.internals.FetchMetricsManager.topicPartitionTags;
import static org.apache.kafka.clients.consumer.internals.FetchMetricsManager.topicTags;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FetchMetricsManagerTest {
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I would put @SuppressWarnings("deprecation") on this class because it is knowingly using deprecated methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

I have a suspicion you put the suppression on FetchMetricsManager when I meant to put it on this test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have corrected it, also I have placed the suppression on individual tests so they can be removed while removing deprecated code.

Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

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

@apoorvmittal10 : Thanks for the updated PR. One more comment. Also, is the test failure related to this PR?

.build();
maybeRecordDeprecatedPartitionLag(name, tp, lag);

Sensor recordsLag = new SensorBuilder(metrics, name, () -> mkMap(mkEntry("topic", tp.topic()), mkEntry("partition", String.valueOf(tp.partition()))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we do the same for the deprecated metrics too for better consistency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why you're still using the Utils.mkMap methods instead of Map.of from Java 9+?

Copy link
Member

Choose a reason for hiding this comment

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

@ijuma please see my previous comment #18232 (comment)

Copy link
Member

@ijuma ijuma Jan 1, 2025

Choose a reason for hiding this comment

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

Hmm, if we want insertion ordering to be preserved, we should change SensorBuilder to use a LinkedHashMap (SequencedMap would be better, but that requires Java 21). Having this implicit requirement and hoping people get it right is very error prone.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah agree a explicit requirement to send an ordered map would be better in builder itself.

Copy link
Member

Choose a reason for hiding this comment

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

I have opened https://issues.apache.org/jira/browse/KAFKA-18390 to address @ijuma's comment. The fix is targeted for version 4.1.0, as it may introduce significant changes to the codebase.

@apoorvmittal10
Copy link
Collaborator Author

Also, is the test failure related to this PR?

It seems to be build scan publish failure. I ll keep an eye on current ongoing build to see if it passes.

Copy link
Contributor

@junrao junrao left a comment

Choose a reason for hiding this comment

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

@apoorvmittal10 : Thanks for the updated PR. LGTM. I will wait to see if other reviewers have more comments.

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

LGTM

}
}

@Deprecated // To be removed in Kafka 5.0 release.
Copy link
Member

Choose a reason for hiding this comment

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

Could you please open the jira to ensure we will remove them from 5.0 :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@apoorvmittal10
Copy link
Collaborator Author

@AndrewJSchofield Please let us know if it's good to merge?

Copy link
Member

@AndrewJSchofield AndrewJSchofield left a comment

Choose a reason for hiding this comment

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

Just one final comment to do with suppression of deprecation warnings. Apart from that, this looks good to me.

/**
* The {@link FetchMetricsManager} class provides wrapper methods to record lag, lead, latency, and fetch metrics.
* It keeps an internal ID of the assigned set of partitions which is updated to ensure the set of metrics it
* records matches up with the topic-partitions in use.
*/
@SuppressWarnings("deprecation")
Copy link
Member

Choose a reason for hiding this comment

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

I don't believe this suppression has any value because it's implementing deprecated code, as opposed to using it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes you are right, I have corrected it.


import static org.apache.kafka.clients.consumer.internals.FetchMetricsManager.topicPartitionTags;
import static org.apache.kafka.clients.consumer.internals.FetchMetricsManager.topicTags;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FetchMetricsManagerTest {
Copy link
Member

Choose a reason for hiding this comment

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

I have a suspicion you put the suppression on FetchMetricsManager when I meant to put it on this test.

@AndrewJSchofield AndrewJSchofield merged commit f88cf57 into apache:trunk Dec 31, 2024
8 of 9 checks passed
return this.metrics.metricInstance(metricsRegistry.partitionPreferredReadReplica, metricTags);
}

@Deprecated
Copy link
Member

Choose a reason for hiding this comment

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

We should never deprecate something without including a @deprecated javadoc with the details I outlined in a separate comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I have added details here #18232 (comment), please let me know your thoughts.

airlock-confluentinc bot pushed a commit to confluentinc/kafka that referenced this pull request Jan 3, 2025
…-1109) (apache#18232)

The PR implements the behaviour defined in KIP-1109. It corrects the consumer topic and topic-partition metrics, while deprecating the incorrect ones.

Reviewers: Chia-Ping Tsai <[email protected]>, Jun Rao <[email protected]>, Andrew Schofield <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants