Merge "Pin haproxy to 1.8.30-buster and fix associated issues" into stable-3.2
diff --git a/README.md b/README.md
index 8e7ef27..c8ac4b4 100644
--- a/README.md
+++ b/README.md
@@ -1,13 +1,12 @@
 # Gerrit high-availability plugin
 
 This plugin allows deploying a cluster of multiple Gerrit masters
-on the same data-center sharing the same ReviewDb and Git repositories.
+on the same data-center sharing the same Git repositories.
 
 Requirements for the Gerrit masters are:
 
 - Gerrit v2.14.20 or later
 - Externally mounted filesystem shared among the cluster
-- ReviewDb on an external DataBase Server
 - Load-balancer (HAProxy or similar)
 
 ## License
@@ -25,12 +24,12 @@
 `gerrit-02.mycompany.com`, listening on the HTTP port 8080, with a shared volume
 mounted under `/shared`, see below the minimal configuration steps.
 
-1. Install one Gerrit master on the first node (e.g. `gerrit-01.mycompany.com`) using an external
-   ReviewDb on a DB server and the repositories location under the shared volume (e.g. `/shared/git`).
-   Init the site in order to create the DB Schema and the initial repositories.
+1. Install one Gerrit master on the first node (e.g. `gerrit-01.mycompany.com`)
+   with the repositories location under the shared volume (e.g. `/shared/git`).
+   Init the site in order to create the initial repositories.
 
 2. Copy all the files of the first Gerrit master onto the second node (e.g. `gerrit-02.mycompany.com`)
-   so that it points to the same ReviewDb and the same repositories location.
+   so that it points to the same repositories location.
 
 3. Install the high-availability plugin into the `$GERRIT_SITE/plugins` directory of both
    the Gerrit servers.
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/autoreindex/IndexTs.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/autoreindex/IndexTs.java
index 62d9c10..a6539e0 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/autoreindex/IndexTs.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/autoreindex/IndexTs.java
@@ -36,6 +36,7 @@
 import java.time.format.DateTimeFormatter;
 import java.util.Optional;
 import java.util.concurrent.ScheduledExecutorService;
+import org.eclipse.jgit.internal.storage.file.FileSnapshot;
 
 @Singleton
 public class IndexTs
@@ -62,15 +63,17 @@
 
   class FlusherRunner implements Runnable {
     private final AbstractIndexRestApiServlet.IndexName index;
+    private final Path tsPath;
+    private FileSnapshot tsPathSnapshot;
 
     @Override
     public void run() {
+      reloadIndexTimeStampIfChanged();
       LocalDateTime latestTs = getIndexTimeStamp();
       Optional<LocalDateTime> currTs = getUpdateTs(index);
       if (!currTs.isPresent() || latestTs.isAfter(currTs.get())) {
-        Path indexTsFile = dataDir.resolve(index.name().toLowerCase());
         try {
-          Files.write(indexTsFile, latestTs.format(formatter).getBytes(StandardCharsets.UTF_8));
+          Files.write(tsPath, latestTs.format(formatter).getBytes(StandardCharsets.UTF_8));
         } catch (IOException e) {
           log.atSevere().withCause(e).log("Unable to update last timestamp for index %s", index);
         }
@@ -79,6 +82,8 @@
 
     FlusherRunner(AbstractIndexRestApiServlet.IndexName index) {
       this.index = index;
+      this.tsPath = getIndexTsPath(index);
+      this.tsPathSnapshot = FileSnapshot.save(tsPath.toFile());
     }
 
     private LocalDateTime getIndexTimeStamp() {
@@ -95,6 +100,31 @@
           throw new IllegalArgumentException("Unsupported index " + index);
       }
     }
+
+    private void reloadIndexTimeStampIfChanged() {
+      if (tsPathSnapshot.isModified(tsPath.toFile())) {
+        getUpdateTs(index).ifPresent(this::setIndexTimeStamp);
+      }
+    }
+
+    private void setIndexTimeStamp(LocalDateTime newTs) {
+      switch (index) {
+        case CHANGE:
+          changeTs = newTs;
+          break;
+        case GROUP:
+          groupTs = newTs;
+          break;
+        case ACCOUNT:
+          accountTs = newTs;
+          break;
+        case PROJECT:
+          projectTs = newTs;
+          break;
+      }
+
+      tsPathSnapshot = FileSnapshot.save(tsPath.toFile());
+    }
   }
 
   @Inject
@@ -152,7 +182,7 @@
 
   public Optional<LocalDateTime> getUpdateTs(AbstractIndexRestApiServlet.IndexName index) {
     try {
-      Path indexTsFile = dataDir.resolve(index.name().toLowerCase());
+      Path indexTsFile = getIndexTsPath(index);
       if (indexTsFile.toFile().exists()) {
         String tsString = Files.readAllLines(indexTsFile).get(0);
         return Optional.of(LocalDateTime.parse(tsString, formatter));
@@ -185,4 +215,8 @@
         throw new IllegalArgumentException("Unsupported index " + index);
     }
   }
+
+  private Path getIndexTsPath(AbstractIndexRestApiServlet.IndexName index) {
+    return dataDir.resolve(index.name().toLowerCase());
+  }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/ForwardedIndexChangeHandler.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/ForwardedIndexChangeHandler.java
index 91e5a02..770259d 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/ForwardedIndexChangeHandler.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/ForwardedIndexChangeHandler.java
@@ -21,6 +21,7 @@
 import com.ericsson.gerrit.plugins.highavailability.index.ForwardedIndexExecutor;
 import com.google.common.base.Splitter;
 import com.google.gerrit.entities.Change;
+import com.google.gerrit.entities.Project;
 import com.google.gerrit.exceptions.StorageException;
 import com.google.gerrit.server.index.change.ChangeIndexer;
 import com.google.gerrit.server.notedb.ChangeNotes;
@@ -30,6 +31,7 @@
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 import java.io.IOException;
+import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -157,7 +159,15 @@
   }
 
   private static Change.Id parseChangeId(String id) {
-    return Change.id(Integer.parseInt(Splitter.on("~").splitToList(id).get(1)));
+    return Change.id(Integer.parseInt(getChangeIdParts(id).get(1)));
+  }
+
+  private static Project.NameKey parseProject(String id) {
+    return Project.nameKey(getChangeIdParts(id).get(0));
+  }
+
+  private static List<String> getChangeIdParts(String id) {
+    return Splitter.on("~").splitToList(id);
   }
 
   private static boolean isCausedByNoSuchChangeException(Throwable throwable) {
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java
index f9ec516..a567f61 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java
@@ -88,6 +88,7 @@
     setHeaders(rsp);
     String path = req.getRequestURI();
     T id = parse(path.substring(path.lastIndexOf('/') + 1));
+
     try {
       forwardedIndexingHandler.index(id, operation, parseBody(req));
       rsp.setStatus(SC_NO_CONTENT);
diff --git a/src/test/docker/gerrit/Dockerfile b/src/test/docker/gerrit/Dockerfile
index 3c74b10..c61bd2f 100644
--- a/src/test/docker/gerrit/Dockerfile
+++ b/src/test/docker/gerrit/Dockerfile
@@ -1,12 +1,12 @@
-FROM gerritcodereview/gerrit:3.2.10
+FROM gerritcodereview/gerrit:3.2.14-ubuntu20
 
 ENV GERRIT_BRANCH=stable-3.2
 
-ENV GERRIT_CI_URL=https://gerrit-ci.gerritforge.com/job
+ENV GERRIT_CI_URL=https://archive-ci.gerritforge.com/job
 
 USER root
 
-RUN yum install -y iputils nmap curl lsof gettext net-tools sudo
+RUN apt update && apt install -y iputils-ping nmap curl lsof gettext net-tools sudo
 
 USER gerrit