Skip to content

Commit

Permalink
[METRICS] Remove unnecessary Guava Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pan3793 committed May 13, 2021
1 parent e77ea13 commit a8d607b
Showing 1 changed file with 11 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
package org.apache.kyuubi.metrics

import java.lang.management.ManagementFactory
import java.util.concurrent.locks.ReentrantLock

import com.codahale.metrics.{Counter, Gauge, MetricRegistry}
import com.codahale.metrics.jvm.{BufferPoolMetricSet, GarbageCollectorMetricSet, MemoryUsageGaugeSet, ThreadStatesGaugeSet}
import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache}
import com.codahale.metrics.{Gauge, MetricRegistry}
import com.codahale.metrics.jvm._

import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.metrics.MetricsConf.METRICS_REPORTERS
Expand All @@ -33,29 +31,17 @@ import org.apache.kyuubi.service.CompositeService
class MetricsSystem extends CompositeService("MetricsSystem") {

private val registry = new MetricRegistry
private var counters: LoadingCache[String, Counter] = _
private val countersLock = new ReentrantLock

def incAndGetCount(key: String): Long = {
try {
countersLock.lock()
val counter = counters.get(key)
counter.inc(1L)
counter.getCount
} finally {
countersLock.unlock()
}

def incAndGetCount(key: String): Long = synchronized {
val counter = registry.counter(key)
counter.inc(1L)
counter.getCount
}

def decAndGetCount(key: String): Long = {
try {
countersLock.lock()
val counter = counters.get(key)
counter.dec(1L)
counter.getCount
} finally {
countersLock.unlock()
}
def decAndGetCount(key: String): Long = synchronized {
val counter = registry.counter(key)
counter.dec(1L)
counter.getCount
}

def registerGauge[T](name: String, value: => T, default: T): Unit = {
Expand All @@ -70,12 +56,6 @@ class MetricsSystem extends CompositeService("MetricsSystem") {
registry.registerAll(new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer))
registry.registerAll(new ThreadStatesGaugeSet)

counters = CacheBuilder.newBuilder().build[String, Counter](
new CacheLoader[String, Counter] {
override def load(key: String): Counter = registry.counter(key)
}
)

conf.get(METRICS_REPORTERS).map(ReporterType.withName).foreach {
case JSON => addService(new JsonReporterService(registry))
case SLF4J => addService(new Slf4jReporterService(registry))
Expand Down

0 comments on commit a8d607b

Please sign in to comment.