Move tryPut to ChronicleMapStore Inserting a new entry into chronicle-map is a store level operation and as such belongs to the ChronicleMapStore rather than the ChronicleMapCacheImpl. Also, this allows to emit metrics related to failed put operation, which will be addressed in a follow up change. Change-Id: I812d8d930e096c1757925dbe304d1c2b180ddabc
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheImpl.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheImpl.java index 4d7a343..b704a7b 100644 --- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheImpl.java +++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheImpl.java
@@ -257,7 +257,7 @@ public void putUnchecked(Object key, Object value, Timestamp created) { TimedValue<?> wrappedValue = new TimedValue<>(value, created.toInstant().toEpochMilli()); KeyWrapper<?> wrappedKey = new KeyWrapper<>(key); - if (tryPut(store, (KeyWrapper<K>) wrappedKey, (TimedValue<V>) wrappedValue)) { + if (store.tryPut((KeyWrapper<K>) wrappedKey, (TimedValue<V>) wrappedValue)) { mem.put((K) key, (TimedValue<V>) wrappedValue); } } @@ -274,7 +274,7 @@ */ @SuppressWarnings("unchecked") public void putUnchecked(KeyWrapper<Object> wrappedKey, TimedValue<Object> wrappedValue) { - if (tryPut(store, (KeyWrapper<K>) wrappedKey, (TimedValue<V>) wrappedValue)) { + if (store.tryPut((KeyWrapper<K>) wrappedKey, (TimedValue<V>) wrappedValue)) { mem.put((K) wrappedKey.getValue(), (TimedValue<V>) wrappedValue); } } @@ -289,39 +289,13 @@ boolean putTimedToStore(K key, TimedValue<V> timedVal) { KeyWrapper<K> wrappedKey = new KeyWrapper<>(key); - boolean putSuccess = tryPut(store, wrappedKey, timedVal); + boolean putSuccess = store.tryPut(wrappedKey, timedVal); if (putSuccess) { hotEntries.add(key); } return putSuccess; } - /** - * Attempt to put the key/value pair into the chronicle-map cache. Also catches and warns on disk - * allocation errors, so that such failures result in non-cached entries rather than throwing. - * - * @param store the chronicle-map store - * @param wrappedKey the wrapped key value - * @param timedVal the timed value - * @param <K> the type of the wrapped key - * @param <V> the type of the timed value - * @return true when the value was successfully inserted in chronicle-map, false otherwise - */ - static <K, V> boolean tryPut( - ChronicleMap<KeyWrapper<K>, TimedValue<V>> store, - KeyWrapper<K> wrappedKey, - TimedValue<V> timedVal) { - try { - store.put(wrappedKey, timedVal); - } catch (IllegalArgumentException | IllegalStateException e) { - logger.atWarning().withCause(e).log( - "[cache %s] Caught exception when inserting entry '%s' in chronicle-map", - store.name(), wrappedKey.getValue()); - return false; - } - return true; - } - public void prune() { if (!config.getExpireAfterWrite().isZero()) { store.forEachEntry(
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheLoader.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheLoader.java index 33d970f..9a68780 100644 --- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheLoader.java +++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheLoader.java
@@ -14,8 +14,6 @@ package com.googlesource.gerrit.modules.cache.chroniclemap; -import static com.googlesource.gerrit.modules.cache.chroniclemap.ChronicleMapCacheImpl.tryPut; - import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheStats; import com.google.common.flogger.FluentLogger; @@ -101,7 +99,7 @@ // Note that we return a loadedValue, even when we // we fail populating the cache with it, to make clients more // resilient to storage cache failures - if (tryPut(store, new KeyWrapper<>(key), loadedValue)) { + if (store.tryPut(new KeyWrapper<>(key), loadedValue)) { loadSuccessCount.increment(); } }); @@ -143,7 +141,7 @@ new FutureCallback<V>() { @Override public void onSuccess(V result) { - if (tryPut(store, new KeyWrapper<>(key), new TimedValue<>(result))) { + if (store.tryPut(new KeyWrapper<>(key), new TimedValue<>(result))) { loadSuccessCount.increment(); } totalLoadTime.add(System.nanoTime() - start); @@ -186,7 +184,7 @@ @Override public void put(K key, TimedValue<V> value) { - tryPut(store, new KeyWrapper<>(key), value); + store.tryPut(new KeyWrapper<>(key), value); } @Override
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapStore.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapStore.java index 5161cd0..1f829bb 100644 --- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapStore.java +++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapStore.java
@@ -53,6 +53,26 @@ metrics.registerCallBackMetrics(this); } + /** + * Attempt to put the key/value pair into the chronicle-map store. Also catches and warns on disk + * allocation errors, so that such failures result in non-cached entries rather than throwing. + * + * @param wrappedKey the wrapped key value + * @param timedVal the timed value + * @return true when the value was successfully inserted in chronicle-map, false otherwise + */ + public boolean tryPut(KeyWrapper<K> wrappedKey, TimedValue<V> timedVal) { + try { + store.put(wrappedKey, timedVal); + } catch (IllegalArgumentException | IllegalStateException e) { + logger.atWarning().withCause(e).log( + "[cache %s] Caught exception when inserting entry '%s' in chronicle-map", + store.name(), wrappedKey.getValue()); + return false; + } + return true; + } + @SuppressWarnings("rawtypes") public double percentageUsedAutoResizes() { /*