use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method validConfig_thenNoConfig_withDefaultConfig.
@Test
public void validConfig_thenNoConfig_withDefaultConfig() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> defaultServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"mate\"}}]}");
channelBuilder.defaultServiceConfig(defaultServiceConfig);
Map<String, Object> rawServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"1st raw config\"}}]}");
nameResolverFactory.nextRawServiceConfig.set(rawServiceConfig);
InternalConfigSelector configSelector = mock(InternalConfigSelector.class);
nameResolverFactory.nextAttributes.set(Attributes.newBuilder().set(InternalConfigSelector.KEY, configSelector).build());
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
// should use previous service config because new resolution result is no config.
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
// 2nd resolution lbConfig is no config
nameResolverFactory.nextRawServiceConfig.set(null);
nameResolverFactory.allResolved();
verify(mockLoadBalancer, times(2)).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses newResolvedAddress = resultCaptor.getValue();
assertThat(newResolvedAddress.getLoadBalancingPolicyConfig()).isEqualTo("mate");
assertThat(newResolvedAddress.getAttributes().get(InternalConfigSelector.KEY)).isNull();
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method noConfig_usingDefaultConfig.
@Test
public void noConfig_usingDefaultConfig() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> defaultServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"foo\"}}]}");
channelBuilder.defaultServiceConfig(defaultServiceConfig);
nameResolverFactory.nextRawServiceConfig.set(null);
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("foo");
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class PriorityLoadBalancerTest method handleResolvedAddresses.
@Test
public void handleResolvedAddresses() {
SocketAddress socketAddress = new InetSocketAddress(8080);
EquivalentAddressGroup eag = new EquivalentAddressGroup(socketAddress);
eag = AddressFilter.setPathFilter(eag, ImmutableList.of("p1"));
List<EquivalentAddressGroup> addresses = ImmutableList.of(eag);
Attributes attributes = Attributes.newBuilder().set(Attributes.Key.create("fakeKey"), "fakeValue").build();
Object fooConfig0 = new Object();
PriorityChildConfig priorityChildConfig0 = new PriorityChildConfig(new PolicySelection(fooLbProvider, fooConfig0), true);
Object barConfig0 = new Object();
PriorityChildConfig priorityChildConfig1 = new PriorityChildConfig(new PolicySelection(barLbProvider, barConfig0), true);
Object fooConfig1 = new Object();
PriorityChildConfig priorityChildConfig2 = new PriorityChildConfig(new PolicySelection(fooLbProvider, fooConfig1), true);
PriorityLbConfig priorityLbConfig = new PriorityLbConfig(ImmutableMap.of("p0", priorityChildConfig0, "p1", priorityChildConfig1, "p2", priorityChildConfig2), ImmutableList.of("p0", "p1", "p2"));
priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(addresses).setAttributes(attributes).setLoadBalancingPolicyConfig(priorityLbConfig).build());
assertThat(fooBalancers).hasSize(1);
assertThat(barBalancers).isEmpty();
LoadBalancer fooBalancer0 = Iterables.getOnlyElement(fooBalancers);
verify(fooBalancer0).handleResolvedAddresses(resolvedAddressesCaptor.capture());
ResolvedAddresses addressesReceived = resolvedAddressesCaptor.getValue();
assertThat(addressesReceived.getAddresses()).isEmpty();
assertThat(addressesReceived.getAttributes()).isEqualTo(attributes);
assertThat(addressesReceived.getLoadBalancingPolicyConfig()).isEqualTo(fooConfig0);
// Fail over to p1.
fakeClock.forwardTime(10, TimeUnit.SECONDS);
assertThat(fooBalancers).hasSize(1);
assertThat(barBalancers).hasSize(1);
LoadBalancer barBalancer0 = Iterables.getOnlyElement(barBalancers);
verify(barBalancer0).handleResolvedAddresses(resolvedAddressesCaptor.capture());
addressesReceived = resolvedAddressesCaptor.getValue();
assertThat(Iterables.getOnlyElement(addressesReceived.getAddresses()).getAddresses()).containsExactly(socketAddress);
assertThat(addressesReceived.getAttributes()).isEqualTo(attributes);
assertThat(addressesReceived.getLoadBalancingPolicyConfig()).isEqualTo(barConfig0);
// Fail over to p2.
fakeClock.forwardTime(10, TimeUnit.SECONDS);
assertThat(fooBalancers).hasSize(2);
assertThat(barBalancers).hasSize(1);
LoadBalancer fooBalancer1 = Iterables.getLast(fooBalancers);
verify(fooBalancer1).handleResolvedAddresses(resolvedAddressesCaptor.capture());
addressesReceived = resolvedAddressesCaptor.getValue();
assertThat(addressesReceived.getAddresses()).isEmpty();
assertThat(addressesReceived.getAttributes()).isEqualTo(attributes);
assertThat(addressesReceived.getLoadBalancingPolicyConfig()).isEqualTo(fooConfig1);
// New update: p0 and p2 deleted; p1 config changed.
SocketAddress newSocketAddress = new InetSocketAddress(8081);
EquivalentAddressGroup newEag = new EquivalentAddressGroup(newSocketAddress);
newEag = AddressFilter.setPathFilter(newEag, ImmutableList.of("p1"));
List<EquivalentAddressGroup> newAddresses = ImmutableList.of(newEag);
Object newBarConfig = new Object();
PriorityLbConfig newPriorityLbConfig = new PriorityLbConfig(ImmutableMap.of("p1", new PriorityChildConfig(new PolicySelection(barLbProvider, newBarConfig), true)), ImmutableList.of("p1"));
priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(newAddresses).setLoadBalancingPolicyConfig(newPriorityLbConfig).build());
assertThat(fooBalancers).hasSize(2);
assertThat(barBalancers).hasSize(1);
verify(barBalancer0, times(2)).handleResolvedAddresses(resolvedAddressesCaptor.capture());
addressesReceived = resolvedAddressesCaptor.getValue();
assertThat(Iterables.getOnlyElement(addressesReceived.getAddresses()).getAddresses()).containsExactly(newSocketAddress);
assertThat(addressesReceived.getAttributes()).isEqualTo(Attributes.EMPTY);
assertThat(addressesReceived.getLoadBalancingPolicyConfig()).isEqualTo(newBarConfig);
verify(fooBalancer0, never()).shutdown();
verify(fooBalancer1, never()).shutdown();
fakeClock.forwardTime(15, TimeUnit.MINUTES);
verify(fooBalancer0).shutdown();
verify(fooBalancer1).shutdown();
verify(barBalancer0, never()).shutdown();
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class HealthCheckingLoadBalancerFactoryTest method serviceConfigChangesServiceNameWhenRpcActive.
@Test
public void serviceConfigChangesServiceNameWhenRpcActive() {
Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
ResolvedAddresses result1 = ResolvedAddresses.newBuilder().setAddresses(resolvedAddressList).setAttributes(resolutionAttrs).build();
hcLbEventDelivery.handleResolvedAddresses(result1);
verify(origLb).handleResolvedAddresses(result1);
verifyNoMoreInteractions(origLb);
Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
SubchannelStateListener mockListener = mockStateListeners[0];
assertThat(unwrap(subchannel)).isSameInstanceAs(subchannels[0]);
InOrder inOrder = inOrder(origLb, mockListener);
deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));
inOrder.verify(mockListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(CONNECTING)));
HealthImpl healthImpl = healthImpls[0];
assertThat(healthImpl.calls).hasSize(1);
ServerSideCall serverCall = healthImpl.calls.poll();
assertThat(serverCall.cancelled).isFalse();
assertThat(serverCall.request).isEqualTo(makeRequest("TeeService"));
// Health check responded
serverCall.responseObserver.onNext(makeResponse(ServingStatus.SERVING));
inOrder.verify(mockListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(READY)));
// Service config returns with the same health check name.
hcLbEventDelivery.handleResolvedAddresses(result1);
// It's delivered to origLb, but nothing else happens
inOrder.verify(origLb).handleResolvedAddresses(result1);
verifyNoMoreInteractions(origLb, mockListener);
// Service config returns a different health check name.
resolutionAttrs = attrsWithHealthCheckService("FooService");
ResolvedAddresses result2 = ResolvedAddresses.newBuilder().setAddresses(resolvedAddressList).setAttributes(resolutionAttrs).build();
hcLbEventDelivery.handleResolvedAddresses(result2);
inOrder.verify(origLb).handleResolvedAddresses(result2);
// Current health check RPC cancelled.
assertThat(serverCall.cancelled).isTrue();
// A second RPC is started immediately
assertThat(healthImpl.calls).hasSize(1);
serverCall = healthImpl.calls.poll();
// with the new service name
assertThat(serverCall.request).isEqualTo(makeRequest("FooService"));
// State stays in READY, instead of switching to CONNECTING.
verifyNoMoreInteractions(origLb, mockListener);
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class HealthCheckingLoadBalancerFactoryTest method serviceConfigChangesServiceNameWhenRpcInactive.
@Test
public void serviceConfigChangesServiceNameWhenRpcInactive() {
Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
ResolvedAddresses result1 = ResolvedAddresses.newBuilder().setAddresses(resolvedAddressList).setAttributes(resolutionAttrs).build();
hcLbEventDelivery.handleResolvedAddresses(result1);
verify(origLb).handleResolvedAddresses(result1);
verifyNoMoreInteractions(origLb);
Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
SubchannelStateListener mockListener = mockStateListeners[0];
assertThat(unwrap(subchannel)).isSameInstanceAs(subchannels[0]);
InOrder inOrder = inOrder(origLb, mockListener);
HealthImpl healthImpl = healthImpls[0];
// Underlying subchannel is not READY initially
ConnectivityStateInfo underlyingErrorState = ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE.withDescription("connection refused"));
deliverSubchannelState(0, underlyingErrorState);
inOrder.verify(mockListener).onSubchannelState(same(underlyingErrorState));
inOrder.verifyNoMoreInteractions();
// Service config returns with the same health check name.
hcLbEventDelivery.handleResolvedAddresses(result1);
// It's delivered to origLb, but nothing else happens
inOrder.verify(origLb).handleResolvedAddresses(result1);
assertThat(healthImpl.calls).isEmpty();
verifyNoMoreInteractions(origLb);
// Service config returns a different health check name.
resolutionAttrs = attrsWithHealthCheckService("FooService");
ResolvedAddresses result2 = ResolvedAddresses.newBuilder().setAddresses(resolvedAddressList).setAttributes(resolutionAttrs).build();
hcLbEventDelivery.handleResolvedAddresses(result2);
inOrder.verify(origLb).handleResolvedAddresses(result2);
// Underlying subchannel is now ready
deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));
// Concluded CONNECTING state
inOrder.verify(mockListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(CONNECTING)));
// Health check RPC is started
assertThat(healthImpl.calls).hasSize(1);
// with the new service name
assertThat(healthImpl.calls.poll().request).isEqualTo(makeRequest("FooService"));
verifyNoMoreInteractions(origLb, mockListener);
}
Aggregations