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

wangchao316 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 403f5a9f9b [IOTDB-4347]NPE error when session is expired (#7245)
403f5a9f9b is described below

commit 403f5a9f9b08660ebe00a3cc9d289590253d606a
Author: Yifu Zhou <[email protected]>
AuthorDate: Tue Sep 6 17:38:02 2022 +0800

    [IOTDB-4347]NPE error when session is expired (#7245)
    
    [IOTDB-4347]NPE error when session is expired (#7245)
---
 docs/UserGuide/API/Status-Codes.md                       |  3 ++-
 docs/zh/UserGuide/API/Status-Codes.md                    |  3 ++-
 .../java/org/apache/iotdb/db/auth/AuthorityChecker.java  |  6 +++---
 .../apache/iotdb/db/query/control/SessionManager.java    | 16 ++++++++++++----
 .../org/apache/iotdb/db/utils/ErrorHandlingUtils.java    |  8 ++++++--
 .../src/main/java/org/apache/iotdb/rpc/TSStatusCode.java |  1 +
 6 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/docs/UserGuide/API/Status-Codes.md 
b/docs/UserGuide/API/Status-Codes.md
index f3bdeb6ff8..19cacaaf7b 100644
--- a/docs/UserGuide/API/Status-Codes.md
+++ b/docs/UserGuide/API/Status-Codes.md
@@ -43,7 +43,7 @@ With Status Code, instead of writing codes like `if 
(e.getErrorMessage().contain
 Here is a list of Status Code and related message:
 
 |Status Code|Status Type|Meanings|
-|:---|:---|:---|
+|:--|:---|:---|
 |200|SUCCESS_STATUS||
 |201|STILL_EXECUTING_STATUS||
 |202|INVALID_HANDLE_STATUS||
@@ -89,6 +89,7 @@ Here is a list of Status Code and related message:
 |504|START_UP_ERROR|Meet error while starting up|
 |505|SHUT_DOWN_ERROR|Meet error while shutdown|
 |506|MULTIPLE_ERROR|Meet error when executing multiple statements|
+|507|SESSION_EXPIRED|Session expired|
 |600|WRONG_LOGIN_PASSWORD_ERROR|Username or password is wrong|
 |601|NOT_LOGIN_ERROR|Has not logged in|
 |602|NO_PERMISSION_ERROR|No permissions for this operation, please add 
privilege|
diff --git a/docs/zh/UserGuide/API/Status-Codes.md 
b/docs/zh/UserGuide/API/Status-Codes.md
index 0f001f3436..438adf7a95 100644
--- a/docs/zh/UserGuide/API/Status-Codes.md
+++ b/docs/zh/UserGuide/API/Status-Codes.md
@@ -43,7 +43,7 @@ try {
 这里是状态码和相对应信息的列表:
 
 |状态码|状态类型|状态信息|
-|:---|:---|:---|
+|:--|:---|:---|
 |200|SUCCESS_STATUS|成功状态|
 |201|STILL_EXECUTING_STATUS|仍在执行状态|
 |202|INVALID_HANDLE_STATUS|无效句柄状态|
@@ -89,6 +89,7 @@ try {
 |504|START_UP_ERROR|启动错误|
 |505|SHUT_DOWN_ERROR|关机错误|
 |506|MULTIPLE_ERROR|多行语句执行错误|
+|507|SESSION_EXPIRED|会话过期|
 |600|WRONG_LOGIN_PASSWORD_ERROR|用户名或密码错误|
 |601|NOT_LOGIN_ERROR|没有登录|
 |602|NO_PERMISSION_ERROR|没有操作权限|
diff --git 
a/server/src/main/java/org/apache/iotdb/db/auth/AuthorityChecker.java 
b/server/src/main/java/org/apache/iotdb/db/auth/AuthorityChecker.java
index f524f6fd76..33160063b5 100644
--- a/server/src/main/java/org/apache/iotdb/db/auth/AuthorityChecker.java
+++ b/server/src/main/java/org/apache/iotdb/db/auth/AuthorityChecker.java
@@ -39,7 +39,7 @@ import org.slf4j.LoggerFactory;
 import java.util.ArrayList;
 import java.util.List;
 
-import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
+import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onQueryException;
 
 public class AuthorityChecker {
 
@@ -162,8 +162,8 @@ public class AuthorityChecker {
       logger.warn("meet error while checking authorization.", e);
       return RpcUtils.getStatus(TSStatusCode.UNINITIALIZED_AUTH_ERROR, 
e.getMessage());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
-          e, OperationType.CHECK_AUTHORITY, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
+      return onQueryException(
+          e, OperationType.CHECK_AUTHORITY.getName(), 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     }
     return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
   }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java 
b/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java
index f1a1b2592e..c99a676c86 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java
@@ -22,6 +22,7 @@ import org.apache.iotdb.common.rpc.thrift.TSStatus;
 import org.apache.iotdb.commons.auth.AuthException;
 import org.apache.iotdb.commons.auth.entity.PrivilegeType;
 import org.apache.iotdb.commons.conf.IoTDBConstant;
+import org.apache.iotdb.commons.exception.IoTDBException;
 import org.apache.iotdb.db.auth.AuthorityChecker;
 import org.apache.iotdb.db.auth.AuthorizerManager;
 import org.apache.iotdb.db.conf.OperationType;
@@ -50,6 +51,7 @@ import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Consumer;
 
 import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
+import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onQueryException;
 
 public class SessionManager {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(SessionManager.class);
@@ -340,7 +342,7 @@ public class SessionManager {
   /** Check whether specific Session has the authorization to given plan. */
   public TSStatus checkAuthority(PhysicalPlan plan, long sessionId) {
     try {
-      if (!checkAuthorization(plan, sessionIdToUsername.get(sessionId))) {
+      if (!checkAuthorization(plan, getUsername(sessionId))) {
         return RpcUtils.getStatus(
             TSStatusCode.NO_PERMISSION_ERROR,
             "No permissions for this operation, please add privilege "
@@ -351,8 +353,8 @@ public class SessionManager {
       LOGGER.warn("meet error while checking authorization.", e);
       return RpcUtils.getStatus(TSStatusCode.UNINITIALIZED_AUTH_ERROR, 
e.getMessage());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
-          e, OperationType.CHECK_AUTHORITY, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
+      return onQueryException(
+          e, OperationType.CHECK_AUTHORITY.getName(), 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     }
     return null;
   }
@@ -371,7 +373,13 @@ public class SessionManager {
   }
 
   public String getUsername(Long sessionId) {
-    return sessionIdToUsername.get(sessionId);
+    String username = sessionIdToUsername.get(sessionId);
+    if (username == null) {
+      throw new RuntimeException(
+          new IoTDBException(
+              "session expired, please re-login.", 
TSStatusCode.SESSION_EXPIRED.getStatusCode()));
+    }
+    return username;
   }
 
   public ZoneId getZoneId(Long sessionId) {
diff --git 
a/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java 
b/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
index 4b90e70bfd..f45a09fca7 100644
--- a/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
+++ b/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
@@ -76,7 +76,7 @@ public class ErrorHandlingUtils {
     return e;
   }
 
-  public static TSStatus onQueryException(Exception e, String operation) {
+  public static TSStatus onQueryException(Exception e, String operation, 
TSStatusCode statusCode) {
     TSStatus status = tryCatchQueryException(e);
     if (status != null) {
       // ignore logging sg not ready exception
@@ -86,10 +86,14 @@ public class ErrorHandlingUtils {
       }
       return status;
     } else {
-      return onNPEOrUnexpectedException(e, operation, 
TSStatusCode.INTERNAL_SERVER_ERROR);
+      return onNPEOrUnexpectedException(e, operation, statusCode);
     }
   }
 
+  public static TSStatus onQueryException(Exception e, String operation) {
+    return onQueryException(e, operation, TSStatusCode.INTERNAL_SERVER_ERROR);
+  }
+
   public static TSStatus onQueryException(Exception e, OperationType 
operation) {
     return onQueryException(e, operation.getName());
   }
diff --git a/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java 
b/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
index b7d095e5c7..edb1675f4d 100644
--- a/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
+++ b/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
@@ -103,6 +103,7 @@ public enum TSStatusCode {
   START_UP_ERROR(504),
   SHUT_DOWN_ERROR(505),
   MULTIPLE_ERROR(506),
+  SESSION_EXPIRED(507),
 
   WRONG_LOGIN_PASSWORD_ERROR(600),
   NOT_LOGIN_ERROR(601),

Reply via email to