Disable querychanges for gerrit replicas

The `querychanges` healthcheck cannot ever be executed on a gerrit
replica since it does not have any change index.

Automatically disable `querychanges` healthcheck for gerrit replica,
regardless of its `enabled` value in configuration.

Bug: Issue 13545
Change-Id: I0028fcac2d7bf156f14f7c108d5ead3e9c05f67c
diff --git a/README.md b/README.md
index e5ad599..0fe07ba 100644
--- a/README.md
+++ b/README.md
@@ -26,8 +26,8 @@
 
 Copy the healthcheck.jar into the Gerrit's /plugins directory and wait for the plugin to be automatically loaded.
 The healthcheck plugin is compatible with both Gerrit master and slave setups. The only difference to bear in mind
-is that some checks may not be successful on slaves (e.g. query changes) because the associated subsystem is switched
-off.
+is that some checks will be automatically disabled on slaves (e.g. query changes) because the associated subsystem
+is switched off.
 
 ## How to use
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
index a19c934..126c26f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.healthcheck;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.QUERYCHANGES;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.MoreObjects;
@@ -23,9 +24,11 @@
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.config.AllProjectsName;
 import com.google.gerrit.server.config.AllUsersName;
+import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
+import java.util.Collections;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -48,16 +51,22 @@
   private final AllUsersName allUsersName;
 
   private final Config config;
+  private final boolean isReplica;
+
+  private static final Set<String> HEALTH_CHECK_DISABLED_FOR_REPLICAS =
+      Collections.singleton(QUERYCHANGES);
 
   @Inject
   public HealthCheckConfig(
       PluginConfigFactory configFactory,
       @PluginName String pluginName,
       AllProjectsName allProjectsName,
-      AllUsersName allUsersName) {
+      AllUsersName allUsersName,
+      @GerritServerConfig Config gerritConfig) {
     config = configFactory.getGlobalPluginConfig(pluginName);
     this.allProjectsName = allProjectsName;
     this.allUsersName = allUsersName;
+    isReplica = gerritConfig.getBoolean("container", "slave", false);
   }
 
   @VisibleForTesting
@@ -72,6 +81,7 @@
     }
     allProjectsName = new AllProjectsName("All-Projects");
     allUsersName = new AllUsersName("All-Users");
+    isReplica = false;
   }
 
   @VisibleForTesting
@@ -125,6 +135,9 @@
   }
 
   public boolean healthCheckEnabled(String healthCheckName) {
+    if (isReplica && HEALTH_CHECK_DISABLED_FOR_REPLICAS.contains(healthCheckName)) {
+      return false;
+    }
     return config.getBoolean(
         HEALTHCHECK, checkNotNull(healthCheckName), "enabled", HEALTH_CHECK_ENABLED_DEFAULT);
   }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
index 186693e..ef5dc8e 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
@@ -135,6 +135,15 @@
   }
 
   @Test
+  @GerritConfig(name = "container.slave", value = "true")
+  public void shouldReturnQueryChangesAsDisabledForSlave() throws Exception {
+    RestResponse resp = getHealthCheckStatus();
+    resp.assertOK();
+
+    assertCheckResult(getResponseJson(resp), QUERYCHANGES, "disabled");
+  }
+
+  @Test
   public void shouldReturnQueryChangesMultipleTimesCheck() throws Exception {
     createChange("refs/for/master");
     getHealthCheckStatus();