Enable autoreindex in high-availability

Allow the high-availability plugin to track changes, accounts and groups
that have been updated while the server was down for a number of minutes
or, crucially, when a new instance is deployed from a snapshot.

Change-Id: I50543949ee5b6969b98201541368386a90cc002f
diff --git a/dual-master/Makefile b/dual-master/Makefile
index 6e90cfb..2b6e9bf 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -90,7 +90,10 @@
 		$(CLUSTER_OPTIONAL_PARAMS) \
 		$(GERRIT_OPTIONAL_MASTER_VOLUME)
 
-service-master-1: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
+service-master-1: set-optional-params-metrics-cloudwatch set-optional-params-smtp \
+					set-optional-params-multisite set-ldap-account-pattern \
+					set-optional-gerrit-ulimits set-optional-jgit-conf \
+					set-high-availability-optional-params
 ifdef GERRIT_MASTER1_INSTANCE_ID
 		$(eval MASTER1_SERVICE_OPTIONAL_PARAMS := $(MASTER1_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_MASTER1_INSTANCE_ID))
 endif
@@ -135,6 +138,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(HA_OPTIONAL_PARAMS) \
 		$(JGIT_OPTIONAL_PARAMS) \
 		$(MULTISITE_OPTIONAL_PARAMS) \
 		$(REMOTE_OPTIONAL_PARAMS) \
@@ -144,7 +148,10 @@
 		$(SMTP_OPTIONAL_PARAMS) \
 		$(GERRIT_ULIMITS)
 
-service-master-2: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
+service-master-2: set-optional-params-metrics-cloudwatch set-optional-params-smtp \
+					set-optional-params-multisite set-ldap-account-pattern \
+					set-optional-gerrit-ulimits set-optional-jgit-conf \
+					set-high-availability-optional-params
 ifdef GERRIT_MASTER2_INSTANCE_ID
 		$(eval MASTER2_SERVICE_OPTIONAL_PARAMS := $(MASTER2_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_MASTER2_INSTANCE_ID))
 endif
@@ -190,6 +197,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(HA_OPTIONAL_PARAMS) \
 		$(JGIT_OPTIONAL_PARAMS) \
 		$(MULTISITE_OPTIONAL_PARAMS) \
 		$(REMOTE_OPTIONAL_PARAMS) \
@@ -494,3 +502,9 @@
 
 git-ssh-publish:
 	$(MAKE) -C ../master-slave/git-ssh git-ssh-publish
+
+set-high-availability-optional-params:
+	$(eval HA_OPTIONAL_PARAMS=)
+ifdef AUTOREINDEX_POLL_INTERVAL
+		$(eval HA_OPTIONAL_PARAMS := ParameterKey=AutoReindexPollInterval,ParameterValue=$(AUTOREINDEX_POLL_INTERVAL))
+endif
diff --git a/dual-master/README.md b/dual-master/README.md
index 01abceb..17d4be2 100644
--- a/dual-master/README.md
+++ b/dual-master/README.md
@@ -213,6 +213,10 @@
     The nested stack will be *retained* when the cluster is deleted, so that
     existing data can be used to perform blue/green deployments.
 
+* `AUTOREINDEX_POLL_INTERVAL`. Optional. Interval between reindexing of all changes, accounts and groups.
+Default: `10m`
+high-availability docs [here](https://gerrit.googlesource.com/plugins/high-availability/+/refs/heads/master/src/main/resources/Documentation/config.md)
+
 #### REPLICATION SERVICE
 
 * `REPLICATION_SERVICE_ENABLED`: Optional. Whether to expose a replication endpoint.
diff --git a/dual-master/cf-service-master.yml b/dual-master/cf-service-master.yml
index 46f5ca5..bfb87ec 100644
--- a/dual-master/cf-service-master.yml
+++ b/dual-master/cf-service-master.yml
@@ -190,6 +190,10 @@
     ConstraintDescription: Choose a slash-separated ('/') string not starting with a slash ('/')
     AllowedPattern: '^[^\/].*'
     Default: 'gerrit/multi-site'
+  AutoReindexPollInterval:
+    Description: Interval between reindexing of all changes, accounts and groups.
+    Type: String
+    Default: '10m'
 
 Mappings:
   Gerrit:
@@ -264,6 +268,8 @@
                       Value: !Ref GerritKeyPrefix
                     - Name: SETUP_HA
                       Value: true
+                    - Name: HA_AUTOREINDEX_POLL_INTERVAL
+                      Value: !Ref AutoReindexPollInterval
                     - Name: HA_PEER_URL
                       Value: !Sub
                         - 'http://${PeerSubdomain}.${HostedZoneName}:${HTTPGerritLBPort}'
diff --git a/dual-master/setup.env.template b/dual-master/setup.env.template
index ce18bb5..f3b01ad 100644
--- a/dual-master/setup.env.template
+++ b/dual-master/setup.env.template
@@ -62,4 +62,6 @@
 MASTER_MAX_COUNT:=2
 
 GERRIT_CONTAINER_FDS_SOFT_LIMIT:=32768
-GERRIT_CONTAINER_FDS_HARD_LIMIT:=32768
\ No newline at end of file
+GERRIT_CONTAINER_FDS_HARD_LIMIT:=32768
+
+AUTOREINDEX_POLL_INTERVAL:=10m
\ No newline at end of file
diff --git a/gerrit/etc/high-availability.config.template b/gerrit/etc/high-availability.config.template
index b26f4d6..db00a79 100644
--- a/gerrit/etc/high-availability.config.template
+++ b/gerrit/etc/high-availability.config.template
@@ -7,6 +7,10 @@
 [peerInfo "static"]
   url = {{ HA_PEER_URL }}
 
+[autoReindex]
+  enabled = true
+  pollInterval = {{ HA_AUTOREINDEX_POLL_INTERVAL }}
+
 {% if MULTISITE_ENABLED is defined and MULTISITE_ENABLED == "true" %}
 [websession]
   synchronize = false
diff --git a/gerrit/setup_gerrit.py b/gerrit/setup_gerrit.py
index f549b60..f56f30e 100755
--- a/gerrit/setup_gerrit.py
+++ b/gerrit/setup_gerrit.py
@@ -213,6 +213,7 @@
     with open(GERRIT_CONFIG_DIRECTORY + "high-availability.config", 'w', encoding='utf-8') as f:
         f.write(template.render(
             HA_PEER_URL=os.getenv('HA_PEER_URL'),
+            HA_AUTOREINDEX_POLL_INTERVAL=os.getenv('HA_AUTOREINDEX_POLL_INTERVAL'),
             MULTISITE_ENABLED=os.getenv('MULTISITE_ENABLED')
         ))