Skip to content

Commit

Permalink
kvutils: Simplify calculating the weight of a Caffeine cache. (#5461)
Browse files Browse the repository at this point in the history
* kvutils: Simplify calculating the weight of a Caffeine cache.

And remove an errant `println` that slipped through the cracks.

Thank you to @ben-manes for the tip!

CHANGELOG_BEGIN
CHANGELOG_END

* kvutils: Make classes final and defs into vals.

Co-Authored-By: Stefano Baghino <stefano.baghino@digitalasset.com>

Co-authored-by: Stefano Baghino <stefano.baghino@digitalasset.com>
  • Loading branch information
SamirTalwar and stefanobaghino-da authored Apr 7, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent b387660 commit 10bac14
Showing 3 changed files with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}

}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 10bac14

Please sign in to comment.