Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-3829 MicroProfile Healthcheck 2.1 implementation #4254

Merged
merged 3 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.servlet.http.HttpServletResponse;

import fish.payara.microprofile.healthcheck.config.MetricsHealthCheckConfiguration;

import java.util.HashMap;
import static java.util.logging.Level.WARNING;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.glassfish.api.StartupRunLevel;
Expand Down Expand Up @@ -92,9 +92,14 @@ public class HealthCheckService implements EventListener, ConfigListener {
@Inject
MetricsHealthCheckConfiguration configuration;

private boolean backwardCompatibilityEnabled;

private static final Logger LOG = Logger.getLogger(HealthCheckService.class.getName());

private final Map<String, Set<HealthCheck>> healthChecks = new ConcurrentHashMap<>();
private final Map<String, Set<HealthCheck>> health = new ConcurrentHashMap<>();
private final Map<String, Set<HealthCheck>> readiness = new ConcurrentHashMap<>();
private final Map<String, Set<HealthCheck>> liveness = new ConcurrentHashMap<>();

private final Map<String, ClassLoader> applicationClassLoaders = new ConcurrentHashMap<>();
private final List<String> applicationsLoaded = new CopyOnWriteArrayList<>();

Expand All @@ -104,6 +109,9 @@ public void postConstruct() {
events = Globals.getDefaultBaseServiceLocator().getService(Events.class);
}
events.register(this);
this.backwardCompatibilityEnabled = ConfigProvider.getConfig()
.getOptionalValue("mp.health.enable-backward-compatibility", Boolean.class)
jGauravGupta marked this conversation as resolved.
Show resolved Hide resolved
.orElse(false);
}

@Override
Expand All @@ -112,7 +120,9 @@ public void event(Event event) {
if (event.is(Deployment.APPLICATION_UNLOADED)) {
ApplicationInfo appInfo = Deployment.APPLICATION_UNLOADED.getHook(event);
if (appInfo != null) {
healthChecks.remove(appInfo.getName());
readiness.remove(appInfo.getName());
liveness.remove(appInfo.getName());
health.remove(appInfo.getName());
applicationClassLoaders.remove(appInfo.getName());
applicationsLoaded.remove(appInfo.getName());
jGauravGupta marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -134,15 +144,48 @@ public boolean isEnabled() {
public boolean isSecurityEnabled() {
return Boolean.parseBoolean(configuration.getSecurityEnabled());
}

/**
* Register a HealthCheck to the Set of HealthChecks to execute when performHealthChecks is called.
* Register a Readiness to the Set of HealthChecks to execute when
* performHealthChecks is called.
*
* @param appName The name of the application being deployed
* @param healthCheck The HealthCheck to register
*/
public void registerHealthCheck(String appName, HealthCheck healthCheck) {
// If we don't already have the app registered, we need to create a new Set for it
public void registerReadiness(String appName, HealthCheck healthCheck) {
registerHealthCheck(appName, healthCheck, readiness);
}

/**
* Register a Liveness to the Set of HealthChecks to execute when
* performHealthChecks is called.
*
* @param appName The name of the application being deployed
* @param healthCheck The HealthCheck to register
*/
public void registerLiveness(String appName, HealthCheck healthCheck) {
registerHealthCheck(appName, healthCheck, liveness);
}

/**
* Register a Health to the Set of HealthChecks to execute when
* performHealthChecks is called.
*
* @param appName The name of the application being deployed
* @param healthCheck The HealthCheck to register
*/
public void registerHealth(String appName, HealthCheck healthCheck) {
registerHealthCheck(appName, healthCheck, health);
}

/**
* Register a HealthCheck to the Set of HealthChecks based on appName.
*
* @param appName The name of the application being deployed
* @param healthCheck The HealthCheck to register
*/
private void registerHealthCheck(String appName, HealthCheck healthCheck, Map<String, Set<HealthCheck>> healthChecks) {
// If we don't already have the app registered, we need to create a new Set for it
if (!healthChecks.containsKey(appName)) {
// Sync so that we don't get clashes
synchronized (this) {
Expand Down Expand Up @@ -184,9 +227,26 @@ public void registerClassLoader(String appName, ClassLoader classloader) {
* @param response The response to return
* @throws IOException If there's an issue writing the response
*/
public void performHealthChecks(HttpServletResponse response) throws IOException {
public void performHealthChecks(HttpServletResponse response, HealthCheckType type) throws IOException {
Set<HealthCheckResponse> healthCheckResponses = new HashSet<>();

final Map<String, Set<HealthCheck>> healthChecks;
if (type == HealthCheckType.READINESS) {
healthChecks = readiness;
} else if (type == HealthCheckType.LIVENESS) {
healthChecks = liveness;
} else {
healthChecks = new HashMap<>(health);
readiness.forEach((k, v) -> healthChecks.merge(k, v, (oldValue, newValue) -> {
oldValue.addAll(newValue);
return oldValue;
}));
liveness.forEach((k, v) -> healthChecks.merge(k, v, (oldValue, newValue) -> {
oldValue.addAll(newValue);
return oldValue;
}));
jGauravGupta marked this conversation as resolved.
Show resolved Hide resolved
}

// Iterate over every HealthCheck stored in the Map
for (Map.Entry<String, Set<HealthCheck>> healthChecksEntry : healthChecks.entrySet()) {
for (HealthCheck healthCheck : healthChecksEntry.getValue()) {
Expand Down Expand Up @@ -218,12 +278,13 @@ public void performHealthChecks(HttpServletResponse response) throws IOException

// If we haven't encountered an exception, construct the JSON response
if (response.getStatus() != 500) {
constructResponse(response, healthCheckResponses);
constructResponse(response, healthCheckResponses, type);
}
}

private void constructResponse(HttpServletResponse httpResponse,
Set<HealthCheckResponse> healthCheckResponses) throws IOException {
Set<HealthCheckResponse> healthCheckResponses,
HealthCheckType type) throws IOException {
httpResponse.setContentType("application/json");

// For each HealthCheckResponse we got from executing the health checks...
Expand All @@ -233,7 +294,11 @@ private void constructResponse(HttpServletResponse httpResponse,

// Add the name and state
healthCheckObject.add("name", healthCheckResponse.getName());
healthCheckObject.add("state", healthCheckResponse.getState().toString());
if(backwardCompatibilityEnabled && type == HealthCheckType.HEALTH) {
healthCheckObject.add("state", healthCheckResponse.getState().toString());
} else {
healthCheckObject.add("status", healthCheckResponse.getState().toString());
}

// Add data if present
JsonObjectBuilder healthCheckData = Json.createObjectBuilder();
Expand All @@ -258,10 +323,11 @@ private void constructResponse(HttpServletResponse httpResponse,
JsonObjectBuilder responseObject = Json.createObjectBuilder();

// Set the aggregate outcome
if (httpResponse.getStatus() == 200) {
responseObject.add("outcome", "UP");
String status = httpResponse.getStatus() == 200 ? "UP" : "DOWN";
if (backwardCompatibilityEnabled && type == HealthCheckType.HEALTH) {
responseObject.add("outcome", status);
} else {
responseObject.add("outcome", "DOWN");
responseObject.add("status", status);
jGauravGupta marked this conversation as resolved.
Show resolved Hide resolved
}

// Add all of the checks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2019] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.microprofile.healthcheck;

public enum HealthCheckType {
jGauravGupta marked this conversation as resolved.
Show resolved Hide resolved

READINESS,
LIVENESS,
HEALTH;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
package fish.payara.microprofile.healthcheck.servlet;

import fish.payara.microprofile.healthcheck.HealthCheckService;
import fish.payara.microprofile.healthcheck.HealthCheckType;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
Expand All @@ -53,7 +54,10 @@
* @author Andrew Pielage
*/
public class HealthCheckServlet extends HttpServlet {


private static final String READINESS_ENDPOINT = "/ready";
private static final String LIVENESS_ENDPOINT = "/live";

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
*
Expand All @@ -75,7 +79,15 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
response.sendError(SC_FORBIDDEN, "MicroProfile Health Check Service is disabled");
return;
}
healthCheckService.performHealthChecks(response);

if (READINESS_ENDPOINT.equals(request.getPathInfo())) {
healthCheckService.performHealthChecks(response, HealthCheckType.READINESS);
} else if (LIVENESS_ENDPOINT.equals(request.getPathInfo())) {
healthCheckService.performHealthChecks(response, HealthCheckType.LIVENESS);
} else {
healthCheckService.performHealthChecks(response, HealthCheckType.HEALTH);
}
jGauravGupta marked this conversation as resolved.
Show resolved Hide resolved

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
Expand Down Expand Up @@ -114,7 +126,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
public String getServletInfo() {
return "Short description";
return "HealthCheck Endpoint";
}// </editor-fold>

}
Loading