Skip to content

Commit

Permalink
Add health check RESTful API
Browse files Browse the repository at this point in the history
Add health check RESTful API in Worker HTTP Server.
			pr-link: #18604
			change-id: cid-9a2a13e98df8e6f5c7298e4cd32af2747dc365da
  • Loading branch information
JiamingMai committed May 10, 2024
1 parent cf5ebda commit db91042
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ public static HttpRequestUri of(List<String> fields) {
httpRequestUri.setPort(80);
}

httpRequestUri.setVersion(fields.get(1));
httpRequestUri.setMappingPath(fields.get(2));
if (fields.size() >= 3) {
httpRequestUri.setVersion(fields.get(1));
httpRequestUri.setMappingPath(fields.get(2));
} else {
httpRequestUri.setVersion("v1");
httpRequestUri.setMappingPath(fields.get(1));
return httpRequestUri;
}

String lastField = fields.get(fields.size() - 1);
if (lastField.startsWith("?")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ private HttpResponseContext dispatch(HttpRequest httpRequest)
List<String> fields = HttpRequestUtil.extractFieldsFromHttpRequestUri(requestUri);
HttpRequestUri httpRequestUri = HttpRequestUri.of(fields);

if (httpRequestUri.getVersion().startsWith("health")) {
HttpResponse response = new DefaultHttpResponse(httpRequest.protocolVersion(), OK);
HttpResponseContext httpResponseContext = new HttpResponseContext(response, null);
return httpResponseContext;
}

switch (httpRequest.method().name()) {
case "GET":
return dispatchGetRequest(httpRequest, httpRequestUri);
Expand Down Expand Up @@ -175,12 +169,24 @@ private HttpResponseContext dispatchGetRequest(
return doGetFileStatus(httpRequest, httpRequestUri);
case "load":
return doLoad(httpRequest, httpRequestUri);
case "health":
return doHealthCheck(httpRequest, httpRequestUri);
default:
// TODO(JiamingMai): this should not happen, we should throw an exception here
return null;
}
}

private HttpResponseContext doHealthCheck(HttpRequest httpRequest,
HttpRequestUri httpRequestUri) {
FullHttpResponse response = new DefaultFullHttpResponse(httpRequest.protocolVersion(), OK,
Unpooled.wrappedBuffer("worker is active".getBytes()));
response.headers()
.set(CONTENT_TYPE, TEXT_PLAIN)
.setInt(CONTENT_LENGTH, response.content().readableBytes());
return new HttpResponseContext(response, null);
}

private HttpResponseContext doWritePage(HttpRequest httpRequest, HttpRequestUri httpRequestUri)
throws PageNotFoundException {
List<String> remainingFields = httpRequestUri.getRemainingFields();
Expand Down

0 comments on commit db91042

Please sign in to comment.