Initial implementation of health servlet
The health servlet allows the plugin to be used to return responses
to HAProxy's health check function [1]. For example if the http(s)
backend is configured with something like:
option httpchk GET /plugins/high-availability/health
then GET calls to the /health URL will return HTTP status 204, thus
indicating that the server is healthy (the default response).
Sending a DELETE request to the same /health URL will set the status
to 'unhealthy', and then any subsequent GET requests will return HTTP
503.
Sending a POST request sets the status back to 'healthy'.
Documentation and tests will be added in follow up changes.
[1] http://cbonte.github.io/haproxy-dconv/1.7/configuration.html#option%20httpchk
Change-Id: Ife2e6176347c5a84f328b9652a2613080572c994
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/HttpModule.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/HttpModule.java
index 57d1d91..204a206 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/HttpModule.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/HttpModule.java
@@ -15,6 +15,7 @@
package com.ericsson.gerrit.plugins.highavailability;
import com.ericsson.gerrit.plugins.highavailability.forwarder.rest.RestForwarderServletModule;
+import com.ericsson.gerrit.plugins.highavailability.health.HealthServletModule;
import com.ericsson.gerrit.plugins.highavailability.websession.file.FileBasedWebsessionModule;
import com.google.gerrit.httpd.plugins.HttpPluginModule;
import com.google.inject.Inject;
@@ -30,6 +31,7 @@
@Override
protected void configureServlets() {
install(new RestForwarderServletModule());
+ install(new HealthServletModule());
if (config.websession().synchronize()) {
install(new FileBasedWebsessionModule());
}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/health/HealthServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/health/HealthServlet.java
new file mode 100644
index 0000000..84616d7
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/health/HealthServlet.java
@@ -0,0 +1,63 @@
+// Copyright (C) 2017 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.ericsson.gerrit.plugins.highavailability.health;
+
+import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
+import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
+
+import com.google.inject.Singleton;
+import java.io.IOException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Singleton
+public class HealthServlet extends HttpServlet {
+ private static final Logger log = LoggerFactory.getLogger(HealthServlet.class);
+ private static final long serialVersionUID = -1L;
+
+ private boolean healthy;
+
+ HealthServlet() {
+ this.healthy = true;
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse rsp) {
+ this.healthy = true;
+ rsp.setStatus(SC_NO_CONTENT);
+ }
+
+ @Override
+ protected void doDelete(HttpServletRequest req, HttpServletResponse rsp) {
+ this.healthy = false;
+ rsp.setStatus(SC_NO_CONTENT);
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse rsp) {
+ if (healthy) {
+ rsp.setStatus(SC_NO_CONTENT);
+ } else {
+ try {
+ rsp.sendError(SC_SERVICE_UNAVAILABLE);
+ } catch (IOException e) {
+ log.error("Failed to send error response", e);
+ }
+ }
+ }
+}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/health/HealthServletModule.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/health/HealthServletModule.java
new file mode 100644
index 0000000..f3318a9
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/health/HealthServletModule.java
@@ -0,0 +1,24 @@
+// Copyright (C) 2017 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.ericsson.gerrit.plugins.highavailability.health;
+
+import com.google.gerrit.httpd.plugins.HttpPluginModule;
+
+public class HealthServletModule extends HttpPluginModule {
+ @Override
+ protected void configureServlets() {
+ serve("/health").with(HealthServlet.class);
+ }
+}