Enable metrics for the high-availability executors' queues

Allow the Gerrit admin to track the activity of the thread pools and
queues status of the executors used by the plugin which also allows to
detect forwarding lags and highlight issues.

The use of queue metrics requires to change the reload mode of
the plugin to 'restart' because the new and old version of the plugin
won't be able to cohexist as they share the same namespace for the
generated metrics.

Also add the missing listeners' registration of the executors which
prevented the existing thread pools to be shutdown and released
upon plugin unload / reload or Gerrit shutdown.

Change-Id: Ic3845971cdd803e0e8701222ec1d844aafbdcf43
diff --git a/BUILD b/BUILD
index 7f32bc1..55c58fd 100644
--- a/BUILD
+++ b/BUILD
@@ -15,6 +15,7 @@
         "Gerrit-Module: com.ericsson.gerrit.plugins.highavailability.Module",
         "Gerrit-HttpModule: com.ericsson.gerrit.plugins.highavailability.HttpModule",
         "Gerrit-InitStep: com.ericsson.gerrit.plugins.highavailability.Setup",
+        "Gerrit-ReloadMode: restart",
         "Implementation-Title: high-availability plugin",
         "Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/high-availability",
     ],
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/ExecutorProvider.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/ExecutorProvider.java
index 3d3212b..b61ca68 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/ExecutorProvider.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/ExecutorProvider.java
@@ -24,7 +24,7 @@
   private ScheduledExecutorService executor;
 
   protected ExecutorProvider(WorkQueue workQueue, int threadPoolSize, String threadNamePrefix) {
-    executor = workQueue.createQueue(threadPoolSize, threadNamePrefix);
+    executor = workQueue.createQueue(threadPoolSize, threadNamePrefix, true);
   }
 
   @Override
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/IndexModule.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/IndexModule.java
index 3bcc34f..171218d 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/IndexModule.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/IndexModule.java
@@ -31,17 +31,20 @@
     bind(ScheduledExecutorService.class)
         .annotatedWith(IndexExecutor.class)
         .toProvider(IndexExecutorProvider.class);
+    listener().to(IndexExecutorProvider.class);
     bind(ScheduledExecutorService.class)
         .annotatedWith(ForwardedIndexExecutor.class)
         .toProvider(ForwardedIndexExecutorProvider.class);
+    listener().to(ForwardedIndexExecutorProvider.class);
     bind(IndexEventLocks.class).in(Scopes.SINGLETON);
     bind(ScheduledExecutorService.class)
         .annotatedWith(BatchIndexExecutor.class)
         .toProvider(BatchIndexExecutorProvider.class);
+    listener().to(BatchIndexExecutorProvider.class);
     bind(ScheduledExecutorService.class)
         .annotatedWith(ForwardedBatchIndexExecutor.class)
         .toProvider(ForwardedBatchIndexExecutorProvider.class);
-    listener().to(IndexExecutorProvider.class);
+    listener().to(ForwardedBatchIndexExecutorProvider.class);
     DynamicSet.bind(binder(), ChangeIndexedListener.class)
         .to(IndexEventHandler.class)
         .in(Scopes.SINGLETON);
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/cache/CacheExecutorProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/cache/CacheExecutorProviderTest.java
index 7f7071b..e682bb3 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/cache/CacheExecutorProviderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/cache/CacheExecutorProviderTest.java
@@ -39,7 +39,8 @@
   @Before
   public void setUp() throws Exception {
     WorkQueue workQueueMock = mock(WorkQueue.class);
-    when(workQueueMock.createQueue(4, "Forward-Cache-Eviction-Event")).thenReturn(executorMock);
+    when(workQueueMock.createQueue(4, "Forward-Cache-Eviction-Event", true))
+        .thenReturn(executorMock);
     Configuration configMock = mock(Configuration.class, Answers.RETURNS_DEEP_STUBS);
     when(configMock.cache().threadPoolSize()).thenReturn(4);
 
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/event/EventExecutorProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/event/EventExecutorProviderTest.java
index 6e666ed..d99dcf3 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/event/EventExecutorProviderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/event/EventExecutorProviderTest.java
@@ -35,7 +35,7 @@
   @Before
   public void setUp() throws Exception {
     WorkQueue workQueueMock = mock(WorkQueue.class);
-    when(workQueueMock.createQueue(1, "Forward-Stream-Event")).thenReturn(executorMock);
+    when(workQueueMock.createQueue(1, "Forward-Stream-Event", true)).thenReturn(executorMock);
     eventsExecutorProvider = new EventExecutorProvider(workQueueMock);
   }
 
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexExecutorProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexExecutorProviderTest.java
index abab0b9..a9d412f 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexExecutorProviderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexExecutorProviderTest.java
@@ -38,7 +38,7 @@
   public void setUp() throws Exception {
     executorMock = mock(ScheduledThreadPoolExecutor.class);
     WorkQueue workQueueMock = mock(WorkQueue.class);
-    when(workQueueMock.createQueue(4, "Forward-Index-Event")).thenReturn(executorMock);
+    when(workQueueMock.createQueue(4, "Forward-Index-Event", true)).thenReturn(executorMock);
     Configuration configMock = mock(Configuration.class, Answers.RETURNS_DEEP_STUBS);
     when(configMock.index().threadPoolSize()).thenReturn(4);
     indexExecutorProvider = new IndexExecutorProvider(workQueueMock, configMock);