diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Cache.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Cache.scala index c8b90dc9e901..dbf29e147a16 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Cache.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Cache.scala @@ -5,14 +5,14 @@ package com.daml.ledger.participant.state.kvutils.caching import com.github.benmanes.caffeine.{cache => caffeine} -import scala.collection.JavaConverters._ +import scala.compat.java8.OptionConverters._ trait Cache[Key, Value] { def get(key: Key, acquire: Key => Value): Value def size: Size - private[caching] def entries: Iterable[(Key, Value)] + def weight: Size } object Cache { @@ -29,26 +29,28 @@ object Cache { caffeine.Caffeine .newBuilder() .maximumWeight(maximumWeight) - .weigher[Key, Value](Weight.weigher) + .weigher(Weight.weigher[Key, Value]) .build[Key, Value]()) } - class NoCache[Key, Value] extends Cache[Key, Value] { + final class NoCache[Key, Value] extends Cache[Key, Value] { override def get(key: Key, acquire: Key => Value): Value = acquire(key) - override def size: Size = 0 + override val size: Size = 0 - override private[caching] def entries: Iterable[(Key, Value)] = Iterable.empty + override val weight: Size = 0 } - class CaffeineCache[Key, Value](val cache: caffeine.Cache[Key, Value]) extends Cache[Key, Value] { + final class CaffeineCache[Key, Value](val cache: caffeine.Cache[Key, Value]) + extends Cache[Key, Value] { override def get(key: Key, acquire: Key => Value): Value = cache.get(key, key => acquire(key)) override def size: Size = cache.estimatedSize() - override private[caching] def entries: Iterable[(Key, Value)] = - cache.asMap().asScala + override def weight: Size = + cache.policy().eviction().asScala.flatMap(_.weightedSize().asScala).getOrElse(0) } + } diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Weight.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Weight.scala index c4e5025b9b52..09053234743c 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Weight.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/caching/Weight.scala @@ -21,12 +21,6 @@ object Weight { def weigher[Key: Weight, Value: Weight]: caffeine.Weigher[Key, Value] = new WeightWeigher[Key, Value] - def ofCache[Key, Value](cache: Cache[Key, Value])( - implicit keyWeight: Weight[Key], - valueWeight: Weight[Value], - ): Size = - cache.entries.map { case (key, value) => keyWeight.weigh(key) + valueWeight.weigh(value) }.sum - implicit object `Bytes Weight` extends Weight[Bytes] { override def weigh(value: Bytes): Size = value.size().toLong diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/SubmissionValidator.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/SubmissionValidator.scala index 9d0426093c78..2a46965ef25b 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/SubmissionValidator.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/validator/SubmissionValidator.scala @@ -10,7 +10,7 @@ import com.codahale.metrics.{MetricRegistry, Timer} import com.daml.ledger.participant.state.kvutils import com.daml.ledger.participant.state.kvutils.DamlKvutils._ import com.daml.ledger.participant.state.kvutils.api.LedgerReader -import com.daml.ledger.participant.state.kvutils.caching.{Cache, Weight} +import com.daml.ledger.participant.state.kvutils.caching.Cache import com.daml.ledger.participant.state.kvutils.{Bytes, Envelope, KeyValueCommitting} import com.daml.ledger.participant.state.metrics.MetricName import com.daml.ledger.participant.state.metrics.Metrics.timedFuture @@ -302,16 +302,8 @@ class SubmissionValidator[LogResult] private[validator] ( val transformSubmission: Timer = metricRegistry.timer(prefix :+ "transform_submission") private val stateValueCachePrefix: MetricName = prefix :+ "state_value_cache" - metricRegistry.gauge( - stateValueCachePrefix :+ "size", - () => - () => { - println(s"State value cache size = ${stateValueCache.size}") - stateValueCache.size - }) - metricRegistry.gauge( - stateValueCachePrefix :+ "weight", - () => () => Weight.ofCache(stateValueCache)) + metricRegistry.gauge(stateValueCachePrefix :+ "size", () => () => stateValueCache.size) + metricRegistry.gauge(stateValueCachePrefix :+ "weight", () => () => stateValueCache.weight) } }