Skip to content

Commit

Permalink
Changed SingleDimensionCacheStats to use ConcurrentMap
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
  • Loading branch information
Peter Alfonsi committed Feb 14, 2024
1 parent 01e04a8 commit 3777e3f
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* A CacheStats implementation for caches that aggregate over a single dimension.
* For example, caches in the IndicesRequestCache only aggregate over ShardId value.
*/
public class SingleDimensionCacheStats implements CacheStats {
// Maintain a counter metric for each shard id (dimension values)
private final Map<String, CounterMetric> hitsMap;
private final Map<String, CounterMetric> missesMap;
private final Map<String, CounterMetric> evictionsMap;
private final Map<String, CounterMetric> memorySizeMap;
private final Map<String, CounterMetric> entriesMap;
private final ConcurrentMap<String, CounterMetric> hitsMap;
private final ConcurrentMap<String, CounterMetric> missesMap;
private final ConcurrentMap<String, CounterMetric> evictionsMap;
private final ConcurrentMap<String, CounterMetric> memorySizeMap;
private final ConcurrentMap<String, CounterMetric> entriesMap;

// Also maintain a single total counter metric, to avoid having to sum over many values for shards
private final CounterMetric totalHits;
Expand All @@ -41,11 +43,11 @@ public class SingleDimensionCacheStats implements CacheStats {
private final String allowedDimensionName;

public SingleDimensionCacheStats(String allowedDimensionName) {
this.hitsMap = new HashMap<>();
this.missesMap = new HashMap<>();
this.evictionsMap = new HashMap<>();
this.memorySizeMap = new HashMap<>();
this.entriesMap = new HashMap<>();
this.hitsMap = new ConcurrentHashMap<>();
this.missesMap = new ConcurrentHashMap<>();
this.evictionsMap = new ConcurrentHashMap<>();
this.memorySizeMap = new ConcurrentHashMap<>();
this.entriesMap = new ConcurrentHashMap<>();

this.totalHits = new CounterMetric();
this.totalMisses = new CounterMetric();
Expand Down Expand Up @@ -212,8 +214,8 @@ private Map<String, Long> convertCounterMapToLong(Map<String, CounterMetric> inp
return result;
}

private Map<String, CounterMetric> convertLongMapToCounterMetric(Map<String, Long> inputMap) {
Map<String, CounterMetric> result = new HashMap<>();
private ConcurrentMap<String, CounterMetric> convertLongMapToCounterMetric(Map<String, Long> inputMap) {
ConcurrentMap<String, CounterMetric> result = new ConcurrentHashMap<>();
for (String key: inputMap.keySet()) {
CounterMetric counter = new CounterMetric();
counter.inc(inputMap.get(key));
Expand Down

0 comments on commit 3777e3f

Please sign in to comment.