use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.
the class HedgingPolicyTest method getRetryPolicies_hedgingDisabled.
@Test
public void getRetryPolicies_hedgingDisabled() throws Exception {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(RetryPolicyTest.class.getResourceAsStream("/io/grpc/internal/test_hedging_service_config.json"), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
Object serviceConfigObj = JsonParser.parse(sb.toString());
assertTrue(serviceConfigObj instanceof Map);
@SuppressWarnings("unchecked") Map<String, ?> serviceConfig = (Map<String, ?>) serviceConfigObj;
ManagedChannelServiceConfig channelServiceConfig = ManagedChannelServiceConfig.fromServiceConfig(serviceConfig, /* retryEnabled= */
false, /* maxRetryAttemptsLimit= */
3, /* maxHedgedAttemptsLimit= */
4, /* loadBalancingConfig= */
null);
MethodDescriptor.Builder<Void, Void> builder = TestMethodDescriptors.voidMethod().toBuilder();
MethodDescriptor<Void, Void> method = builder.setFullMethodName("SimpleService1/Foo1").build();
assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isNull();
} finally {
if (reader != null) {
reader.close();
}
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.
the class HedgingPolicyTest method getHedgingPolicies.
@Test
public void getHedgingPolicies() throws Exception {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(RetryPolicyTest.class.getResourceAsStream("/io/grpc/internal/test_hedging_service_config.json"), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
Object serviceConfigObj = JsonParser.parse(sb.toString());
assertTrue(serviceConfigObj instanceof Map);
@SuppressWarnings("unchecked") Map<String, ?> serviceConfig = (Map<String, ?>) serviceConfigObj;
ManagedChannelServiceConfig channelServiceConfig = ManagedChannelServiceConfig.fromServiceConfig(serviceConfig, /* retryEnabled= */
true, /* maxRetryAttemptsLimit= */
3, /* maxHedgedAttemptsLimit= */
4, /* loadBalancingConfig= */
null);
MethodDescriptor.Builder<Void, Void> builder = TestMethodDescriptors.voidMethod().toBuilder();
MethodDescriptor<Void, Void> method = builder.setFullMethodName("not/exist").build();
assertThat(channelServiceConfig.getMethodConfig(method)).isNull();
method = builder.setFullMethodName("not_exist/Foo1").build();
assertThat(channelServiceConfig.getMethodConfig(method)).isNull();
method = builder.setFullMethodName("SimpleService1/not_exist").build();
assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isEqualTo(new HedgingPolicy(3, TimeUnit.MILLISECONDS.toNanos(2100), ImmutableSet.of(Code.UNAVAILABLE, Code.RESOURCE_EXHAUSTED)));
method = builder.setFullMethodName("SimpleService1/Foo1").build();
assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isEqualTo(new HedgingPolicy(4, TimeUnit.MILLISECONDS.toNanos(100), ImmutableSet.of(Code.UNAVAILABLE)));
method = builder.setFullMethodName("SimpleService2/not_exist").build();
assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isNull();
method = builder.setFullMethodName("SimpleService2/Foo2").build();
assertThat(channelServiceConfig.getMethodConfig(method).hedgingPolicy).isEqualTo(new HedgingPolicy(4, TimeUnit.MILLISECONDS.toNanos(100), ImmutableSet.of(Code.UNAVAILABLE)));
} finally {
if (reader != null) {
reader.close();
}
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project grpc-java by grpc.
the class FaultFilter method buildClientInterceptor.
@Nullable
@Override
public ClientInterceptor buildClientInterceptor(FilterConfig config, @Nullable FilterConfig overrideConfig, PickSubchannelArgs args, final ScheduledExecutorService scheduler) {
checkNotNull(config, "config");
if (overrideConfig != null) {
config = overrideConfig;
}
FaultConfig faultConfig = (FaultConfig) config;
Long delayNanos = null;
Status abortStatus = null;
if (faultConfig.maxActiveFaults() == null || activeFaultCounter.get() < faultConfig.maxActiveFaults()) {
Metadata headers = args.getHeaders();
if (faultConfig.faultDelay() != null) {
delayNanos = determineFaultDelayNanos(faultConfig.faultDelay(), headers);
}
if (faultConfig.faultAbort() != null) {
abortStatus = determineFaultAbortStatus(faultConfig.faultAbort(), headers);
}
}
if (delayNanos == null && abortStatus == null) {
return null;
}
final Long finalDelayNanos = delayNanos;
final Status finalAbortStatus = getAbortStatusWithDescription(abortStatus);
final class FaultInjectionInterceptor implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, final CallOptions callOptions, final Channel next) {
Executor callExecutor = callOptions.getExecutor();
if (callExecutor == null) {
// This should never happen in practice because
// ManagedChannelImpl.ConfigSelectingClientCall always provides CallOptions with
// a callExecutor.
// TODO(https://github.com/grpc/grpc-java/issues/7868)
callExecutor = MoreExecutors.directExecutor();
}
if (finalDelayNanos != null) {
Supplier<? extends ClientCall<ReqT, RespT>> callSupplier;
if (finalAbortStatus != null) {
callSupplier = Suppliers.ofInstance(new FailingClientCall<ReqT, RespT>(finalAbortStatus, callExecutor));
} else {
callSupplier = new Supplier<ClientCall<ReqT, RespT>>() {
@Override
public ClientCall<ReqT, RespT> get() {
return next.newCall(method, callOptions);
}
};
}
final DelayInjectedCall<ReqT, RespT> delayInjectedCall = new DelayInjectedCall<>(finalDelayNanos, callExecutor, scheduler, callOptions.getDeadline(), callSupplier);
final class DeadlineInsightForwardingCall extends ForwardingClientCall<ReqT, RespT> {
@Override
protected ClientCall<ReqT, RespT> delegate() {
return delayInjectedCall;
}
@Override
public void start(Listener<RespT> listener, Metadata headers) {
Listener<RespT> finalListener = new SimpleForwardingClientCallListener<RespT>(listener) {
@Override
public void onClose(Status status, Metadata trailers) {
if (status.getCode().equals(Code.DEADLINE_EXCEEDED)) {
// TODO(zdapeng:) check effective deadline locally, and
// do the following only if the local deadline is exceeded.
// (If the server sends DEADLINE_EXCEEDED for its own deadline, then the
// injected delay does not contribute to the error, because the request is
// only sent out after the delay. There could be a race between local and
// remote, but it is rather rare.)
String description = String.format("Deadline exceeded after up to %d ns of fault-injected delay", finalDelayNanos);
if (status.getDescription() != null) {
description = description + ": " + status.getDescription();
}
status = Status.DEADLINE_EXCEEDED.withDescription(description).withCause(status.getCause());
// Replace trailers to prevent mixing sources of status and trailers.
trailers = new Metadata();
}
delegate().onClose(status, trailers);
}
};
delegate().start(finalListener, headers);
}
}
return new DeadlineInsightForwardingCall();
} else {
return new FailingClientCall<>(finalAbortStatus, callExecutor);
}
}
}
return new FaultInjectionInterceptor();
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project tesla by linking12.
the class DynamicGrpcClient method doRemoteCall.
@Override
public String doRemoteCall(final FilterRpcDO rpcDo, final String jsonInput) {
try {
final String serviceName = rpcDo.getServiceName();
final String methodName = rpcDo.getMethodName();
final String group = rpcDo.getServiceGroup();
final String version = rpcDo.getServiceVersion();
Pair<Descriptor, Descriptor> inOutType = ProtobufUtil.resolveServiceInputOutputType(rpcDo);
Descriptor inPutType = inOutType.getLeft();
Descriptor outPutType = inOutType.getRight();
MethodDescriptor<DynamicMessage, DynamicMessage> methodDesc = this.createGrpcMethodDescriptor(serviceName, methodName, inPutType, outPutType);
DynamicMessage message = this.createGrpcDynamicMessage(inPutType, jsonInput);
Message response = (Message) genricService.$invoke(serviceName, group, version, methodName, methodDesc, message);
return JSON2PROTOBUF.printToString(response);
} catch (IOException e) {
throw new RpcServiceException(String.format("json covert to DynamicMessage failed! the json is :%s, the protobuf type is: %s", jsonInput), e);
} catch (Throwable e) {
throw new RpcFrameworkException(String.format("service definition is wrong,please check the proto file you update,service is %s, method is %s", rpcDo.getServiceName(), rpcDo.getMethodName()), e);
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project etcd-java by IBM.
the class EtcdClient method refreshCredentials.
private CallCredentials refreshCredentials() {
return new CallCredentials() {
// TODO volatile TBD
private Metadata tokenHeader;
private final long authTime = System.currentTimeMillis();
private final ListenableFuture<Metadata> futureTokenHeader = Futures.transform(authenticate(), (Function<AuthenticateResponse, Metadata>) ar -> tokenHeader = tokenHeader(ar));
@Override
public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, Executor appExecutor, MetadataApplier applier) {
Metadata tokHeader = tokenHeader;
if (tokHeader != null)
applier.apply(tokHeader);
else
futureTokenHeader.addListener(() -> {
try {
applier.apply(futureTokenHeader.get());
} catch (ExecutionException | InterruptedException ee) {
// (IE won't be thrown)
Status failStatus = Status.fromThrowable(ee.getCause());
Code code = failStatus != null ? failStatus.getCode() : null;
if (code != Code.INVALID_ARGUMENT && (System.currentTimeMillis() - authTime) > 15_000L) {
// this will force another auth attempt
failStatus = Status.UNAUTHENTICATED.withDescription("re-attempt re-auth");
}
applier.fail(failStatus);
}
}, directExecutor());
}
// @Override
public void thisUsesUnstableApi() {
}
};
}
Aggregations