You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@kafka.apache.org by "Guozhang Wang (Jira)" <ji...@apache.org> on 2022/07/14 21:34:00 UTC

[jira] [Resolved] (KAFKA-13846) Add an overloaded metricOrElseCreate function in Metrics

     [ https://issues.apache.org/jira/browse/KAFKA-13846?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Guozhang Wang resolved KAFKA-13846.
-----------------------------------
    Fix Version/s: 3.3.0
       Resolution: Fixed

> Add an overloaded metricOrElseCreate function in Metrics
> --------------------------------------------------------
>
>                 Key: KAFKA-13846
>                 URL: https://issues.apache.org/jira/browse/KAFKA-13846
>             Project: Kafka
>          Issue Type: Improvement
>          Components: metrics
>            Reporter: Guozhang Wang
>            Assignee: Sagar Rao
>            Priority: Major
>              Labels: newbie
>             Fix For: 3.3.0
>
>
> The `Metrics` registry is often used by concurrent threads, however it's get/create APIs are not well suited for it. A common pattern from the user today is:
> {code}
> metric = metrics.metric(metricName);
> if (metric == null) {
>   try {
>     metrics.createMetric(..)
>   } catch (IllegalArgumentException e){
>     // another thread may create the metric at the mean time
>   }
> } 
> {code}
> Otherwise the caller would need to synchronize the whole block trying to get the metric. However, the `createMetric` function call itself indeed synchronize internally on updating the metric map.
> So we could consider adding a metricOrElseCreate function which is similar to createMetric, but instead of throwing an illegal argument exception within the internal synchronization block, it would just return the already existing metric.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)