You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2022/02/18 09:51:42 UTC

[iotdb] branch master updated: [IOTDB-2541] Enhance /ping API to liveness probe (#5057)

This is an automated email from the ASF dual-hosted git repository.

rong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 52a044e  [IOTDB-2541] Enhance /ping API to liveness probe (#5057)
52a044e is described below

commit 52a044e1b2c0941b649cf2a493f263d636fc8dc5
Author: BaiJian <er...@hotmail.com>
AuthorDate: Fri Feb 18 17:51:05 2022 +0800

    [IOTDB-2541] Enhance /ping API to liveness probe (#5057)
---
 docs/UserGuide/API/RestService.md                  | 30 +++++++++++++++++-----
 docs/zh/UserGuide/API/RestService.md               | 30 +++++++++++++++++-----
 .../db/protocol/rest/impl/PingApiServiceImpl.java  | 13 ++++++++++
 .../iotdb/db/service/thrift/ThriftService.java     |  6 +++--
 .../iotdb/db/protocol/rest/IoTDBRestServiceIT.java | 16 +++++++++++-
 5 files changed, 80 insertions(+), 15 deletions(-)

diff --git a/docs/UserGuide/API/RestService.md b/docs/UserGuide/API/RestService.md
index 534564e..01ea548 100644
--- a/docs/UserGuide/API/RestService.md
+++ b/docs/UserGuide/API/RestService.md
@@ -90,6 +90,12 @@ Example request:
 ```shell
 $ curl http://127.0.0.1:18080/ping
 ```
+
+Response status codes:
+
+- `200`: The service is alive.
+- `503`: The service cannot accept any requests now.
+
 Response parameters:
 
 |parameter name  |parameter type |parameter describe|
@@ -98,12 +104,24 @@ Response parameters:
 | message  |  string | message |
 
 Sample response:
-```json
-{
-  "code": 200,
-  "message": "SUCCESS_STATUS"
-}
-```
+
+- With HTTP status code `200`:
+
+  ```json
+  {
+    "code": 200,
+    "message": "SUCCESS_STATUS"
+  }
+  ```
+
+- With HTTP status code `503`:
+
+  ```json
+  {
+    "code": 500,
+    "message": "thrift service is unavailable"
+  }
+  ```
 
 > `/ping` can be accessed without authorization.
 
diff --git a/docs/zh/UserGuide/API/RestService.md b/docs/zh/UserGuide/API/RestService.md
index 6a579a0..1cba5b3 100644
--- a/docs/zh/UserGuide/API/RestService.md
+++ b/docs/zh/UserGuide/API/RestService.md
@@ -84,6 +84,12 @@ ping 接口可以用于线上服务检活。
 ```shell
 $ curl http://127.0.0.1:18080/ping
 ```
+
+返回的 HTTP 状态码:
+
+- `200`:当前服务工作正常,可以接收外部请求。
+- `503`:当前服务出现异常,不能接收外部请求。
+
 响应参数:
 
 |参数名称  |参数类型  |参数描述|
@@ -92,12 +98,24 @@ $ curl http://127.0.0.1:18080/ping
 | message  |  string | 信息提示 |
 
 响应示例:
-```json
-{
-  "code": 200,
-  "message": "SUCCESS_STATUS"
-}
-```
+
+- HTTP 状态码为 `200` 时:
+
+  ```json
+  {
+    "code": 200,
+    "message": "SUCCESS_STATUS"
+  }
+  ```
+
+- HTTP 状态码为 `503` 时:
+
+  ```json
+  {
+    "code": 500,
+    "message": "thrift service is unavailable"
+  }
+  ```
 
 > `/ping` 接口访问不需要鉴权。
 
diff --git a/server/src/main/java/org/apache/iotdb/db/protocol/rest/impl/PingApiServiceImpl.java b/server/src/main/java/org/apache/iotdb/db/protocol/rest/impl/PingApiServiceImpl.java
index 4e753f6..61e025c 100644
--- a/server/src/main/java/org/apache/iotdb/db/protocol/rest/impl/PingApiServiceImpl.java
+++ b/server/src/main/java/org/apache/iotdb/db/protocol/rest/impl/PingApiServiceImpl.java
@@ -19,6 +19,8 @@ package org.apache.iotdb.db.protocol.rest.impl;
 
 import org.apache.iotdb.db.protocol.rest.PingApiService;
 import org.apache.iotdb.db.protocol.rest.model.ExecutionStatus;
+import org.apache.iotdb.db.service.RPCService;
+import org.apache.iotdb.db.service.thrift.ThriftService;
 import org.apache.iotdb.rpc.TSStatusCode;
 
 import javax.ws.rs.core.Response;
@@ -26,8 +28,19 @@ import javax.ws.rs.core.SecurityContext;
 
 public class PingApiServiceImpl extends PingApiService {
 
+  private static final String UNAVAILABLE_SERVICE = "thrift service is unavailable";
+
   @Override
   public Response tryPing(SecurityContext securityContext) {
+    if (RPCService.getInstance().getRPCServiceStatus().equals(ThriftService.STATUS_DOWN)) {
+      return Response.status(Response.Status.SERVICE_UNAVAILABLE)
+          .entity(
+              new ExecutionStatus()
+                  .code(TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode())
+                  .message(UNAVAILABLE_SERVICE))
+          .build();
+    }
+
     return Response.ok()
         .entity(
             new ExecutionStatus()
diff --git a/server/src/main/java/org/apache/iotdb/db/service/thrift/ThriftService.java b/server/src/main/java/org/apache/iotdb/db/service/thrift/ThriftService.java
index b6acaa8..836f6b9 100644
--- a/server/src/main/java/org/apache/iotdb/db/service/thrift/ThriftService.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/thrift/ThriftService.java
@@ -33,8 +33,10 @@ import java.util.concurrent.CountDownLatch;
 public abstract class ThriftService implements IService {
 
   private static final Logger logger = LoggerFactory.getLogger(ThriftService.class);
-  private static final String STATUS_UP = "UP";
-  private static final String STATUS_DOWN = "DOWN";
+
+  public static final String STATUS_UP = "UP";
+  public static final String STATUS_DOWN = "DOWN";
+
   protected final String mbeanName =
       String.format(
           "%s:%s=%s", IoTDBConstant.IOTDB_PACKAGE, IoTDBConstant.JMX_TYPE, getID().getJmxName());
diff --git a/server/src/test/java/org/apache/iotdb/db/protocol/rest/IoTDBRestServiceIT.java b/server/src/test/java/org/apache/iotdb/db/protocol/rest/IoTDBRestServiceIT.java
index 607b46f..d492327 100644
--- a/server/src/test/java/org/apache/iotdb/db/protocol/rest/IoTDBRestServiceIT.java
+++ b/server/src/test/java/org/apache/iotdb/db/protocol/rest/IoTDBRestServiceIT.java
@@ -18,6 +18,8 @@
  */
 package org.apache.iotdb.db.protocol.rest;
 
+import org.apache.iotdb.db.exception.StartupException;
+import org.apache.iotdb.db.service.RPCService;
 import org.apache.iotdb.db.utils.EnvironmentUtils;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -77,8 +79,20 @@ public class IoTDBRestServiceIT {
       HttpEntity responseEntity = response.getEntity();
       String message = EntityUtils.toString(responseEntity, "utf-8");
       JsonObject result = JsonParser.parseString(message).getAsJsonObject();
+      assertEquals(200, response.getStatusLine().getStatusCode());
       assertEquals(200, Integer.parseInt(result.get("code").toString()));
-    } catch (IOException e) {
+
+      // Shutdown RPCService to test
+      RPCService.getInstance().stop();
+      response = httpClient.execute(httpGet);
+      responseEntity = response.getEntity();
+      message = EntityUtils.toString(responseEntity, "utf-8");
+      result = JsonParser.parseString(message).getAsJsonObject();
+      assertEquals(503, response.getStatusLine().getStatusCode());
+      assertEquals(500, Integer.parseInt(result.get("code").toString()));
+      RPCService.getInstance().start();
+
+    } catch (IOException | StartupException e) {
       e.printStackTrace();
       fail(e.getMessage());
     } finally {