Merge branch 'stable-2.14' into stable-2.15

* stable-2.14:
  Setup: Rename the number-to-string conversion methods
  Setup: Add autoReindex delay and pollInterval configuration steps
  Setup: Make the http section method name consistent
  Setup: Add init step for autoReindex configuration

Change-Id: I061150375866ae25728ab809df50929fa6274f09
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java
index 92bc0dc..0ad64e5 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java
@@ -186,6 +186,8 @@
     static final String ENABLED = "enabled";
     static final String DELAY = "delay";
     static final String POLL_INTERVAL = "pollInterval";
+    static final long DEFAULT_DELAY = 10L;
+    static final long DEFAULT_POLL_INTERVAL = 0L;
 
     private final boolean enabled;
     private final long delaySec;
@@ -194,10 +196,16 @@
     public AutoReindex(Config cfg) {
       this.enabled = cfg.getBoolean(AUTO_REINDEX_SECTION, ENABLED, false);
       this.delaySec =
-          ConfigUtil.getTimeUnit(cfg, AUTO_REINDEX_SECTION, null, DELAY, 10L, TimeUnit.SECONDS);
+          ConfigUtil.getTimeUnit(
+              cfg, AUTO_REINDEX_SECTION, null, DELAY, DEFAULT_DELAY, TimeUnit.SECONDS);
       this.pollSec =
           ConfigUtil.getTimeUnit(
-              cfg, AUTO_REINDEX_SECTION, null, POLL_INTERVAL, 0L, TimeUnit.SECONDS);
+              cfg,
+              AUTO_REINDEX_SECTION,
+              null,
+              POLL_INTERVAL,
+              DEFAULT_POLL_INTERVAL,
+              TimeUnit.SECONDS);
     }
 
     public boolean enabled() {
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Setup.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Setup.java
index 7b0b832..217e644 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Setup.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Setup.java
@@ -14,6 +14,12 @@
 
 package com.ericsson.gerrit.plugins.highavailability;
 
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.AutoReindex.AUTO_REINDEX_SECTION;
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.AutoReindex.DEFAULT_DELAY;
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.AutoReindex.DEFAULT_POLL_INTERVAL;
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.AutoReindex.DELAY;
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.AutoReindex.ENABLED;
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.AutoReindex.POLL_INTERVAL;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.Cache.CACHE_SECTION;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.DEFAULT_THREAD_POOL_SIZE;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.Http.CONNECTION_TIMEOUT_KEY;
@@ -94,7 +100,8 @@
       Path pluginConfigFile = site.etc_dir.resolve(pluginName + ".config");
       config = new FileBasedConfig(pluginConfigFile.toFile(), FS.DETECTED);
       config.load();
-      configureHttp();
+      configureAutoReindexSection();
+      configureHttpSection();
       configureCacheSection();
       configureIndexSection();
       configureWebsessionsSection();
@@ -107,6 +114,27 @@
     }
   }
 
+  private void configureAutoReindexSection() {
+    ui.header("AutoReindex section");
+    Boolean autoReindex =
+        promptAndSetBoolean("Auto reindex", AUTO_REINDEX_SECTION, null, ENABLED, false);
+    config.setBoolean(AUTO_REINDEX_SECTION, null, ENABLED, autoReindex);
+
+    String delay =
+        promptAndSetString(
+            "Delay", AUTO_REINDEX_SECTION, null, DELAY, numberToString(DEFAULT_DELAY));
+    config.setLong(AUTO_REINDEX_SECTION, null, DELAY, Long.valueOf(delay));
+
+    String pollInterval =
+        promptAndSetString(
+            "Poll interval",
+            AUTO_REINDEX_SECTION,
+            null,
+            POLL_INTERVAL,
+            numberToString(DEFAULT_POLL_INTERVAL));
+    config.setLong(AUTO_REINDEX_SECTION, null, POLL_INTERVAL, Long.valueOf(pollInterval));
+  }
+
   private void configureMainSection() {
     ui.header("Main section");
     String sharedDirDefault = ui.isBatch() ? DEFAULT_SHARED_DIRECTORY : null;
@@ -137,7 +165,7 @@
     }
   }
 
-  private void configureHttp() {
+  private void configureHttpSection() {
     ui.header("Http section");
     promptAndSetString("User", HTTP_SECTION, USER_KEY, null);
     promptAndSetString("Password", HTTP_SECTION, PASSWORD_KEY, null);
@@ -145,13 +173,22 @@
         "Max number of tries to forward to remote peer",
         HTTP_SECTION,
         MAX_TRIES_KEY,
-        str(DEFAULT_MAX_TRIES));
+        numberToString(DEFAULT_MAX_TRIES));
     promptAndSetString(
-        "Retry interval [ms]", HTTP_SECTION, RETRY_INTERVAL_KEY, str(DEFAULT_RETRY_INTERVAL));
+        "Retry interval [ms]",
+        HTTP_SECTION,
+        RETRY_INTERVAL_KEY,
+        numberToString(DEFAULT_RETRY_INTERVAL));
     promptAndSetString(
-        "Connection timeout [ms]", HTTP_SECTION, CONNECTION_TIMEOUT_KEY, str(DEFAULT_TIMEOUT_MS));
+        "Connection timeout [ms]",
+        HTTP_SECTION,
+        CONNECTION_TIMEOUT_KEY,
+        numberToString(DEFAULT_TIMEOUT_MS));
     promptAndSetString(
-        "Socket timeout [ms]", HTTP_SECTION, SOCKET_TIMEOUT_KEY, str(DEFAULT_TIMEOUT_MS));
+        "Socket timeout [ms]",
+        HTTP_SECTION,
+        SOCKET_TIMEOUT_KEY,
+        numberToString(DEFAULT_TIMEOUT_MS));
   }
 
   private void configureCacheSection() {
@@ -160,7 +197,7 @@
         "Cache thread pool size",
         CACHE_SECTION,
         THREAD_POOL_SIZE_KEY,
-        str(DEFAULT_THREAD_POOL_SIZE));
+        numberToString(DEFAULT_THREAD_POOL_SIZE));
   }
 
   private void configureIndexSection() {
@@ -169,7 +206,7 @@
         "Index thread pool size",
         INDEX_SECTION,
         THREAD_POOL_SIZE_KEY,
-        str(DEFAULT_THREAD_POOL_SIZE));
+        numberToString(DEFAULT_THREAD_POOL_SIZE));
   }
 
   private void configureWebsessionsSection() {
@@ -178,6 +215,16 @@
         "Cleanup interval", WEBSESSION_SECTION, CLEANUP_INTERVAL_KEY, DEFAULT_CLEANUP_INTERVAL);
   }
 
+  private Boolean promptAndSetBoolean(
+      String title, String section, String subsection, String name, Boolean defaultValue) {
+    Boolean oldValue = config.getBoolean(section, subsection, name, defaultValue);
+    Boolean newValue = Boolean.parseBoolean(ui.readString(String.valueOf(oldValue), title));
+    if (!Objects.equals(oldValue, newValue)) {
+      config.setBoolean(section, subsection, name, newValue);
+    }
+    return newValue;
+  }
+
   private String promptAndSetString(
       String title, String section, String name, String defaultValue) {
     return promptAndSetString(title, section, null, name, defaultValue);
@@ -197,8 +244,12 @@
     return newValue;
   }
 
-  private static String str(int n) {
-    return Integer.toString(n);
+  private static String numberToString(int number) {
+    return Integer.toString(number);
+  }
+
+  private static String numberToString(long number) {
+    return Long.toString(number);
   }
 
   private boolean createHAReplicaSite(FileBasedConfig pluginConfig)