Introduce the changes query healthcheck
Make sure that Gerrit is able to perform a query on Lucene and
return a defined set of changes.
Change-Id: I3625b6ad54bb9605c9ddb497923da9f161bbffa5
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 b1fe43d..6c93e91 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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.server.config.PluginConfigFactory;
@@ -27,9 +28,12 @@
@Singleton
public class HealthCheckConfig {
public static final String HEALTHCHECK = "healthcheck";
- private final Config config;
- private static final long HEALTHCHECK_TIMEOUT_DEFAULT = 500L;
public static final HealthCheckConfig DEFAULT_CONFIG = new HealthCheckConfig(null);
+ private static final long HEALTHCHECK_TIMEOUT_DEFAULT = 500L;
+ private static final String QUERY_DEFAULT = "status:open";
+ private static final int LIMIT_DEFAULT = 10;
+
+ private final Config config;
@Inject
public HealthCheckConfig(PluginConfigFactory configFactory, @PluginName String pluginName) {
@@ -57,4 +61,15 @@
return config.getTimeUnit(
HEALTHCHECK, healthCheckName, "timeout", defaultTimeout, TimeUnit.MILLISECONDS);
}
+
+ public String getQuery(String healthCheckName) {
+ String defaultQuery = healthCheckName == null ? QUERY_DEFAULT : getQuery(null);
+ return MoreObjects.firstNonNull(
+ config.getString(HEALTHCHECK, healthCheckName, "query"), defaultQuery);
+ }
+
+ public int getLimit(String healthCheckName) {
+ int defaultLimit = healthCheckName == null ? LIMIT_DEFAULT : getLimit(null);
+ return config.getInt(HEALTHCHECK, healthCheckName, "limit", defaultLimit);
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
index a497a83..9c6b17d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
@@ -21,6 +21,7 @@
import com.googlesource.gerrit.plugins.healthcheck.check.JGitHealthCheck;
import com.googlesource.gerrit.plugins.healthcheck.check.MetricsHandler;
import com.googlesource.gerrit.plugins.healthcheck.check.ProjectsListHealthCheck;
+import com.googlesource.gerrit.plugins.healthcheck.check.QueryChangesHealthCheck;
import com.googlesource.gerrit.plugins.healthcheck.check.ReviewDbHealthCheck;
public class HealthCheckSubsystemsModule extends AbstractModule {
@@ -35,6 +36,7 @@
bindChecker(ReviewDbHealthCheck.class);
bindChecker(JGitHealthCheck.class);
bindChecker(ProjectsListHealthCheck.class);
+ bindChecker(QueryChangesHealthCheck.class);
}
private void bindChecker(Class<? extends HealthCheck> healthCheckClass) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
index 2eb896e..402fa35 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
@@ -18,4 +18,5 @@
String REVIEWDB = "reviewdb";
String JGIT = "jgit";
String PROJECTSLIST = "projectslist";
+ String QUERYCHANGES = "querychanges";
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java
new file mode 100644
index 0000000..67bb733
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/QueryChangesHealthCheck.java
@@ -0,0 +1,71 @@
+// Copyright (C) 2019 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.healthcheck.check;
+
+import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.QUERYCHANGES;
+
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.gerrit.server.query.change.QueryChanges;
+import com.google.gerrit.server.util.ManualRequestContext;
+import com.google.gerrit.server.util.OneOffRequestContext;
+import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class QueryChangesHealthCheck extends AbstractHealthCheck {
+ private static final Logger log = LoggerFactory.getLogger(QueryChangesHealthCheck.class);
+ private final QueryChanges queryChanges;
+ private final int limit;
+ private final OneOffRequestContext oneOffCtx;
+
+ @Inject
+ public QueryChangesHealthCheck(
+ ListeningExecutorService executor,
+ HealthCheckConfig config,
+ QueryChanges queryChanges,
+ OneOffRequestContext oneOffCtx,
+ MetricsHandler.Factory metricsHandlerFactory) {
+ super(executor, config, QUERYCHANGES, metricsHandlerFactory);
+ this.queryChanges = queryChanges;
+ this.limit = config.getLimit(QUERYCHANGES);
+ queryChanges.setLimit(limit);
+ queryChanges.addQuery(config.getQuery(QUERYCHANGES));
+ queryChanges.setStart(0);
+ this.oneOffCtx = oneOffCtx;
+ }
+
+ @Override
+ protected Result doCheck() throws Exception {
+ try (ManualRequestContext ctx = oneOffCtx.open()) {
+ List<?> changes = queryChanges.apply(null);
+ if (changes == null) {
+ log.warn("Cannot query changes: received a null list of results");
+ return Result.FAILED;
+ }
+
+ if (changes.size() < limit) {
+ log.warn(
+ "Query changes did not return enough items: expected {} items but got only {}",
+ limit,
+ changes.size());
+ return Result.FAILED;
+ }
+
+ return Result.PASSED;
+ }
+ }
+}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 5af0790..74ad62c 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -53,4 +53,14 @@
- `healthcheck.<checkName>.timeout` : Specific timeout (msec) for the
healthcheck to complete. Zero means that there is no timeout.
- Default: 500
\ No newline at end of file
+ Default: 500
+
+- `healthcheck.<checkName>.query` : Query to be executed for extracting
+ elements from the check.
+
+ Default: status:open
+
+- `healthcheck.<checkName>.limit` : Maximum number of elements to retrieve from
+ the the check results.
+
+ Default: 10
\ No newline at end of file
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 86a0965..38f807e 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
@@ -17,21 +17,49 @@
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.google.common.truth.Truth.assertThat;
import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.JGIT;
+import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.QUERYCHANGES;
import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.REVIEWDB;
import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.Sandboxed;
import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
+import com.google.inject.AbstractModule;
+import com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames;
import java.io.IOException;
+import org.junit.Before;
import org.junit.Test;
@TestPlugin(name = "healthcheck", sysModule = "com.googlesource.gerrit.plugins.healthcheck.Module")
@Sandboxed
public class HealthCheckIT extends LightweightPluginDaemonTest {
Gson gson = new Gson();
+ HealthCheckConfig config;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+
+ config =
+ server
+ .getTestInjector()
+ .createChildInjector(
+ new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class).annotatedWith(PluginName.class).toInstance("healthcheck");
+ }
+ })
+ .getInstance(HealthCheckConfig.class);
+ int numChanges = config.getLimit(HealthCheckNames.QUERYCHANGES);
+ for (int i = 0; i < numChanges; i++) {
+ createChange("refs/for/master");
+ }
+ }
@Test
public void shouldReturnOkWhenHealthy() throws Exception {
@@ -63,6 +91,17 @@
assertCheckResult(respPayload, JGIT, "passed");
}
+ @Test
+ public void shouldReturnQueryChangesCheck() throws Exception {
+ createChange("refs/for/master");
+ RestResponse resp = getHealthCheckStatus();
+ resp.assertOK();
+
+ JsonObject respPayload = gson.fromJson(resp.getReader(), JsonObject.class);
+
+ assertCheckResult(respPayload, QUERYCHANGES, "passed");
+ }
+
private RestResponse getHealthCheckStatus() throws IOException {
return adminRestSession.get("/config/server/healthcheck~status");
}