static long extractRetryDelay(Throwable cause) { if (cause != null) { Metadata trailers = Status.trailersFromThrowable(cause); if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) { RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO); if (retryInfo.hasRetryDelay()) { return Durations.toMillis(retryInfo.getRetryDelay()); } } } return -1L; } }
if (e.getTrailers() != null && e.getTrailers().containsKey(sInnerCauseKey)) { try { cause = (Throwable) SerializationUtils.deserialize(e.getTrailers().get(sInnerCauseKey));
/** * Returns true if a value is defined for the given key. * * <p>This is done by linear search, so if it is followed by {@link #get} or {@link #getAll}, * prefer calling them directly and checking the return value against {@code null}. */ public boolean containsKey(Metadata.Key<?> key) { return contextMetadata.containsKey(key); }
/** * Returns true if a value is defined for the given key. * * <p>This is done by linear search, so if it is followed by {@link #get} or {@link #getAll}, * prefer calling them directly and checking the return value against {@code null}. */ public boolean containsKey(Metadata.Key<?> key) { return contextMetadata.containsKey(key); }
@VisibleForTesting public void updateHeaders(Metadata headers) { if (!headers.containsKey(GRPC_RESOURCE_PREFIX_KEY)) { headers.put(GRPC_RESOURCE_PREFIX_KEY, defaultValue); } } }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next ) { if (!headers.containsKey(tokenKey)) { call.close(Status.PERMISSION_DENIED.withDescription("no authorization token"), new Metadata()); return new ServerCall.Listener<ReqT>(){}; } final String authToken = headers.get(tokenKey); LOGGER.debug("Got authentication token (" + authToken + ")"); return next.startCall(call, headers); } }
public Lumongo.FetchResponse executeFetch(Member m, Lumongo.FetchRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); Lumongo.FetchResponse response = rpcConnection.getService().fetch(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
public Lumongo.GetTermsResponseInternal getTerms(Member m, GetTermsRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); Lumongo.GetTermsResponseInternal response = rpcConnection.getService().getTerms(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
@Override public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { if (headers.containsKey(JwtClientInterceptor.JWT_KEY)) { String jwt = headers.get(JwtClientInterceptor.JWT_KEY); LOGGER.trace("Server received jwt key: " + jwt); Context ctx = Context.current().withValue(GrpcContextKeys.JWT_KEY, jwt); return Contexts.interceptCall(ctx, call, headers, next); } else { call.close(Status.UNAUTHENTICATED.withDescription("JWT not passed in metadata."), headers); return new ServerCall.Listener<ReqT>() { }; } }
public InternalQueryResponse executeQuery(Member m, QueryRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); InternalQueryResponse response = rpcConnection.getService().query(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
public GetFieldNamesResponse getFieldNames(Member m, GetFieldNamesRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); GetFieldNamesResponse response = rpcConnection.getService().getFieldNames(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
@Override public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { if (headers.containsKey(TenantTokenClientInterceptor.TENANT_ID_KEY)) { String tenantId = headers.get(TenantTokenClientInterceptor.TENANT_ID_KEY); LOGGER.trace("Server received tenant id key: " + tenantId); Context ctx = Context.current().withValue(GrpcContextKeys.TENANT_ID_KEY, tenantId); return Contexts.interceptCall(ctx, call, headers, next); } else { call.close(Status.UNAUTHENTICATED.withDescription("Tenant token not passed in metadata."), headers); return new ServerCall.Listener<ReqT>() { }; } }
public StoreResponse executeStore(Member m, StoreRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); StoreResponse response = rpcConnection.getService().store(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
public DeleteResponse executeDelete(Member m, DeleteRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); DeleteResponse response = rpcConnection.getService().delete(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
public OptimizeResponse optimize(Member m, OptimizeRequest request) throws Exception { ReadWriteLock lock = getLockForMember(m); lock.readLock().lock(); InternalRpcConnection rpcConnection = null; try { rpcConnection = getInternalRpcConnection(m); OptimizeResponse response = rpcConnection.getService().optimize(request); returnInternalBlockingConnection(m, rpcConnection, true); return response; } catch (StatusRuntimeException e) { Metadata trailers = e.getTrailers(); if (trailers.containsKey(MetaKeys.ERROR_KEY)) { throw new Exception(trailers.get(MetaKeys.ERROR_KEY)); } else { throw e; } } catch (Exception e) { returnInternalBlockingConnection(m, rpcConnection, false); throw e; } finally { lock.readLock().unlock(); } }
RESPONSE_HEADERS, HashMultimap.create()); boolean sideChannelOn = headers.containsKey(HEADERS_KEY); ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> simpleForwardingServerCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
static long extractRetryDelay(Throwable cause) { if (cause != null) { Metadata trailers = Status.trailersFromThrowable(cause); if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) { RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO); if (retryInfo.hasRetryDelay()) { return Durations.toMillis(retryInfo.getRetryDelay()); } } } return -1L; } }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, final Metadata headers, ServerCallHandler<ReqT, RespT> next) { // Check if this RPC has tags to track request (e.g., older clients). if (headers != null && headers.containsKey(DESCRIPTOR_HEADER) && headers.containsKey(ID_HEADER)) { RequestTag requestTag = new RequestTag(headers.get(DESCRIPTOR_HEADER), Long.parseLong(headers.get(ID_HEADER))); requestTracker.trackRequest(requestTag); log.debug(requestTag.getRequestId(), "Received tag from RPC request {}.", requestTag.getRequestDescriptor()); } else { log.debug("No tags provided for call {} in headers: {}.", call.getMethodDescriptor().getFullMethodName(), headers); } return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) { @Override public void sendHeaders(Metadata responseHeaders) { super.sendHeaders(responseHeaders); } }, headers); } }
public static GrpcError throwableToStatus(Throwable t) { Status status = Status.fromThrowable(t); if (t instanceof InvalidProtocolBufferException) { status = Status.INVALID_ARGUMENT.withCause(t); } Metadata trailer = Status.trailersFromThrowable(t); int statusCode = grpcToHttpStatus(status); com.google.rpc.Status.Builder payload = com.google.rpc.Status.newBuilder(); payload.setCode(status.getCode().value()); StringBuilder errorMessage = new StringBuilder( "HTTP " + statusCode + " (gRPC: " + status.getCode().name() + ")"); if (!Strings.isNullOrEmpty(status.getDescription())) { errorMessage.append(": ").append(Strings.nullToEmpty(status.getDescription())); } payload.setMessage(errorMessage.toString()); if (trailer != null) { if (trailer.containsKey(RETRY_INFO_KEY)) { RetryInfo retryInfo = trailer.get(RETRY_INFO_KEY); payload.addDetails(Any.pack(retryInfo)); } if (trailer.containsKey(DEBUG_INFO_KEY)) { DebugInfo debugInfo = trailer.get(DEBUG_INFO_KEY); payload.addDetails(Any.pack(debugInfo)); } } return new GrpcError( status, payload.build() ); }
assertFalse(headers.containsKey(RPCTracingHelpers.DESCRIPTOR_HEADER)); assertFalse(headers.containsKey(RPCTracingHelpers.ID_HEADER));