Merge branch 'stable-3.1'

* stable-3.1:
  Force indexing of the ascync pending account events
  e2e-tests: Make CloneUsingMultiGerrit1 name unique
  e2e-tests: Add the CreateProjectUsingMultiGerritTwice scenario
  e2e-tests: Fix CloneUsingMultiGerrit1 seconds unit

Change-Id: I5b0cc187ae2bb4b7e9b5805ff0ecc3566458615a
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 0934fd5..5212aa4 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
@@ -24,6 +24,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -77,4 +78,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 9f659ec..202fb42 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,9 @@
 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.entities.Account;
+import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.server.config.AllUsersName;
 import com.google.inject.Inject;
 import com.googlesource.gerrit.plugins.multisite.forwarder.ForwardedIndexAccountHandler;
@@ -33,8 +35,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;
@@ -90,4 +95,23 @@
       }
     }
   }
+
+  @Override
+  public void start() {}
+
+  @Override
+  public void stop() {
+    Set<Account.Id> accountsToIndex = indexAccountHandler.pendingAccountsToIndex();
+    if (!accountsToIndex.isEmpty()) {
+      logger.atWarning().log("Forcing reindex of accounts %s upon shutdown", accountsToIndex);
+      indexAccountHandler.doAsyncIndex();
+    }
+
+    Set<Account.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);
diff --git a/src/test/resources/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerritTwice.json b/src/test/resources/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerritTwice.json
new file mode 100644
index 0000000..da1a058
--- /dev/null
+++ b/src/test/resources/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerritTwice.json
@@ -0,0 +1,4 @@
+[
+  {
+  }
+]
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CloneUsingMultiGerrit1.scala b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CloneUsingMultiGerrit1.scala
index 4a7c65d..9297907 100644
--- a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CloneUsingMultiGerrit1.scala
+++ b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CloneUsingMultiGerrit1.scala
@@ -23,14 +23,19 @@
 
 class CloneUsingMultiGerrit1 extends GitSimulation {
   private val data: FileBasedFeederBuilder[Any]#F#F = jsonFile(resource).convert(url).queue
-  private val default: String = name
+  private var default: String = name
+
+  def this(default: String) {
+    this()
+    this.default = default
+  }
 
   override def replaceOverride(in: String): String = {
     val next = replaceProperty("http_port1", 8081, in)
     replaceKeyWith("_project", default, next)
   }
 
-  private val test: ScenarioBuilder = scenario(name)
+  val test: ScenarioBuilder = scenario(unique)
     .feed(data)
     .exec(gitRequest)
 
@@ -42,11 +47,11 @@
       atOnceUsers(1)
     ),
     test.inject(
-      nothingFor(21 second),
+      nothingFor(21 seconds),
       atOnceUsers(1)
     ),
     deleteProject.test.inject(
-      nothingFor(23 second),
+      nothingFor(23 seconds),
       atOnceUsers(1)
     ),
   ).protocols(gitProtocol, httpProtocol)
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerrit.scala b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerrit.scala
index 3ae12b5..721d581 100644
--- a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerrit.scala
+++ b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerrit.scala
@@ -27,7 +27,7 @@
     this.default = default
   }
 
-  val test: ScenarioBuilder = scenario(name)
+  val test: ScenarioBuilder = scenario(unique)
     .feed(data)
     .exec(httpRequest)
 
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerritTwice.scala b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerritTwice.scala
new file mode 100644
index 0000000..81e7578
--- /dev/null
+++ b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/CreateProjectUsingMultiGerritTwice.scala
@@ -0,0 +1,52 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.multisite.scenarios
+
+import com.google.gerrit.scenarios.GitSimulation
+import io.gatling.core.Predef.{atOnceUsers, _}
+
+import scala.concurrent.duration._
+
+class CreateProjectUsingMultiGerritTwice extends GitSimulation {
+  private val default: String = name
+
+  private val createProject = new CreateProjectUsingMultiGerrit(default)
+  private val deleteProject = new DeleteProjectUsingMultiGerrit(default)
+  private val createItAgain = new CreateProjectUsingMultiGerrit(default)
+  private val verifyProject = new CloneUsingMultiGerrit1(default)
+  private val deleteItAfter = new DeleteProjectUsingMultiGerrit(default)
+
+  setUp(
+    createProject.test.inject(
+      atOnceUsers(1)
+    ),
+    deleteProject.test.inject(
+      nothingFor(21 seconds),
+      atOnceUsers(1)
+    ),
+    createItAgain.test.inject(
+      nothingFor(43 seconds),
+      atOnceUsers(1)
+    ),
+    verifyProject.test.inject(
+      nothingFor(70 seconds),
+      atOnceUsers(1)
+    ),
+    deleteItAfter.test.inject(
+      nothingFor(72 seconds),
+      atOnceUsers(1)
+    ),
+  ).protocols(gitProtocol, httpProtocol)
+}
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/DeleteProjectUsingMultiGerrit.scala b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/DeleteProjectUsingMultiGerrit.scala
index ee645a9..674f1b3 100644
--- a/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/DeleteProjectUsingMultiGerrit.scala
+++ b/src/test/scala/com/googlesource/gerrit/plugins/multisite/scenarios/DeleteProjectUsingMultiGerrit.scala
@@ -27,7 +27,7 @@
     this.default = default
   }
 
-  val test: ScenarioBuilder = scenario(name)
+  val test: ScenarioBuilder = scenario(unique)
     .feed(data)
     .exec(httpRequest)