use of io.grpc.rls.CachingRlsLbClient.CachedRouteLookupResponse in project grpc-java by grpc.
the class CachingRlsLbClientTest method rls_withCustomRlsChannelServiceConfig.
@Test
public void rls_withCustomRlsChannelServiceConfig() throws Exception {
Map<String, ?> routeLookupChannelServiceConfig = ImmutableMap.of("loadBalancingConfig", ImmutableList.of(ImmutableMap.of("grpclb", ImmutableMap.of("childPolicy", ImmutableList.of(ImmutableMap.of("pick_first", ImmutableMap.of())), "serviceName", "service1"))));
LbPolicyConfiguration lbPolicyConfiguration = new LbPolicyConfiguration(ROUTE_LOOKUP_CONFIG, routeLookupChannelServiceConfig, childLbPolicy);
rlsLbClient = CachingRlsLbClient.newBuilder().setBackoffProvider(fakeBackoffProvider).setResolvedAddressesFactory(resolvedAddressFactory).setEvictionListener(evictionListener).setHelper(helper).setLbPolicyConfig(lbPolicyConfiguration).setThrottler(fakeThrottler).setTimeProvider(fakeTimeProvider).build();
RouteLookupRequest routeLookupRequest = RouteLookupRequest.create(ImmutableMap.of("server", "bigtable.googleapis.com", "service-key", "foo", "method-key", "bar"));
rlsServerImpl.setLookupTable(ImmutableMap.of(routeLookupRequest, RouteLookupResponse.create(ImmutableList.of("target"), "header")));
// initial request
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequest);
assertThat(resp.isPending()).isTrue();
// server response
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
assertThat(rlsChannelOverriddenAuthority).isEqualTo("bigtable.googleapis.com:443");
assertThat(rlsChannelServiceConfig).isEqualTo(routeLookupChannelServiceConfig);
}
use of io.grpc.rls.CachingRlsLbClient.CachedRouteLookupResponse in project grpc-java by grpc.
the class CachingRlsLbClientTest method get_childPolicyWrapper_reusedForSameTarget.
@Test
public void get_childPolicyWrapper_reusedForSameTarget() throws Exception {
setUpRlsLbClient();
RouteLookupRequest routeLookupRequest = RouteLookupRequest.create(ImmutableMap.of("server", "bigtable.googleapis.com", "service-key", "foo", "method-key", "bar"));
RouteLookupRequest routeLookupRequest2 = RouteLookupRequest.create(ImmutableMap.of("server", "bigtable.googleapis.com", "service-key", "foo", "method-key", "baz"));
rlsServerImpl.setLookupTable(ImmutableMap.of(routeLookupRequest, RouteLookupResponse.create(ImmutableList.of("target"), "header"), routeLookupRequest2, RouteLookupResponse.create(ImmutableList.of("target"), "header2")));
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequest);
assertThat(resp.isPending()).isTrue();
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
assertThat(resp.getHeaderData()).isEqualTo("header");
ChildPolicyWrapper childPolicyWrapper = resp.getChildPolicyWrapper();
assertThat(childPolicyWrapper.getTarget()).isEqualTo("target");
assertThat(childPolicyWrapper.getPicker()).isNotInstanceOf(RlsPicker.class);
// request2 has same target, it should reuse childPolicyWrapper
CachedRouteLookupResponse resp2 = getInSyncContext(routeLookupRequest2);
assertThat(resp2.isPending()).isTrue();
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
resp2 = getInSyncContext(routeLookupRequest2);
assertThat(resp2.hasData()).isTrue();
assertThat(resp2.getHeaderData()).isEqualTo("header2");
assertThat(resp2.getChildPolicyWrapper()).isEqualTo(resp.getChildPolicyWrapper());
}
use of io.grpc.rls.CachingRlsLbClient.CachedRouteLookupResponse in project grpc-java by grpc.
the class CachingRlsLbClientTest method get_throttledAndRecover.
@Test
public void get_throttledAndRecover() throws Exception {
setUpRlsLbClient();
RouteLookupRequest routeLookupRequest = RouteLookupRequest.create(ImmutableMap.of("server", "bigtable.googleapis.com", "service-key", "foo", "method-key", "bar"));
rlsServerImpl.setLookupTable(ImmutableMap.of(routeLookupRequest, RouteLookupResponse.create(ImmutableList.of("target"), "header")));
fakeThrottler.nextResult = true;
fakeBackoffProvider.nextPolicy = createBackoffPolicy(10, TimeUnit.MILLISECONDS);
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasError()).isTrue();
fakeTimeProvider.forwardTime(10, TimeUnit.MILLISECONDS);
// initially backed off entry is backed off again
verify(evictionListener).onEviction(eq(routeLookupRequest), any(CacheEntry.class), eq(EvictionType.REPLACED));
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasError()).isTrue();
// let it pass throttler
fakeThrottler.nextResult = false;
fakeTimeProvider.forwardTime(10, TimeUnit.MILLISECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.isPending()).isTrue();
// server responses
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
}
use of io.grpc.rls.CachingRlsLbClient.CachedRouteLookupResponse in project grpc-java by grpc.
the class CachingRlsLbClientTest method get_noError_lifeCycle.
@Test
public void get_noError_lifeCycle() throws Exception {
setUpRlsLbClient();
InOrder inOrder = inOrder(evictionListener);
RouteLookupRequest routeLookupRequest = RouteLookupRequest.create(ImmutableMap.of("server", "bigtable.googleapis.com", "service-key", "foo", "method-key", "bar"));
rlsServerImpl.setLookupTable(ImmutableMap.of(routeLookupRequest, RouteLookupResponse.create(ImmutableList.of("target"), "header")));
// initial request
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequest);
assertThat(resp.isPending()).isTrue();
// server response
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
// cache hit for staled entry
fakeTimeProvider.forwardTime(ROUTE_LOOKUP_CONFIG.staleAgeInNanos(), TimeUnit.NANOSECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
// async refresh finishes
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
inOrder.verify(evictionListener).onEviction(eq(routeLookupRequest), any(CacheEntry.class), eq(EvictionType.REPLACED));
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
// existing cache expired
fakeTimeProvider.forwardTime(ROUTE_LOOKUP_CONFIG.maxAgeInNanos(), TimeUnit.NANOSECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.isPending()).isTrue();
inOrder.verify(evictionListener).onEviction(eq(routeLookupRequest), any(CacheEntry.class), eq(EvictionType.EXPIRED));
inOrder.verifyNoMoreInteractions();
}
use of io.grpc.rls.CachingRlsLbClient.CachedRouteLookupResponse in project grpc-java by grpc.
the class CachingRlsLbClientTest method get_updatesLbState.
@Test
public void get_updatesLbState() throws Exception {
setUpRlsLbClient();
InOrder inOrder = inOrder(helper);
RouteLookupRequest routeLookupRequest = RouteLookupRequest.create(ImmutableMap.of("server", "bigtable.googleapis.com", "service-key", "service1", "method-key", "create"));
rlsServerImpl.setLookupTable(ImmutableMap.of(routeLookupRequest, RouteLookupResponse.create(ImmutableList.of("primary.cloudbigtable.googleapis.com"), "header-rls-data-value")));
// valid channel
CachedRouteLookupResponse resp = getInSyncContext(routeLookupRequest);
assertThat(resp.isPending()).isTrue();
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
resp = getInSyncContext(routeLookupRequest);
assertThat(resp.hasData()).isTrue();
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(SubchannelPicker.class);
ArgumentCaptor<ConnectivityState> stateCaptor = ArgumentCaptor.forClass(ConnectivityState.class);
inOrder.verify(helper, times(2)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());
assertThat(new HashSet<>(pickerCaptor.getAllValues())).hasSize(1);
assertThat(stateCaptor.getAllValues()).containsExactly(ConnectivityState.CONNECTING, ConnectivityState.READY);
Metadata headers = new Metadata();
PickResult pickResult = pickerCaptor.getValue().pickSubchannel(new PickSubchannelArgsImpl(TestMethodDescriptors.voidMethod().toBuilder().setFullMethodName("service1/create").build(), headers, CallOptions.DEFAULT));
assertThat(pickResult.getStatus().isOk()).isTrue();
assertThat(pickResult.getSubchannel()).isNotNull();
assertThat(headers.get(RLS_DATA_KEY)).isEqualTo("header-rls-data-value");
// move backoff further back to only test error behavior
fakeBackoffProvider.nextPolicy = createBackoffPolicy(100, TimeUnit.MILLISECONDS);
// try to get invalid
RouteLookupRequest invalidRouteLookupRequest = RouteLookupRequest.create(ImmutableMap.<String, String>of());
CachedRouteLookupResponse errorResp = getInSyncContext(invalidRouteLookupRequest);
assertThat(errorResp.isPending()).isTrue();
fakeTimeProvider.forwardTime(SERVER_LATENCY_MILLIS, TimeUnit.MILLISECONDS);
errorResp = getInSyncContext(invalidRouteLookupRequest);
assertThat(errorResp.hasError()).isTrue();
// Channel is still READY because the subchannel for method /service1/create is still READY.
// Method /doesn/exists will use fallback child balancer and fail immediately.
inOrder.verify(helper).updateBalancingState(eq(ConnectivityState.READY), pickerCaptor.capture());
pickResult = pickerCaptor.getValue().pickSubchannel(new PickSubchannelArgsImpl(TestMethodDescriptors.voidMethod().toBuilder().setFullMethodName("doesn/exists").build(), headers, CallOptions.DEFAULT));
assertThat(pickResult.getStatus().getCode()).isEqualTo(Code.UNAVAILABLE);
assertThat(pickResult.getStatus().getDescription()).isEqualTo("fallback not available");
}
Aggregations