Merge branch 'stable-3.5' into stable-3.6

* stable-3.5:
  Provide default configuration for Gerrit persistent caches
  Introduce metric for caches that fall back to default config
  Add test that detects persistent caches without defaults
  Remove metrics when cache gets closed
  Use static, per-thread buffers for (de)serialization
  Add read/write metrics to TimedValueMarshaller
  ChronicleMapCacheIT: remove Truth8 qualifier from assertThat
  *Marshaller: get CacheSerilizer only once
  Persist caches keys index at configurable pace
  Add restore/persist operations related metrics
  Persist cache keys index to a file
  Add cache keys index metrics
  Avoid full cache scanning for pruning

Change-Id: I514885b07f721ed67c8643e80289e004b21233f0
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheConfig.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheConfig.java
index e2c5afe..37d2845 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheConfig.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheConfig.java
@@ -264,7 +264,6 @@
     static final ImmutableMap<String, DefaultConfig> defaultMap =
         new ImmutableMap.Builder<String, DefaultConfig>()
             .put("accounts", DefaultConfig.create(30, 256, 1000, 1))
-            .put("approvals", DefaultConfig.create(103, 365, 1000, 3))
             .put("change_kind", DefaultConfig.create(59, 26, 1000, 1))
             .put("change_notes", DefaultConfig.create(36, 10240, 1000, 3))
             .put("comment_context", DefaultConfig.create(80, 662, 2000, 3))
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java
index e4474b0..27be43f 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java
@@ -21,7 +21,6 @@
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.extensions.registration.DynamicMap;
 import com.google.gerrit.metrics.MetricMaker;
-import com.google.gerrit.server.cache.CacheBackend;
 import com.google.gerrit.server.cache.MemoryCacheFactory;
 import com.google.gerrit.server.cache.PersistentCacheBaseFactory;
 import com.google.gerrit.server.cache.PersistentCacheDef;
@@ -89,24 +88,20 @@
   }
 
   @Override
-  public <K, V> Cache<K, V> buildImpl(
-      PersistentCacheDef<K, V> in, long limit, CacheBackend backend) {
+  public <K, V> Cache<K, V> buildImpl(PersistentCacheDef<K, V> in, long limit) {
     ChronicleMapCacheConfig config =
         configFactory.create(
             in.configKey(),
             fileName(cacheDir, in.name(), in.version()),
             in.expireAfterWrite(),
             in.refreshAfterWrite());
-    return build(in, backend, config, metricMaker);
+    return build(in, config, metricMaker);
   }
 
   @SuppressWarnings("unchecked")
   @VisibleForTesting
   <K, V> Cache<K, V> build(
-      PersistentCacheDef<K, V> in,
-      CacheBackend backend,
-      ChronicleMapCacheConfig config,
-      MetricMaker metricMaker) {
+      PersistentCacheDef<K, V> in, ChronicleMapCacheConfig config, MetricMaker metricMaker) {
     ChronicleMapCacheDefProxy<K, V> def = new ChronicleMapCacheDefProxy<>(in);
 
     ChronicleMapCacheImpl<K, V> cache;
@@ -120,7 +115,7 @@
 
       LoadingCache<K, TimedValue<V>> mem =
           (LoadingCache<K, TimedValue<V>>)
-              memCacheFactory.build(def, (CacheLoader<K, V>) memLoader, backend);
+              memCacheFactory.build(def, (CacheLoader<K, V>) memLoader);
 
       cache =
           new ChronicleMapCacheImpl<>(
@@ -142,14 +137,14 @@
 
   @Override
   public <K, V> LoadingCache<K, V> buildImpl(
-      PersistentCacheDef<K, V> in, CacheLoader<K, V> loader, long limit, CacheBackend backend) {
+      PersistentCacheDef<K, V> in, CacheLoader<K, V> loader, long limit) {
     ChronicleMapCacheConfig config =
         configFactory.create(
             in.configKey(),
             fileName(cacheDir, in.name(), in.version()),
             in.expireAfterWrite(),
             in.refreshAfterWrite());
-    return build(in, loader, backend, config, metricMaker);
+    return build(in, loader, config, metricMaker);
   }
 
   @SuppressWarnings("unchecked")
@@ -157,7 +152,6 @@
   public <K, V> LoadingCache<K, V> build(
       PersistentCacheDef<K, V> in,
       CacheLoader<K, V> loader,
-      CacheBackend backend,
       ChronicleMapCacheConfig config,
       MetricMaker metricMaker) {
     ChronicleMapCacheImpl<K, V> cache;
@@ -173,7 +167,7 @@
 
       LoadingCache<K, TimedValue<V>> mem =
           (LoadingCache<K, TimedValue<V>>)
-              memCacheFactory.build(def, (CacheLoader<K, V>) memLoader, backend);
+              memCacheFactory.build(def, (CacheLoader<K, V>) memLoader);
 
       cache =
           new ChronicleMapCacheImpl<>(
diff --git a/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheIT.java b/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheIT.java
index 389fbb1..9367727 100644
--- a/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheIT.java
+++ b/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheIT.java
@@ -21,7 +21,6 @@
 import com.google.gerrit.acceptance.UseLocalDisk;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.extensions.api.accounts.AccountInput;
-import com.google.gerrit.server.cache.CacheBackend;
 import com.google.gerrit.server.cache.PersistentCacheFactory;
 import com.google.inject.Inject;
 import org.junit.Test;
@@ -46,8 +45,7 @@
   public void shouldBuildInMemoryCacheWhenDiskLimitIsNegative() {
     final int negativeDiskLimit = -1;
     final Cache<String, String> cache =
-        persistentCacheFactory.build(
-            new TestPersistentCacheDef("foo", null, negativeDiskLimit, 0), CacheBackend.CAFFEINE);
+        persistentCacheFactory.build(new TestPersistentCacheDef("foo", null, negativeDiskLimit, 0));
 
     assertThat(cache.getClass().getSimpleName()).isEqualTo("CaffeinatedGuavaCache");
   }
@@ -57,8 +55,7 @@
     final int positiveDiskLimit = 1024;
     assertThat(
             persistentCacheFactory.build(
-                new TestPersistentCacheDef("foo", null, positiveDiskLimit, ZERO_INMEMORY_CACHE),
-                CacheBackend.CAFFEINE))
+                new TestPersistentCacheDef("foo", null, positiveDiskLimit, ZERO_INMEMORY_CACHE)))
         .isInstanceOf(ChronicleMapCacheImpl.class);
   }
 
diff --git a/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheTest.java b/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheTest.java
index b943c07..ab6ee14 100644
--- a/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheTest.java
+++ b/src/test/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheTest.java
@@ -28,7 +28,6 @@
 import com.google.gerrit.common.Nullable;
 import com.google.gerrit.metrics.DisabledMetricMaker;
 import com.google.gerrit.metrics.MetricMaker;
-import com.google.gerrit.server.cache.CacheBackend;
 import com.google.gerrit.server.cache.MemoryCacheFactory;
 import com.google.gerrit.server.cache.serialize.CacheSerializer;
 import com.google.gerrit.server.cache.serialize.StringCacheSerializer;
@@ -624,11 +623,10 @@
 
     if (withLoader) {
       return (ChronicleMapCacheImpl<String, String>)
-          cacheFactory.build(
-              cacheDef, cacheDef.loader(), CacheBackend.CAFFEINE, config, metricMaker);
+          cacheFactory.build(cacheDef, cacheDef.loader(), config, metricMaker);
     }
     return (ChronicleMapCacheImpl<String, String>)
-        cacheFactory.build(cacheDef, CacheBackend.CAFFEINE, config, metricMaker);
+        cacheFactory.build(cacheDef, config, metricMaker);
   }
 
   private ChronicleMapCacheImpl<String, String> newCacheWithLoader(@Nullable String loadedValue) {