Skip to content

Commit

Permalink
Fix restAPi return null message when meets multiple error (#12621)
Browse files Browse the repository at this point in the history
  • Loading branch information
HTHou authored May 30, 2024
1 parent 36c7e09 commit a4a7be0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,26 @@
import org.apache.iotdb.service.rpc.thrift.TSExecuteStatementResp;
import org.apache.iotdb.service.rpc.thrift.TSFetchResultsResp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Proxy;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class RpcUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(RpcUtils.class);

/** How big should the default read and write buffers be? Defaults to 1KB */
public static final int THRIFT_DEFAULT_BUF_CAPACITY = 1024;

Expand Down Expand Up @@ -139,6 +146,18 @@ public static TSStatus getStatus(TSStatusCode tsStatusCode) {
public static TSStatus getStatus(List<TSStatus> statusList) {
TSStatus status = new TSStatus(TSStatusCode.MULTIPLE_ERROR.getStatusCode());
status.setSubStatus(statusList);
StringBuilder errMsg = new StringBuilder().append("Multiple error occur, messages: ");
Set<String> msgSet = new HashSet<>();
for (TSStatus subStatus : statusList) {
if (subStatus.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()
&& subStatus.getCode() != TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
if (!msgSet.contains(status.getMessage())) {
errMsg.append(status.getMessage()).append("; ");
msgSet.add(status.getMessage());
}
}
}
LOGGER.warn(errMsg.toString());
return status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.iotdb.db.protocol.rest.v2.impl;

import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.conf.rest.IoTDBRestServiceDescriptor;
Expand Down Expand Up @@ -51,6 +52,7 @@
import javax.ws.rs.core.SecurityContext;

import java.time.ZoneId;
import java.util.List;

public class RestApiServiceImpl extends RestApiService {

Expand Down Expand Up @@ -108,17 +110,7 @@ public Response executeNonQueryStatement(SQL sql, SecurityContext securityContex
schemaFetcher,
config.getQueryTimeoutThreshold());

return Response.ok()
.entity(
(result.status.code == TSStatusCode.SUCCESS_STATUS.getStatusCode()
|| result.status.code == TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode())
? new ExecutionStatus()
.code(TSStatusCode.SUCCESS_STATUS.getStatusCode())
.message(TSStatusCode.SUCCESS_STATUS.name())
: new ExecutionStatus()
.code(result.status.getCode())
.message(result.status.getMessage()))
.build();
return responseGenerateHelper(result);
} catch (Exception e) {
return Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
} finally {
Expand Down Expand Up @@ -211,31 +203,7 @@ public Response insertRecords(
partitionFetcher,
schemaFetcher,
config.getQueryTimeoutThreshold());
if (result.status.code == TSStatusCode.SUCCESS_STATUS.getStatusCode()
|| result.status.code == TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
return Response.ok()
.entity(
new ExecutionStatus()
.code(TSStatusCode.SUCCESS_STATUS.getStatusCode())
.message(TSStatusCode.SUCCESS_STATUS.name()))
.build();
} else if (result.status.message == null
&& result.status.subStatus != null
&& result.status.subStatus.size() > 0) {
return Response.ok()
.entity(
new ExecutionStatus()
.code(result.status.getCode())
.message(result.status.subStatus.get(0).message))
.build();
} else {
return Response.ok()
.entity(
new ExecutionStatus()
.code(result.status.getCode())
.message(result.status.getMessage()))
.build();
}
return responseGenerateHelper(result);

} catch (Exception e) {
return Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
Expand Down Expand Up @@ -282,17 +250,7 @@ public Response insertTablet(
schemaFetcher,
config.getQueryTimeoutThreshold());

return Response.ok()
.entity(
(result.status.code == TSStatusCode.SUCCESS_STATUS.getStatusCode()
|| result.status.code == TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode())
? new ExecutionStatus()
.code(TSStatusCode.SUCCESS_STATUS.getStatusCode())
.message(TSStatusCode.SUCCESS_STATUS.name())
: new ExecutionStatus()
.code(result.status.getCode())
.message(result.status.getMessage()))
.build();
return responseGenerateHelper(result);
} catch (Exception e) {
return Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
} finally {
Expand All @@ -301,4 +259,38 @@ public Response insertTablet(
}
}
}

private Response responseGenerateHelper(ExecutionResult result) {
if (result.status.code == TSStatusCode.SUCCESS_STATUS.getStatusCode()
|| result.status.code == TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
return Response.ok()
.entity(
new ExecutionStatus()
.code(TSStatusCode.SUCCESS_STATUS.getStatusCode())
.message(TSStatusCode.SUCCESS_STATUS.name()))
.build();
} else if (result.status.code == TSStatusCode.MULTIPLE_ERROR.getStatusCode()) {
List<TSStatus> subStatus = result.status.getSubStatus();
StringBuilder errMsg = new StringBuilder();
for (TSStatus status : subStatus) {
if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()
&& status.getCode() != TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
errMsg.append(status.getMessage()).append("; ");
}
}
return Response.ok()
.entity(
new ExecutionStatus()
.code(TSStatusCode.MULTIPLE_ERROR.getStatusCode())
.message(errMsg.toString()))
.build();
} else {
return Response.ok()
.entity(
new ExecutionStatus()
.code(result.status.getCode())
.message(result.status.getMessage()))
.build();
}
}
}

0 comments on commit a4a7be0

Please sign in to comment.