Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #127 from minhhoangvn/master
Browse files Browse the repository at this point in the history
fix: error invalid call sequence with exception in initiated gRPC request
  • Loading branch information
minhhoangvn authored Jun 29, 2022
2 parents 6dbdaa4 + f7d0816 commit fd05ecf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
27 changes: 21 additions & 6 deletions src/main/java/vn/zalopay/benchmark/GRPCSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ public SampleResult sample(Entry ignored) {
sampleResult.sampleStart();
} catch (Exception e) {
log.error("An unknown error occurred before the GRPC request was initiated, and the stack trace is as follows: ", e);
sampleResult.setSuccessful(false);
sampleResult.setResponseCode("500");
sampleResult.setDataType(SampleResult.TEXT);
sampleResult.setResponseMessage("GRPCSampler Exception: An unknown exception occurred before the GRPC request was initiated. See the console print log for a detailed stack trace.");
startSamplerToStopIfCatchException(sampleResult);
generateErrorResultInInitGRPCRequest(sampleResult, e);
return sampleResult;
}

Expand All @@ -101,7 +99,8 @@ public SampleResult sample(Entry ignored) {
sampleResult.setDataType(SampleResult.TEXT);
sampleResult.setResponseData(grpcResponse.getGrpcMessageString().getBytes(StandardCharsets.UTF_8));
} catch (RuntimeException e) {
errorResult(grpcResponse, sampleResult, e);
startSamplerToStopIfCatchException(sampleResult);
generateErrorResult(grpcResponse, sampleResult, e);
}

return sampleResult;
Expand Down Expand Up @@ -138,7 +137,18 @@ private String whoAmI() {
getName();
}

private void errorResult(GrpcResponse grpcResponse, SampleResult sampleResult, Exception e) {
private void generateErrorResultInInitGRPCRequest(SampleResult sampleResult, Exception e) {
String msg = String.format("GRPCSampler Exception: An unknown exception occurred before the GRPC " +
"request was initiated. %s", e.getCause().getMessage());
sampleResult.setSampleLabel("Error When Initiated gRPC");
sampleResult.setSuccessful(false);
sampleResult.setResponseCode("500");
sampleResult.setDataType(SampleResult.TEXT);
sampleResult.setResponseData(msg.getBytes(StandardCharsets.UTF_8));
sampleResult.setResponseMessage(msg);
}

private void generateErrorResult(GrpcResponse grpcResponse, SampleResult sampleResult, Exception e) {
try {
sampleResult.setSuccessful(false);
sampleResult.setResponseCode("500");
Expand All @@ -153,6 +163,11 @@ private void errorResult(GrpcResponse grpcResponse, SampleResult sampleResult, E
}
}

private void startSamplerToStopIfCatchException(SampleResult sampleResult) {
if (sampleResult.getStartTime() == 0L)
sampleResult.sampleStart();
}

/**
* GETTER AND SETTER
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vn/zalopay/benchmark/core/ClientCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ private void init(String hostPort, String testProtoFiles, String libFolder, Stri

try {
fileDescriptorSet = ProtocInvoker.forConfig(testProtoFiles, libFolder).invoke();
} catch (Throwable t) {
} catch (Exception e) {
shutdownNettyChannel();
throw new RuntimeException("Unable to resolve service by invoking protoc", t);
throw new RuntimeException("Unable to resolve service by invoking protoc", e);
}

// Set up the dynamic client and make the call.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vn/zalopay/benchmark/core/ClientList.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public static ServiceResolver getServiceResolver(String protoFile, String libFol
serviceResolverMap.put(serviceResolverKey, serviceResolver);
return serviceResolver;
}
} catch (Throwable t) {
throw new RuntimeException("Unable to resolve service by invoking protoc", t);
} catch (Exception e) {
throw new RuntimeException("Unable to resolve service by invoking protoc", e);
}

throw new RuntimeException("Unable to resolve service by invoking protoc");
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/vn/zalopay/benchmark/core/protobuf/ProtocInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,30 @@ public FileDescriptorSet invoke() throws ProtocInvocationException {

private void invokeBinary(ImmutableList<String> protocArgs) throws ProtocInvocationException {
int status;
String[] protocLogLines;
String[] protocInfoLogLines;
String[] protocErrorLogLines;

// The "protoc" library unconditionally writes to stdout. So, we replace stdout right before
// calling into the library in order to gather its output.
PrintStream stdoutBackup = System.out;
PrintStream stderrBackup = System.err;
try {
ByteArrayOutputStream protocStdout = new ByteArrayOutputStream();
ByteArrayOutputStream protocStderr = new ByteArrayOutputStream();
System.setOut(new PrintStream(protocStdout));

System.setErr(new PrintStream(protocStderr));
status = Protoc.runProtoc(protocArgs.toArray(new String[0]));
protocLogLines = protocStdout.toString().split("\n");
protocInfoLogLines = protocStdout.toString().split("\n");
protocErrorLogLines = protocStderr.toString().split("\n");
} catch (IOException | InterruptedException e) {
throw new ProtocInvocationException("Unable to execute protoc binary", e);
} finally {
// Restore stdout.
System.setOut(stdoutBackup);
System.setErr(stderrBackup);
}
if (status != 0) {
// If protoc failed, we dump its output as a warning.
logger.warn("Protoc invocation failed with status: " + status);
for (String line : protocLogLines) {
logger.warn("[Protoc log] " + line);
}

throw new ProtocInvocationException(
String.format("Got exit code [%d] from protoc with args [%s]", status, protocArgs));
protocInvokerErrorHandler(protocArgs, status, protocInfoLogLines, protocErrorLogLines);
}
}

Expand Down Expand Up @@ -261,4 +259,23 @@ private ProtocInvocationException(String message, Throwable cause) {
}
}

private void protocInvokerErrorHandler(ImmutableList<String> protocArgs, int status, String[] protocInfoLogLines,
String[] protocErrorLogLines) throws ProtocInvocationException {
// If protoc failed, we dump its output as a warning.
logger.error("Protoc invocation failed with status: " + status);
for (String line : protocInfoLogLines) {
logger.error("[Protoc log] " + line);
}

for (String line : protocErrorLogLines) {
logger.error("[Protoc error log] " + line);
}

throw new ProtocInvocationException(
String.format("Got error exit code [%d] from protoc: protoc command [%s] has " +
"error" +
" [%s]",
status,
String.join("\n", protocInfoLogLines), String.join("\n", protocErrorLogLines)));
}
}

0 comments on commit fd05ecf

Please sign in to comment.