use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.
the class ClientCallImplTest method methodInfoDeadlinePropagatedToStream.
@Test
public void methodInfoDeadlinePropagatedToStream() {
ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(null);
CallOptions callOptions = baseCallOptions.withDeadline(Deadline.after(2000, SECONDS));
// Case: config Deadline expires later than CallOptions Deadline
Map<String, ?> rawMethodConfig = ImmutableMap.of("timeout", "3000s");
MethodInfo methodInfo = new MethodInfo(rawMethodConfig, false, 0, 0);
callOptions = callOptions.withOption(MethodInfo.KEY, methodInfo);
ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), callOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector).setDecompressorRegistry(decompressorRegistry);
call.start(callListener, new Metadata());
verify(clientStreamProvider).newStream(same(method), callOptionsCaptor.capture(), any(Metadata.class), any(Context.class));
Deadline actualDeadline = callOptionsCaptor.getValue().getDeadline();
assertThat(actualDeadline).isLessThan(Deadline.after(2001, SECONDS));
// Case: config Deadline expires earlier than CallOptions Deadline
rawMethodConfig = ImmutableMap.of("timeout", "1000s");
methodInfo = new MethodInfo(rawMethodConfig, false, 0, 0);
callOptions = callOptions.withOption(MethodInfo.KEY, methodInfo);
call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), callOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector).setDecompressorRegistry(decompressorRegistry);
call.start(callListener, new Metadata());
verify(clientStreamProvider, times(2)).newStream(same(method), callOptionsCaptor.capture(), any(Metadata.class), any(Context.class));
actualDeadline = callOptionsCaptor.getValue().getDeadline();
assertThat(actualDeadline).isLessThan(Deadline.after(1001, SECONDS));
}
use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.
the class ManagedChannelServiceConfigTest method managedChannelServiceConfig_parseMethodConfig.
@Test
public void managedChannelServiceConfig_parseMethodConfig() {
Map<String, ?> name1 = ImmutableMap.of("service", "service1", "method", "method1");
Map<String, ?> name2 = ImmutableMap.of("service", "service2");
Map<String, ?> methodConfig = ImmutableMap.of("name", ImmutableList.of(name1, name2), "timeout", "1.234s", "retryPolicy", ImmutableMap.builder().put("maxAttempts", 3.0D).put("initialBackoff", "1s").put("maxBackoff", "10s").put("backoffMultiplier", 1.5D).put("perAttemptRecvTimeout", "2.5s").put("retryableStatusCodes", ImmutableList.of("UNAVAILABLE")).build());
Map<String, ?> defaultMethodConfig = ImmutableMap.of("name", ImmutableList.of(ImmutableMap.of()), "timeout", "4.321s");
Map<String, ?> rawServiceConfig = ImmutableMap.of("methodConfig", ImmutableList.of(methodConfig, defaultMethodConfig));
// retry disabled
ManagedChannelServiceConfig serviceConfig = ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, false, 0, 0, null);
MethodInfo methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "method1"));
assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(1234));
assertThat(methodInfo.retryPolicy).isNull();
methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "methodX"));
assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(4321));
assertThat(methodInfo.retryPolicy).isNull();
methodInfo = serviceConfig.getMethodConfig(methodForName("service2", "methodX"));
assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(1234));
assertThat(methodInfo.retryPolicy).isNull();
// retry enabled
serviceConfig = ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, true, 2, 0, null);
methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "method1"));
assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(1234));
assertThat(methodInfo.retryPolicy.maxAttempts).isEqualTo(2);
assertThat(methodInfo.retryPolicy.perAttemptRecvTimeoutNanos).isEqualTo(MILLISECONDS.toNanos(2500));
assertThat(methodInfo.retryPolicy.retryableStatusCodes).containsExactly(UNAVAILABLE);
methodInfo = serviceConfig.getMethodConfig(methodForName("service1", "methodX"));
assertThat(methodInfo.timeoutNanos).isEqualTo(MILLISECONDS.toNanos(4321));
assertThat(methodInfo.retryPolicy).isNull();
}
use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.
the class ManagedChannelServiceConfigTest method getDefaultConfigSelectorFromConfig.
@Test
public void getDefaultConfigSelectorFromConfig() {
Map<String, ?> name = ImmutableMap.of("service", "service1", "method", "method1");
Map<String, ?> methodConfig = ImmutableMap.of("name", ImmutableList.of(name), "timeout", "1.234s");
Map<String, ?> rawServiceConfig = ImmutableMap.of("methodConfig", ImmutableList.of(methodConfig));
ManagedChannelServiceConfig serviceConfig = ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, false, 0, 0, null);
InternalConfigSelector configSelector = serviceConfig.getDefaultConfigSelector();
MethodDescriptor<?, ?> method = methodForName("service1", "method1");
Result result = configSelector.selectConfig(new PickSubchannelArgsImpl(method, new Metadata(), CallOptions.DEFAULT));
MethodInfo methodInfoFromDefaultConfigSelector = ((ManagedChannelServiceConfig) result.getConfig()).getMethodConfig(method);
assertThat(methodInfoFromDefaultConfigSelector).isEqualTo(serviceConfig.getMethodConfig(method));
}
use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.
the class ConfigSelectingClientCallTest method configSelectorInterceptsCall.
@Test
public void configSelectorInterceptsCall() {
Map<String, ?> rawMethodConfig = ImmutableMap.of("retryPolicy", ImmutableMap.of("maxAttempts", 3.0D, "initialBackoff", "1s", "maxBackoff", "10s", "backoffMultiplier", 1.5D, "retryableStatusCodes", ImmutableList.of("UNAVAILABLE")));
final MethodInfo methodInfo = new MethodInfo(rawMethodConfig, true, 4, 4);
final Metadata.Key<String> metadataKey = Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER);
final CallOptions.Key<String> callOptionsKey = CallOptions.Key.create("test");
InternalConfigSelector configSelector = new InternalConfigSelector() {
@Override
public Result selectConfig(final PickSubchannelArgs args) {
ManagedChannelServiceConfig config = new ManagedChannelServiceConfig(methodInfo, ImmutableMap.<String, MethodInfo>of(), ImmutableMap.<String, MethodInfo>of(), null, null, null);
return Result.newBuilder().setConfig(config).setInterceptor(// An interceptor that mutates CallOptions based on headers value.
new ClientInterceptor() {
String value = args.getHeaders().get(metadataKey);
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
callOptions = callOptions.withOption(callOptionsKey, value);
return next.newCall(method, callOptions);
}
}).build();
}
};
ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT.withAuthority("bar.authority"));
Metadata metadata = new Metadata();
metadata.put(metadataKey, "fooValue");
configSelectingClientCall.start(callListener, metadata);
assertThat(call.callOptions.getAuthority()).isEqualTo("bar.authority");
assertThat(call.callOptions.getOption(MethodInfo.KEY)).isEqualTo(methodInfo);
assertThat(call.callOptions.getOption(callOptionsKey)).isEqualTo("fooValue");
}
use of io.grpc.internal.ManagedChannelServiceConfig.MethodInfo in project grpc-java by grpc.
the class ClientCallImpl method applyMethodConfig.
private void applyMethodConfig() {
MethodInfo info = callOptions.getOption(MethodInfo.KEY);
if (info == null) {
return;
}
if (info.timeoutNanos != null) {
Deadline newDeadline = Deadline.after(info.timeoutNanos, TimeUnit.NANOSECONDS);
Deadline existingDeadline = callOptions.getDeadline();
// If the new deadline is sooner than the existing deadline, swap them.
if (existingDeadline == null || newDeadline.compareTo(existingDeadline) < 0) {
callOptions = callOptions.withDeadline(newDeadline);
}
}
if (info.waitForReady != null) {
callOptions = info.waitForReady ? callOptions.withWaitForReady() : callOptions.withoutWaitForReady();
}
if (info.maxInboundMessageSize != null) {
Integer existingLimit = callOptions.getMaxInboundMessageSize();
if (existingLimit != null) {
callOptions = callOptions.withMaxInboundMessageSize(Math.min(existingLimit, info.maxInboundMessageSize));
} else {
callOptions = callOptions.withMaxInboundMessageSize(info.maxInboundMessageSize);
}
}
if (info.maxOutboundMessageSize != null) {
Integer existingLimit = callOptions.getMaxOutboundMessageSize();
if (existingLimit != null) {
callOptions = callOptions.withMaxOutboundMessageSize(Math.min(existingLimit, info.maxOutboundMessageSize));
} else {
callOptions = callOptions.withMaxOutboundMessageSize(info.maxOutboundMessageSize);
}
}
}
Aggregations