Drain executor of index change requests before closing index

IndexCollection.stop() closes the index to flush pending index writes
at shutdown.  Unfortunately some pending index writes can be enqueued
in the index executor without the index even being aware of them yet,
causing lost writes on exit.  Drain the work queue to avoid this.

This has always been possible in a gerrit instance with >1 index
threads (e.g., with '[index] threads = 2' in gerrit.config).
176cd58bb0bb (Always use a thread pool for interactive indexing,
2016-05-09) made Gerrit use a thread pool with multiple threads for
indexing by default, making the problem easier to trigger.

Noticed by using ctrl+c to exit gerrit daemon in a development
environment.

Making the close calls parallel as in a44d8396e72d (Lucene: close
sub-indexes in parallel, 2013-06-21) can wait for a followup change.

Change-Id: I7bda1305858fe4aa5ea12a4bc68d35f0cdedf225
diff --git a/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java b/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java
index bdc7189..9d02960 100644
--- a/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java
+++ b/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java
@@ -30,8 +30,8 @@
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
 import com.google.gerrit.reviewdb.client.Account;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.client.PatchSet;
@@ -88,6 +88,7 @@
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Secondary index implementation using Apache Lucene.
@@ -179,20 +180,13 @@
 
   @Override
   public void close() {
-    List<ListenableFuture<?>> closeFutures = Lists.newArrayListWithCapacity(2);
-    closeFutures.add(executor.submit(new Runnable() {
-      @Override
-      public void run() {
-        openIndex.close();
-      }
-    }));
-    closeFutures.add(executor.submit(new Runnable() {
-      @Override
-      public void run() {
-        closedIndex.close();
-      }
-    }));
-    Futures.getUnchecked(Futures.allAsList(closeFutures));
+    MoreExecutors.shutdownAndAwaitTermination(
+        executor, Long.MAX_VALUE, TimeUnit.SECONDS);
+    try {
+      openIndex.close();
+    } finally {
+      closedIndex.close();
+    }
   }
 
   @Override