Force indexing of the ascync pending account events

When the multi-site plugin is unloaded OR upon Gerrit
shutdown, all the pending account indexing events need to
be executed otherwise they will be completely lost.

Change-Id: I1107e8a253c8fce507ab1b420938342d7840b9e9
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandler.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandler.java
index 3eea29c..fb7aef1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandler.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandler.java
@@ -25,6 +25,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -78,4 +79,8 @@
       return false;
     }
   }
+
+  public Set<Account.Id> pendingAccountsToIndex() {
+    return accountsToIndex.keySet();
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/IndexEventRouter.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/IndexEventRouter.java
index 0c6d3fd..45c8e53 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/IndexEventRouter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/IndexEventRouter.java
@@ -17,7 +17,10 @@
 import static com.googlesource.gerrit.plugins.multisite.forwarder.ForwardedIndexingHandler.Operation.DELETE;
 import static com.googlesource.gerrit.plugins.multisite.forwarder.ForwardedIndexingHandler.Operation.INDEX;
 
+import com.google.common.flogger.FluentLogger;
+import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.reviewdb.client.Account;
+import com.google.gerrit.reviewdb.client.Account.Id;
 import com.google.gerrit.server.config.AllUsersName;
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
@@ -34,8 +37,11 @@
 import com.googlesource.gerrit.plugins.replication.RefReplicationDoneEvent;
 import java.io.IOException;
 import java.util.Optional;
+import java.util.Set;
 
-public class IndexEventRouter implements ForwardedEventRouter<IndexEvent> {
+public class IndexEventRouter implements ForwardedEventRouter<IndexEvent>, LifecycleListener {
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+
   private final ForwardedIndexAccountHandler indexAccountHandler;
   private final ForwardedIndexChangeHandler indexChangeHandler;
   private final ForwardedIndexGroupHandler indexGroupHandler;
@@ -92,4 +98,23 @@
       }
     }
   }
+
+  @Override
+  public void start() {}
+
+  @Override
+  public void stop() {
+    Set<Id> accountsToIndex = indexAccountHandler.pendingAccountsToIndex();
+    if (!accountsToIndex.isEmpty()) {
+      logger.atWarning().log("Forcing reindex of accounts %s upon shutdown", accountsToIndex);
+      indexAccountHandler.doAsyncIndex();
+    }
+
+    Set<Id> accountsIndexFailed = indexAccountHandler.pendingAccountsToIndex();
+    if (!accountsIndexFailed.isEmpty()) {
+      logger.atSevere().log(
+          "The accounts %s failed to be indexed and their Lucene index is stale",
+          accountsIndexFailed);
+    }
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/RouterModule.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/RouterModule.java
index 91dfc53..bac907e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/RouterModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/router/RouterModule.java
@@ -14,13 +14,14 @@
 
 package com.googlesource.gerrit.plugins.multisite.forwarder.router;
 
-import com.google.inject.AbstractModule;
+import com.google.gerrit.lifecycle.LifecycleModule;
 import com.google.inject.Scopes;
 
-public class RouterModule extends AbstractModule {
+public class RouterModule extends LifecycleModule {
   @Override
   protected void configure() {
     bind(IndexEventRouter.class).in(Scopes.SINGLETON);
+    listener().to(IndexEventRouter.class).in(Scopes.SINGLETON);
     bind(CacheEvictionEventRouter.class).in(Scopes.SINGLETON);
     bind(ProjectListUpdateRouter.class).in(Scopes.SINGLETON);
     bind(StreamEventRouter.class).in(Scopes.SINGLETON);