static HttpHeaders statusToTrailers(ServiceRequestContext ctx, Status status, boolean headersSent) { final HttpHeaders trailers; if (headersSent) { // Normal trailers. trailers = new DefaultHttpHeaders(); } else { // Trailers only response trailers = new DefaultHttpHeaders(true, 3, true) .status(HttpStatus.OK) .set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto"); } trailers.add(GrpcHeaderNames.GRPC_STATUS, Integer.toString(status.getCode().value())); if (status.getDescription() != null) { trailers.add(GrpcHeaderNames.GRPC_MESSAGE, StatusMessageEscaper.escape(status.getDescription())); } if (ctx.server().config().verboseResponses() && status.getCause() != null) { final ThrowableProto proto = GrpcStatus.serializeThrowable(status.getCause()); trailers.add(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN, Base64.getEncoder().encodeToString(proto.toByteArray())); } return trailers; }
private static String niceToString(Status status) { // the default Status.toString has a stack trace in it, which is ugly return MoreObjects .toStringHelper(status) .add("code", status.getCode().name()) .add("description", status.getDescription()) .add("cause", status.getCause() != null ? status.getCause().getMessage() : null) .toString(); }
/** {@inheritDoc} */ @Override public void onClose(Status status, Metadata trailers) { if (status.getCause() instanceof StreamWaitTimeoutException && ((StreamWaitTimeoutException)status.getCause()).getState() == State.WAITING) { handleTimeoutError(status); return; } super.onClose(status, trailers); }
/** * Constructs the exception with both a status and trailers. */ @ExperimentalApi public StatusRuntimeException(Status status, @Nullable Metadata trailers) { super(Status.formatThrowableMessage(status), status.getCause()); this.status = status; this.trailers = trailers; }
StatusRuntimeException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace) { super(Status.formatThrowableMessage(status), status.getCause()); this.status = status; this.trailers = trailers; this.fillInStackTrace = fillInStackTrace; fillInStackTrace(); }
StatusException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace) { super(Status.formatThrowableMessage(status), status.getCause()); this.status = status; this.trailers = trailers; this.fillInStackTrace = fillInStackTrace; fillInStackTrace(); }
/** * Constructs an exception with both a status and trailers. */ @ExperimentalApi public StatusException(Status status, @Nullable Metadata trailers) { super(Status.formatThrowableMessage(status), status.getCause()); this.status = status; this.trailers = trailers; }
@Override public void closed(final Status status) { // For cancellations, promptly inform any users of the context that their work should be // aborted. Otherwise, we can wait until pending work is done. if (!status.isOk()) { // The callExecutor might be busy doing user work. To avoid waiting, use an executor that // is not serializing. cancelExecutor.execute(new ContextCloser(context, status.getCause())); } final class Closed extends ContextRunnable { Closed() { super(context); } @Override public void runInContext() { getListener().closed(status); } } callExecutor.execute(new Closed()); }
@Override public void closed(final Status status) { // For cancellations, promptly inform any users of the context that their work should be // aborted. Otherwise, we can wait until pending work is done. if (!status.isOk()) { // The callExecutor might be busy doing user work. To avoid waiting, use an executor that // is not serializing. cancelExecutor.execute(new ContextCloser(context, status.getCause())); } final class Closed extends ContextRunnable { Closed() { super(context); } @Override public void runInContext() { getListener().closed(status); } } callExecutor.execute(new Closed()); }
private static EtcdException fromStatus(Status status) { return newEtcdException( ErrorCode.fromGrpcStatus(status), status.getDescription(), status.getCause() ); } }
private static EtcdException fromStatus(Status status) { return newEtcdException( ErrorCode.fromGrpcStatus(status), status.getDescription(), status.getCause() ); } }
private static EtcdException fromStatus(Status status) { return newEtcdException( ErrorCode.fromGrpcStatus(status), status.getDescription(), status.getCause() ); } }
static HttpHeaders statusToTrailers(ServiceRequestContext ctx, Status status, boolean headersSent) { final HttpHeaders trailers; if (headersSent) { // Normal trailers. trailers = new DefaultHttpHeaders(); } else { // Trailers only response trailers = new DefaultHttpHeaders(true, 3, true) .status(HttpStatus.OK) .set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto"); } trailers.add(GrpcHeaderNames.GRPC_STATUS, Integer.toString(status.getCode().value())); if (status.getDescription() != null) { trailers.add(GrpcHeaderNames.GRPC_MESSAGE, StatusMessageEscaper.escape(status.getDescription())); } if (ctx.server().config().verboseResponses() && status.getCause() != null) { final ThrowableProto proto = GrpcStatus.serializeThrowable(status.getCause()); trailers.add(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN, Base64.getEncoder().encodeToString(proto.toByteArray())); } return trailers; }
/** * Returns the {@link Status} of a cancelled context or {@code null} if the context * is not cancelled. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1975") public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED.withDescription("io.grpc.Context was cancelled without error"); } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { // If fromThrowable could not determine a status, then // just return CANCELLED. return Status.CANCELLED.withDescription("Context cancelled").withCause(cancellationCause); } return status.withCause(cancellationCause); } }
/** * Special retry handling for watchdog timeouts, which uses its own fail counter. * * @return true if a retry has been scheduled */ private void handleTimeoutError(Status status) { Preconditions.checkArgument(status.getCause() instanceof StreamWaitTimeoutException, "status is not caused by a StreamWaitTimeoutException"); StreamWaitTimeoutException e = ((StreamWaitTimeoutException) status.getCause()); // Cancel the existing rpc. rpcTimerContext.close(); failedCount++; // Can this request be retried int maxRetries = retryOptions.getMaxScanTimeoutRetries(); if (retryOptions.enableRetries() && ++timeoutRetryCount <= maxRetries) { LOG.warn("The client could not get a response in %d ms. Retrying the scan.", e.getWaitTimeMs()); resetStatusBasedBackoff(); performRetry(0); } else { LOG.warn("The client could not get a response after %d tries, giving up.", timeoutRetryCount); rpc.getRpcMetrics().markFailure(); finalizeStats(status); setException(getExhaustedRetriesException(status)); } }
status.getCause(), failedCount, status, channelId, trailers); rpc.getRpcMetrics().markFailure(); finalizeStats(status); status.getCause(), failedCount, status, channelId, trailers); setException(getExhaustedRetriesException(status)); } else { LOG.warn("Retrying failed call. Failure #%d, got: %s on channel %s.\nTrailers: %s", status.getCause(), failedCount, status, channelId, trailers); performRetry(nextBackOff);
/** * Returns the {@link Status} of a cancelled context or {@code null} if the context * is not cancelled. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1975") public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED.withDescription("io.grpc.Context was cancelled without error"); } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { // If fromThrowable could not determine a status, then // just return CANCELLED. return Status.CANCELLED.withDescription("Context cancelled").withCause(cancellationCause); } return status.withCause(cancellationCause); } }