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-17016: Align the behavior of GaugeWrapper and MeterWrapper #16426

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

Conversation

FrankYang0529
Copy link
Member

When using MeterWrapper, it initializes Meter again after users close it.
However, the GaugeWrapper behavior is different. We use aggregatedMetric directly.
If GaugeWrapper is closed, we don't have chance to initialize it again.

Committer Checklist (excluded from commit message)

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

@FrankYang0529 FrankYang0529 marked this pull request as draft June 23, 2024 05:55
@FrankYang0529 FrankYang0529 marked this pull request as ready for review June 23, 2024 13:09
Copy link
Contributor

@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.

@FrankYang0529 thanks for this patch!

core/src/main/scala/kafka/server/KafkaRequestHandler.scala Outdated Show resolved Hide resolved
Copy link
Contributor

@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.

@FrankYang0529 thanks for your patch

}
return gaugeObject
return metric
}

def close(): Unit = gaugeLock synchronized {
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems the lock is used to make sure we don't call removeMetric/newGauge repeatedly, so we can do a bit refactor for it. WDYT?

  case class GaugeWrapper(metricType: String) {
    private final val removed = new AtomicBoolean(false)
    // The map to store:
    //   - per-partition value for topic-level metrics. The key will be the partition number
    //   - per-topic value for broker-level metrics. The key will be the topic name
    private val metricValues = new ConcurrentHashMap[String, Long]()

    def setValue(key: String, value: Long): Unit = {
      newGaugeIfNeed()
      metricValues.put(key, value)
    }

    def removeKey(key: String): Unit = {
      newGaugeIfNeed()
      metricValues.remove(key)
    }

    def close(): Unit = if (removed.compareAndSet(true, false)) {
      metricsGroup.removeMetric(metricType, tags)
      metricValues.clear()
    }

    private def newGaugeIfNeed(): Unit = if (removed.compareAndSet(false, true)) {
      metricsGroup.newGauge(metricType, () => metricValues.values().stream().mapToLong(v => v).sum(), tags)
    }

    newGaugeIfNeed()
  }

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the great suggestion. Updated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants