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

Allow liveness check using query parameter during server shutdown #10111

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -23,6 +23,8 @@
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.GET;
Expand All @@ -46,6 +48,9 @@
@Path("/")
public class HealthCheckResource {

@Inject
private AtomicBoolean _shutDownInProgress;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

who is ingesting this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In AdminApiApplication


@Inject
@Named(AdminApiApplication.SERVER_INSTANCE_ID)
private String _instanceId;
Expand All @@ -62,11 +67,12 @@ public class HealthCheckResource {
@ApiResponse(code = 503, message = "Server is not healthy")
})
public String checkHealth(
@ApiParam(value = "health check type: liveness or readiness") @QueryParam("checkType") String checkType) {
@ApiParam(value = "health check type: liveness or readiness") @QueryParam("checkType") @Nullable
String checkType) {
if ("liveness".equalsIgnoreCase(checkType)) {
return "OK";
return checkLiveness();
} else {
return getReadinessStatus();
return checkReadiness();
}
}

Expand All @@ -92,10 +98,11 @@ public String checkLiveness() {
@ApiResponse(code = 503, message = "Server is not ready to serve queries")
})
public String checkReadiness() {
return getReadinessStatus();
}

private String getReadinessStatus() throws WebApplicationException {
if (_shutDownInProgress.get()) {
String errMessage = "Server is shutting down";
throw new WebApplicationException(errMessage,
Response.status(Response.Status.SERVICE_UNAVAILABLE).entity(errMessage).build());
}
Status status = ServiceStatus.getServiceStatus(_instanceId);
if (status == Status.GOOD) {
_serverMetrics.addMeteredGlobalValue(ServerMeter.READINESS_CHECK_OK_CALLS, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public class ShutDownFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
if (_shutDownInProgress.get() && !(requestContext.getMethod().equals("GET") && requestContext.getUriInfo().getPath()
.equals("health/liveness"))) {
throw new WebApplicationException("Server is shutting down", Response.Status.SERVICE_UNAVAILABLE);
// NOTE: Allow health check requests for liveness check
if (_shutDownInProgress.get() && !(requestContext.getUriInfo().getMatchedResources()
.get(0) instanceof HealthCheckResource)) {
String errMessage = "Server is shutting down";
throw new WebApplicationException(errMessage,
Response.status(Response.Status.SERVICE_UNAVAILABLE).entity(errMessage).build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,44 @@ public void checkHealthProbes() {
when(mockFailureCallback.getServiceStatus()).thenReturn(ServiceStatus.Status.BAD);

assertEquals(_webTarget.path(livenessPath).request().get(Response.class).getStatus(), 200);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "liveness").request().get(Response.class).getStatus(), 200);
assertEquals(_webTarget.path(healthPath).request().get(Response.class).getStatus(), 503);
assertEquals(_webTarget.path(readinessPath).request().get(Response.class).getStatus(), 503);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "readiness").request().get(Response.class).getStatus(),
503);

ServiceStatus.setServiceStatusCallback(_instanceId, mockSuccessCallback);
assertEquals(_webTarget.path(livenessPath).request().get(Response.class).getStatus(), 200);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "liveness").request().get(Response.class).getStatus(), 200);
assertEquals(_webTarget.path(healthPath).request().get(Response.class).getStatus(), 200);
assertEquals(_webTarget.path(readinessPath).request().get(Response.class).getStatus(), 200);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "readiness").request().get(Response.class).getStatus(),
200);

ServiceStatus.setServiceStatusCallback(_instanceId, mockFailureCallback);
assertEquals(_webTarget.path(livenessPath).request().get(Response.class).getStatus(), 200);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "liveness").request().get(Response.class).getStatus(), 200);
assertEquals(_webTarget.path(healthPath).request().get(Response.class).getStatus(), 503);
assertEquals(_webTarget.path(readinessPath).request().get(Response.class).getStatus(), 503);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "readiness").request().get(Response.class).getStatus(),
503);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "liveness").request().get(Response.class).getStatus(), 200);

// Start shutting down the HTTP server, only liveness check should go through
ServiceStatus.setServiceStatusCallback(_instanceId, mockSuccessCallback);
_adminApiApplication.startShuttingDown();
assertEquals(_webTarget.path(livenessPath).request().get(Response.class).getStatus(), 200);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "liveness").request().get(Response.class).getStatus(), 200);
assertEquals(_webTarget.path(healthPath).request().get(Response.class).getStatus(), 503);
assertEquals(_webTarget.path(readinessPath).request().get(Response.class).getStatus(), 503);
assertEquals(
_webTarget.path(healthPath).queryParam("checkType", "readiness").request().get(Response.class).getStatus(),
503);
}
}