Set a default limit for the in-memory code owner config cache

For Android and Chrome at Google p95 is mostly below 50 code owner
config reads for change. However the worst peek that we observed for p99
is ~6.3k code owner config reads per change. Set a default limit of 10k
for the cache that is well above the worst peek. The only purpose of
this limit is to ensure that we are not running into out-of-memory
errors if a projects has a crazy amount of OWNERS files. If the limit is
reached the latency for code owner computations goes down, but that's
better then running out of memory. According to a back-of-the-envelope
calculation 10k entries should consume about 34 MiB of memory.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: Iad98f176b1aea558294e89ee7995a79827bb5bfc
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfiguration.java b/java/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfiguration.java
index 3af457d..d070d81 100644
--- a/java/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfiguration.java
+++ b/java/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfiguration.java
@@ -49,6 +49,8 @@
   @VisibleForTesting
   static final String KEY_ENABLE_EXPERIMENTAL_REST_ENDPOINTS = "enableExperimentalRestEndpoints";
 
+  @VisibleForTesting static final int DEFAULT_MAX_CODE_OWNER_CONFIG_CACHE_SIZE = 10000;
+
   private static final String KEY_MAX_CODE_OWNER_CONFIG_CACHE_SIZE = "maxCodeOwnerConfigCacheSize";
 
   private final CodeOwnersPluginConfigSnapshot.Factory codeOwnersPluginConfigSnapshotFactory;
@@ -132,7 +134,8 @@
       int maxCodeOwnerConfigCacheSize =
           pluginConfigFactory
               .getFromGerritConfig(pluginName)
-              .getInt(KEY_MAX_CODE_OWNER_CONFIG_CACHE_SIZE, /* defaultValue= */ 0);
+              .getInt(
+                  KEY_MAX_CODE_OWNER_CONFIG_CACHE_SIZE, DEFAULT_MAX_CODE_OWNER_CONFIG_CACHE_SIZE);
       return maxCodeOwnerConfigCacheSize > 0
           ? Optional.of(maxCodeOwnerConfigCacheSize)
           : Optional.empty();
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfigurationTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfigurationTest.java
index ff53824..d9c3e14 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfigurationTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/config/CodeOwnersPluginConfigurationTest.java
@@ -91,8 +91,10 @@
   }
 
   @Test
-  public void codeOwnerConfigCacheSizeIsUnlimitedByDefault() throws Exception {
-    assertThat(codeOwnersPluginConfiguration.getMaxCodeOwnerConfigCacheSize()).isEmpty();
+  public void codeOwnerConfigCacheSizeIsLimitedByDefault() throws Exception {
+    assertThat(codeOwnersPluginConfiguration.getMaxCodeOwnerConfigCacheSize())
+        .value()
+        .isEqualTo(CodeOwnersPluginConfiguration.DEFAULT_MAX_CODE_OWNER_CONFIG_CACHE_SIZE);
   }
 
   @Test
diff --git a/resources/Documentation/config.md b/resources/Documentation/config.md
index 21a22bd..53c39d2 100644
--- a/resources/Documentation/config.md
+++ b/resources/Documentation/config.md
@@ -543,7 +543,7 @@
         files are cached in memory for the time of the request.\
         This configuration parameter allows to set a limit for the number of
         code owner config files that are cached per request.\
-        By default `0` (unlimited).
+        By default `10000`.
 
 # <a id="projectConfiguration">Project configuration in @PLUGIN@.config</a>