Skip to content

Commit 403f5a9

Browse files
authored
[IOTDB-4347]NPE error when session is expired (#7245)
[IOTDB-4347]NPE error when session is expired (#7245)
1 parent 22c8ac1 commit 403f5a9

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

docs/UserGuide/API/Status-Codes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ With Status Code, instead of writing codes like `if (e.getErrorMessage().contain
4343
Here is a list of Status Code and related message:
4444

4545
|Status Code|Status Type|Meanings|
46-
|:---|:---|:---|
46+
|:--|:---|:---|
4747
|200|SUCCESS_STATUS||
4848
|201|STILL_EXECUTING_STATUS||
4949
|202|INVALID_HANDLE_STATUS||
@@ -89,6 +89,7 @@ Here is a list of Status Code and related message:
8989
|504|START_UP_ERROR|Meet error while starting up|
9090
|505|SHUT_DOWN_ERROR|Meet error while shutdown|
9191
|506|MULTIPLE_ERROR|Meet error when executing multiple statements|
92+
|507|SESSION_EXPIRED|Session expired|
9293
|600|WRONG_LOGIN_PASSWORD_ERROR|Username or password is wrong|
9394
|601|NOT_LOGIN_ERROR|Has not logged in|
9495
|602|NO_PERMISSION_ERROR|No permissions for this operation, please add privilege|

docs/zh/UserGuide/API/Status-Codes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ try {
4343
这里是状态码和相对应信息的列表:
4444

4545
|状态码|状态类型|状态信息|
46-
|:---|:---|:---|
46+
|:--|:---|:---|
4747
|200|SUCCESS_STATUS|成功状态|
4848
|201|STILL_EXECUTING_STATUS|仍在执行状态|
4949
|202|INVALID_HANDLE_STATUS|无效句柄状态|
@@ -89,6 +89,7 @@ try {
8989
|504|START_UP_ERROR|启动错误|
9090
|505|SHUT_DOWN_ERROR|关机错误|
9191
|506|MULTIPLE_ERROR|多行语句执行错误|
92+
|507|SESSION_EXPIRED|会话过期|
9293
|600|WRONG_LOGIN_PASSWORD_ERROR|用户名或密码错误|
9394
|601|NOT_LOGIN_ERROR|没有登录|
9495
|602|NO_PERMISSION_ERROR|没有操作权限|

server/src/main/java/org/apache/iotdb/db/auth/AuthorityChecker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import java.util.ArrayList;
4040
import java.util.List;
4141

42-
import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
42+
import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onQueryException;
4343

4444
public class AuthorityChecker {
4545

@@ -162,8 +162,8 @@ public static TSStatus checkAuthority(Statement statement, long sessionId) {
162162
logger.warn("meet error while checking authorization.", e);
163163
return RpcUtils.getStatus(TSStatusCode.UNINITIALIZED_AUTH_ERROR, e.getMessage());
164164
} catch (Exception e) {
165-
return onNPEOrUnexpectedException(
166-
e, OperationType.CHECK_AUTHORITY, TSStatusCode.EXECUTE_STATEMENT_ERROR);
165+
return onQueryException(
166+
e, OperationType.CHECK_AUTHORITY.getName(), TSStatusCode.EXECUTE_STATEMENT_ERROR);
167167
}
168168
return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
169169
}

server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.iotdb.commons.auth.AuthException;
2323
import org.apache.iotdb.commons.auth.entity.PrivilegeType;
2424
import org.apache.iotdb.commons.conf.IoTDBConstant;
25+
import org.apache.iotdb.commons.exception.IoTDBException;
2526
import org.apache.iotdb.db.auth.AuthorityChecker;
2627
import org.apache.iotdb.db.auth.AuthorizerManager;
2728
import org.apache.iotdb.db.conf.OperationType;
@@ -50,6 +51,7 @@
5051
import java.util.function.Consumer;
5152

5253
import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
54+
import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onQueryException;
5355

5456
public class SessionManager {
5557
private static final Logger LOGGER = LoggerFactory.getLogger(SessionManager.class);
@@ -340,7 +342,7 @@ public boolean checkAuthorization(PhysicalPlan plan, String username) throws Aut
340342
/** Check whether specific Session has the authorization to given plan. */
341343
public TSStatus checkAuthority(PhysicalPlan plan, long sessionId) {
342344
try {
343-
if (!checkAuthorization(plan, sessionIdToUsername.get(sessionId))) {
345+
if (!checkAuthorization(plan, getUsername(sessionId))) {
344346
return RpcUtils.getStatus(
345347
TSStatusCode.NO_PERMISSION_ERROR,
346348
"No permissions for this operation, please add privilege "
@@ -351,8 +353,8 @@ public TSStatus checkAuthority(PhysicalPlan plan, long sessionId) {
351353
LOGGER.warn("meet error while checking authorization.", e);
352354
return RpcUtils.getStatus(TSStatusCode.UNINITIALIZED_AUTH_ERROR, e.getMessage());
353355
} catch (Exception e) {
354-
return onNPEOrUnexpectedException(
355-
e, OperationType.CHECK_AUTHORITY, TSStatusCode.EXECUTE_STATEMENT_ERROR);
356+
return onQueryException(
357+
e, OperationType.CHECK_AUTHORITY.getName(), TSStatusCode.EXECUTE_STATEMENT_ERROR);
356358
}
357359
return null;
358360
}
@@ -371,7 +373,13 @@ public TimeZone getCurrSessionTimeZone() {
371373
}
372374

373375
public String getUsername(Long sessionId) {
374-
return sessionIdToUsername.get(sessionId);
376+
String username = sessionIdToUsername.get(sessionId);
377+
if (username == null) {
378+
throw new RuntimeException(
379+
new IoTDBException(
380+
"session expired, please re-login.", TSStatusCode.SESSION_EXPIRED.getStatusCode()));
381+
}
382+
return username;
375383
}
376384

377385
public ZoneId getZoneId(Long sessionId) {

server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static Throwable getRootCause(Throwable e) {
7676
return e;
7777
}
7878

79-
public static TSStatus onQueryException(Exception e, String operation) {
79+
public static TSStatus onQueryException(Exception e, String operation, TSStatusCode statusCode) {
8080
TSStatus status = tryCatchQueryException(e);
8181
if (status != null) {
8282
// ignore logging sg not ready exception
@@ -86,10 +86,14 @@ public static TSStatus onQueryException(Exception e, String operation) {
8686
}
8787
return status;
8888
} else {
89-
return onNPEOrUnexpectedException(e, operation, TSStatusCode.INTERNAL_SERVER_ERROR);
89+
return onNPEOrUnexpectedException(e, operation, statusCode);
9090
}
9191
}
9292

93+
public static TSStatus onQueryException(Exception e, String operation) {
94+
return onQueryException(e, operation, TSStatusCode.INTERNAL_SERVER_ERROR);
95+
}
96+
9397
public static TSStatus onQueryException(Exception e, OperationType operation) {
9498
return onQueryException(e, operation.getName());
9599
}

service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public enum TSStatusCode {
103103
START_UP_ERROR(504),
104104
SHUT_DOWN_ERROR(505),
105105
MULTIPLE_ERROR(506),
106+
SESSION_EXPIRED(507),
106107

107108
WRONG_LOGIN_PASSWORD_ERROR(600),
108109
NOT_LOGIN_ERROR(601),

0 commit comments

Comments
 (0)