Skip to content

Commit a96a353

Browse files
dpsofthughsimpson
andauthored
AtomicGetOrElseUpdateOnTrieMap::atomicGetOrElseUpdate -> TrieMap::getOrElseUpdate (#591)
* remove AtomicGetOrElseUpdateOnTrieMap::atomicGetOrElseUpdate method in favor of TrieMap::getOrElseUpdate * fix rebase bugs --------- Co-authored-by: Hugh Simpson <[email protected]>
1 parent d2db875 commit a96a353

File tree

10 files changed

+18
-22
lines changed

10 files changed

+18
-22
lines changed

core/kamon-core/src/main/scala/kamon/Utilities.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ trait Utilities { self: Configuration =>
4646
* empty.
4747
*/
4848
def filter(configKey: String): Filter =
49-
_filters.atomicGetOrElseUpdate(configKey, Filter.from(configKey))
49+
_filters.getOrElseUpdate(configKey, Filter.from(configKey))
5050

5151
/**
5252
* Kamon's Clock implementation.

core/kamon-core/src/main/scala/kamon/metric/MetricRegistry.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class MetricRegistry(config: Config, clock: Clock) {
5050
Metric.Counter = {
5151

5252
val metric = validateInstrumentType[Metric.Counter] {
53-
_metrics.atomicGetOrElseUpdate(name, _factory.counter(name, description, unit, autoUpdateInterval))
53+
_metrics.getOrElseUpdate(name, _factory.counter(name, description, unit, autoUpdateInterval))
5454
} (name, Instrument.Type.Counter)
5555

5656
checkDescription(metric.name, metric.description, description)
@@ -66,7 +66,7 @@ class MetricRegistry(config: Config, clock: Clock) {
6666
Metric.Gauge = {
6767

6868
val metric = validateInstrumentType[Metric.Gauge] {
69-
_metrics.atomicGetOrElseUpdate(name, _factory.gauge(name, description, unit, autoUpdateInterval))
69+
_metrics.getOrElseUpdate(name, _factory.gauge(name, description, unit, autoUpdateInterval))
7070
} (name, Instrument.Type.Gauge)
7171

7272
checkDescription(metric.name, metric.description, description)
@@ -82,7 +82,7 @@ class MetricRegistry(config: Config, clock: Clock) {
8282
autoUpdateInterval: Option[Duration]): Metric.Histogram = {
8383

8484
val metric = validateInstrumentType[Metric.Histogram] {
85-
_metrics.atomicGetOrElseUpdate(name, _factory.histogram(name, description, unit, dynamicRange, autoUpdateInterval))
85+
_metrics.getOrElseUpdate(name, _factory.histogram(name, description, unit, dynamicRange, autoUpdateInterval))
8686
} (name, Instrument.Type.Histogram)
8787

8888
checkDescription(metric.name, metric.description, description)
@@ -98,7 +98,7 @@ class MetricRegistry(config: Config, clock: Clock) {
9898
def timer(name: String, description: Option[String], dynamicRange: Option[DynamicRange], autoUpdateInterval: Option[Duration]): Metric.Timer = {
9999

100100
val metric = validateInstrumentType[Metric.Timer] {
101-
_metrics.atomicGetOrElseUpdate(name, _factory.timer(name, description, Some(MeasurementUnit.time.nanoseconds),
101+
_metrics.getOrElseUpdate(name, _factory.timer(name, description, Some(MeasurementUnit.time.nanoseconds),
102102
dynamicRange, autoUpdateInterval))
103103
} (name, Instrument.Type.Timer)
104104

@@ -115,7 +115,7 @@ class MetricRegistry(config: Config, clock: Clock) {
115115
autoUpdateInterval: Option[Duration]): Metric.RangeSampler = {
116116

117117
val metric = validateInstrumentType[Metric.RangeSampler] {
118-
_metrics.atomicGetOrElseUpdate(name, _factory.rangeSampler(name, description, unit, dynamicRange, autoUpdateInterval))
118+
_metrics.getOrElseUpdate(name, _factory.rangeSampler(name, description, unit, dynamicRange, autoUpdateInterval))
119119
} (name, Instrument.Type.RangeSampler)
120120

121121
checkDescription(metric.name, metric.description, description)

core/kamon-core/src/main/scala/kamon/package.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,10 @@ package object kamon {
6464

6565

6666
/**
67-
* Workaround to the non thread-safe [scala.collection.concurrent.TrieMap#getOrElseUpdate()] method. More details on
68-
* why this is necessary can be found at [[https://issues.scala-lang.org/browse/SI-7943]].
67+
* Atomic variant of [scala.collection.concurrent.TrieMap#getOrElseUpdate()] method with cleanup and init functions.
6968
*/
7069
implicit class AtomicGetOrElseUpdateOnTrieMap[K, V](val trieMap: TrieMap[K, V]) extends AnyVal {
7170

72-
def atomicGetOrElseUpdate(key: K, op: => V): V =
73-
atomicGetOrElseUpdate(key, op, { _: V => () }, { _: V => () })
74-
7571
def atomicGetOrElseUpdate(key: K, op: => V, cleanup: V => Unit, init: V => Unit): V =
7672
trieMap.get(key) match {
7773
case Some(v) => v

core/kamon-core/src/main/scala/kamon/trace/AdaptiveSampler.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ class AdaptiveSampler extends Sampler {
4848

4949
override def decide(operation: Sampler.Operation): SamplingDecision = {
5050
val operationName = operation.operationName()
51-
val operationSampler = _samplers.get(operationName).getOrElse {
51+
val operationSampler = _samplers.getOrElse(operationName, {
5252
// It might happen that the first time we see an operation under high concurrent throughput we will reach this
5353
// block more than once, but worse case effect is that we will rebalance the operation samplers more than once.
54-
val sampler = _samplers.atomicGetOrElseUpdate(operationName, buildOperationSampler(operationName))
54+
val sampler = _samplers.getOrElseUpdate(operationName, buildOperationSampler(operationName))
5555
rebalance()
5656
sampler
57-
}
57+
})
5858

5959
val decision = operationSampler.decide()
6060
if(decision == SamplingDecision.Sample)

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/AkkaClusterShardingMetrics.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ object AkkaClusterShardingMetrics {
5050
private val _shardTelemetry = ShardingInstruments.shardTelemetry(system, typeName, shardHostedEntities, shardProcessedMessages)
5151

5252
def hostedEntitiesPerShardCounter(shardID: String): AtomicLong =
53-
_shardTelemetry.entitiesPerShard.atomicGetOrElseUpdate(shardID, new AtomicLong())
53+
_shardTelemetry.entitiesPerShard.getOrElseUpdate(shardID, new AtomicLong())
5454

5555
def processedMessagesPerShardCounter(shardID: String): AtomicLong =
56-
_shardTelemetry.messagesPerShard.atomicGetOrElseUpdate(shardID, new AtomicLong())
56+
_shardTelemetry.messagesPerShard.getOrElseUpdate(shardID, new AtomicLong())
5757

5858
// We should only remove when the ShardRegion actor is terminated.
5959
override def remove(): Unit = {

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/AkkaMetrics.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ object AkkaMetrics {
181181
)
182182

183183
def forSystem(name: String): ActorSystemInstruments =
184-
_systemInstrumentsCache.atomicGetOrElseUpdate(name, new ActorSystemInstruments(TagSet.of("system", name)))
184+
_systemInstrumentsCache.getOrElseUpdate(name, new ActorSystemInstruments(TagSet.of("system", name)))
185185

186186
class ActorSystemInstruments(tags: TagSet) extends InstrumentGroup(tags) {
187187
val deadLetters = register(SystemDeadLetters)

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/AkkaRemoteMetrics.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ object AkkaRemoteMetrics {
4141
}
4242

4343
def serializationInstruments(system: String): SerializationInstruments =
44-
_serializationInstrumentsCache.atomicGetOrElseUpdate(system, new SerializationInstruments(system))
44+
_serializationInstrumentsCache.getOrElseUpdate(system, new SerializationInstruments(system))
4545
}

instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/PekkoClusterShardingMetrics.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ object PekkoClusterShardingMetrics {
5050
private val _shardTelemetry = ShardingInstruments.shardTelemetry(system, typeName, shardHostedEntities, shardProcessedMessages)
5151

5252
def hostedEntitiesPerShardCounter(shardID: String): AtomicLong =
53-
_shardTelemetry.entitiesPerShard.atomicGetOrElseUpdate(shardID, new AtomicLong())
53+
_shardTelemetry.entitiesPerShard.getOrElseUpdate(shardID, new AtomicLong())
5454

5555
def processedMessagesPerShardCounter(shardID: String): AtomicLong =
56-
_shardTelemetry.messagesPerShard.atomicGetOrElseUpdate(shardID, new AtomicLong())
56+
_shardTelemetry.messagesPerShard.getOrElseUpdate(shardID, new AtomicLong())
5757

5858
// We should only remove when the ShardRegion actor is terminated.
5959
override def remove(): Unit = {

instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/PekkoMetrics.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ object PekkoMetrics {
181181
)
182182

183183
def forSystem(name: String): ActorSystemInstruments =
184-
_systemInstrumentsCache.atomicGetOrElseUpdate(name, new ActorSystemInstruments(TagSet.of("system", name)))
184+
_systemInstrumentsCache.getOrElseUpdate(name, new ActorSystemInstruments(TagSet.of("system", name)))
185185

186186
class ActorSystemInstruments(tags: TagSet) extends InstrumentGroup(tags) {
187187
val deadLetters = register(SystemDeadLetters)

instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/PekkoRemoteMetrics.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ object PekkoRemoteMetrics {
4141
}
4242

4343
def serializationInstruments(system: String): SerializationInstruments =
44-
_serializationInstrumentsCache.atomicGetOrElseUpdate(system, new SerializationInstruments(system))
44+
_serializationInstrumentsCache.getOrElseUpdate(system, new SerializationInstruments(system))
4545
}

0 commit comments

Comments
 (0)