Skip to content

Commit

Permalink
[pinpoint-apm#4558] Add gRPC StatusError
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Aug 7, 2019
1 parent dac7f65 commit 6e6a2b3
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 6 deletions.
29 changes: 29 additions & 0 deletions grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.grpc;

/**
* @author jaehong.kim
*/
public interface StatusError {

boolean isSimpleError();

String getMessage();

Throwable getThrowable();
}
110 changes: 110 additions & 0 deletions grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2019 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.grpc;

import io.grpc.Status;
import io.grpc.StatusRuntimeException;

/**
* @author jaehong.kim
*/
public class StatusErrors {
private static final String CONNECTION_REFUSED_MESSAGE = "Connection refused: no further information";

public static StatusError throwable(final Throwable t) {
if (t instanceof StatusRuntimeException) {
StatusRuntimeException exception = (StatusRuntimeException) t;
if (exception.getStatus().getCode() == Status.UNAVAILABLE.getCode()) {
final String causeMessage = findCauseMessage(t, CONNECTION_REFUSED_MESSAGE, 2);
if (causeMessage != null) {
final String message = exception.getStatus().getDescription() + ": " + causeMessage;
return new SimpleStatusError(message, t);
}
}
}
return new DefaultStatusError(t);
}

private static String findCauseMessage(final Throwable t, final String message, final int maxDepth) {
int depth = 0;
Throwable cause = t.getCause();
while (cause != null && depth < maxDepth) {
if (cause.getMessage().startsWith(message)) {
return cause.getMessage();
}

if (cause.getCause() == cause) {
break;
}
cause = cause.getCause();
depth += 1;
}
// Not found
return null;
}

private static class SimpleStatusError implements StatusError {
private final String message;
private final Throwable throwable;

public SimpleStatusError(final String message, final Throwable throwable) {
this.message = message;
this.throwable = throwable;
}

@Override
public boolean isSimpleError() {
return true;
}

@Override
public String getMessage() {
return this.message;
}

@Override
public Throwable getThrowable() {
return this.throwable;
}
}

private static class DefaultStatusError implements StatusError {
private final Throwable throwable;

public DefaultStatusError(final Throwable throwable) {
this.throwable = throwable;
}

@Override
public boolean isSimpleError() {
return false;
}

@Override
public String getMessage() {
if (this.throwable != null) {
return this.throwable.getMessage();
}
return "";
}

@Override
public Throwable getThrowable() {
return this.throwable;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.grpc;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.*;

/**
* @author jaehong.kim
*/
public class StatusErrorsTest {
private final Logger logger = LoggerFactory.getLogger(getClass());

@Test
public void throwable() {
StatusError statusError = StatusErrors.throwable(new RuntimeException("test"));
assertEquals("test", statusError.getMessage());
assertFalse(statusError.isSimpleError());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.navercorp.pinpoint.profiler.receiver.grpc;

import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.MessageFormatUtils;
import com.navercorp.pinpoint.grpc.StatusError;
import com.navercorp.pinpoint.grpc.StatusErrors;
import com.navercorp.pinpoint.grpc.trace.PCmdMessage;
import com.navercorp.pinpoint.grpc.trace.PCmdRequest;
import com.navercorp.pinpoint.grpc.trace.PCmdServiceHandshake;
Expand Down Expand Up @@ -142,7 +145,13 @@ public void onNext(PCmdRequest request) {

@Override
public void onError(Throwable t) {
logger.warn("onError:{}", t.getMessage(), t);
final StatusError statusError = StatusErrors.throwable(t);
if (statusError.isSimpleError()) {
logger.info("Failed to command stream, cause={}", statusError.getMessage());
} else {
logger.warn("Failed to command stream, cause={}", statusError.getMessage(), statusError.getThrowable());
}

if (requestStream != null) {
requestStream.onError(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.MessageFormatUtils;
import com.navercorp.pinpoint.grpc.StatusError;
import com.navercorp.pinpoint.grpc.StatusErrors;
import com.navercorp.pinpoint.grpc.trace.AgentGrpc;
import com.navercorp.pinpoint.grpc.trace.PPing;
import io.grpc.stub.ClientCallStreamObserver;
Expand Down Expand Up @@ -78,7 +80,12 @@ public void onNext(PPing ping) {

@Override
public void onError(Throwable t) {
logger.info("{} error Caused by:{}", streamId, t.getMessage(), t);
final StatusError statusError = StatusErrors.throwable(t);
if (statusError.isSimpleError()) {
logger.info("Failed to ping stream, streamId={}, cause={}", streamId, statusError.getMessage());
} else {
logger.warn("Failed to ping stream, streamId={}, cause={}", streamId, statusError.getMessage(), statusError.getThrowable());
}
cancelPingScheduler();
reconnector.reconnect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.navercorp.pinpoint.profiler.sender.grpc;

import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.StatusError;
import com.navercorp.pinpoint.grpc.StatusErrors;
import io.grpc.stub.ClientCallStreamObserver;
import io.grpc.stub.ClientResponseObserver;
import org.slf4j.Logger;
Expand Down Expand Up @@ -56,7 +58,12 @@ public void onNext(RespT value) {

@Override
public void onError(Throwable t) {
logger.info("{} onError:{}", name, t.getMessage(), t);
final StatusError statusError = StatusErrors.throwable(t);
if (statusError.isSimpleError()) {
logger.info("Failed to stream, name={}, cause={}", name, statusError.getMessage());
} else {
logger.warn("Failed to stream, name={}, cause={}", name, statusError.getMessage(), statusError.getThrowable());
}
reconnector.reconnect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.TextFormat;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.StatusError;
import com.navercorp.pinpoint.grpc.StatusErrors;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;

Expand Down Expand Up @@ -57,10 +59,14 @@ public void onNext(ResT response) {

@Override
public void onError(Throwable throwable) {
// Retry
if (logger.isInfoEnabled()) {
logger.info("Request error. request={}, Caused by:{}", logString(message), throwable.getMessage(), throwable);
final StatusError statusError = StatusErrors.throwable(throwable);
if (statusError.isSimpleError()) {
logger.info("Error. request={}, cause={}", logString(message), statusError.getMessage());
} else {
logger.warn("Error. request={}, cause={}", logString(message), statusError.getMessage(), statusError.getThrowable());
}

// Retry
final int remainingRetryCount = nextRetryCount();
retryScheduler.scheduleNextRetry(message, remainingRetryCount);
}
Expand Down

0 comments on commit 6e6a2b3

Please sign in to comment.