Always use the stored timestamp when checking for updates

Rely on the timestamp stored in the filesystem before updating
with a new value: the in-memory hash map could be stale and
it may risk to push back the latest updated timestamp for the indexed
values.

Example: you want to manually change the filesystem time-stamp value
for triggering some ad-hoc auto-reindexing. The in-memory value won't
be current anymore and risk to create more damage than benefit.

Before this change, FlusherRunner::store kept storing a null timestamp,
and was doing so for each call to the latter method, unexpectedly. With
this change, a new timestamp gets stored only if it is later than the
current one, or if it is the first known one.

Change-Id: Ib7ac06bdba25b6b0a24ada282c78cf123e3d6529
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 2c19ce7..54ceb62 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
@@ -33,8 +33,6 @@
 import java.nio.file.Path;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Optional;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,7 +53,6 @@
   private volatile LocalDateTime groupTs;
 
   class FlusherRunner implements Runnable {
-    private Map<AbstractIndexRestApiServlet.IndexName, LocalDateTime> storedTs = new HashMap<>();
 
     @Override
     public void run() {
@@ -65,12 +62,11 @@
     }
 
     private void store(AbstractIndexRestApiServlet.IndexName index, LocalDateTime latestTs) {
-      LocalDateTime currTs = storedTs.get(index);
-      if (currTs == null || latestTs.isAfter(currTs)) {
+      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));
-          storedTs.put(index, currTs);
         } catch (IOException e) {
           log.error("Unable to update last timestamp for index " + index, e);
         }