Disable auto-reindexing if stale during online reindex

This option exists to handle a race condition that can only occur when
there are concurrent writes to the same document. This doesn't apply
during offline reindex, and turning it off will speed things up.

It is not especially feasible to pass this option into ChangeIndexer in
code, so we have to use a config value for it. Remove the "test" prefix
from the config name, and document it.

Change-Id: Iecc12c3ab0f068f24063c358ce50a40b25362511
diff --git a/Documentation/config-gerrit.txt b/Documentation/config-gerrit.txt
index 09a579d..1d3400e 100644
--- a/Documentation/config-gerrit.txt
+++ b/Documentation/config-gerrit.txt
@@ -2653,6 +2653,15 @@
 +
 Defaults to 1024.
 
+[[index.autoReindexIfStale]]index.autoReindexIfStale::
++
+Whether to automatically check if a document became stale in the index
+immediately after indexing it. If false, there is a race condition during two
+simultaneous writes that may cause one of the writes to not be reflected in the
+index. The check to avoid this does consume some resources.
++
+Defaults to true.
+
 ==== Lucene configuration
 
 Open and closed changes are indexed in separate indexes named
diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/GetRelatedIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/GetRelatedIT.java
index 6bb55b9..6c06753 100644
--- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/GetRelatedIT.java
+++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/change/GetRelatedIT.java
@@ -517,7 +517,7 @@
   }
 
   @Test
-  @GerritConfig(name = "index.testAutoReindexIfStale", value = "false")
+  @GerritConfig(name = "index.autoReindexIfStale", value = "false")
   public void getRelatedForStaleChange() throws Exception {
     RevCommit c1_1 = commitBuilder().add("a.txt", "1").message("subject: 1").create();
 
diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/notedb/ChangeRebuilderIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/notedb/ChangeRebuilderIT.java
index 15b74bd..87ca2a0 100644
--- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/notedb/ChangeRebuilderIT.java
+++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/server/notedb/ChangeRebuilderIT.java
@@ -120,7 +120,7 @@
     // unintentional auto-rebuilding of the change in NoteDb during the read
     // path of the reindex-if-stale check. For the purposes of this test, we
     // want precise control over when auto-rebuilding happens.
-    cfg.setBoolean("index", null, "testAutoReindexIfStale", false);
+    cfg.setBoolean("index", null, "autoReindexIfStale", false);
 
     return cfg;
   }
diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Reindex.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Reindex.java
index 410d058..232d71b 100644
--- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Reindex.java
+++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Reindex.java
@@ -84,8 +84,7 @@
     globalConfig = dbInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
     threads = ThreadLimiter.limitThreads(dbInjector, threads);
     checkNotSlaveMode();
-    disableLuceneAutomaticCommit();
-    disableChangeCache();
+    overrideConfig();
     LifecycleManager dbManager = new LifecycleManager();
     dbManager.add(dbInjector);
     dbManager.start();
@@ -177,15 +176,18 @@
     return dbInjector.createChildInjector(modules);
   }
 
-  private void disableLuceneAutomaticCommit() {
+  private void overrideConfig() {
+    // Disable auto-commit for speed; committing will happen at the end of the process.
     if (IndexModule.getIndexType(dbInjector) == IndexType.LUCENE) {
       globalConfig.setLong("index", "changes_open", "commitWithin", -1);
       globalConfig.setLong("index", "changes_closed", "commitWithin", -1);
     }
-  }
 
-  private void disableChangeCache() {
+    // Disable change cache.
     globalConfig.setLong("cache", "changes", "maximumWeight", 0);
+
+    // Disable auto-reindexing if stale, since there are no concurrent writes to race with.
+    globalConfig.setBoolean("index", null, "autoReindexIfStale", false);
   }
 
   private <K, V, I extends Index<K, V>> boolean reindex(IndexDefinition<K, V, I> def)
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/index/change/ChangeIndexer.java b/gerrit-server/src/main/java/com/google/gerrit/server/index/change/ChangeIndexer.java
index 4edfab2..2ff04ae 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/index/change/ChangeIndexer.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/index/change/ChangeIndexer.java
@@ -164,7 +164,7 @@
   }
 
   private static boolean autoReindexIfStale(Config cfg) {
-    return cfg.getBoolean("index", null, "testAutoReindexIfStale", true);
+    return cfg.getBoolean("index", null, "autoReindexIfStale", true);
   }
 
   /**