use of io.grpc.Attributes in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method nameResolutionFailsThenRecoverToGrpclb.
@Test
public void nameResolutionFailsThenRecoverToGrpclb() {
Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
deliverNameResolutionError(error);
verify(helper).updatePicker(pickerCaptor.capture());
ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
assertSame(error, errorPicker.result.getStatus());
// Recover with a subsequent success
List<ResolvedServerInfoGroup> resolvedServers = createResolvedServerInfoGroupList(true);
EquivalentAddressGroup eag = resolvedServers.get(0).toEquivalentAddressGroup();
Attributes resolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
deliverResolvedAddresses(resolvedServers, resolutionAttrs);
assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
assertNull(balancer.getDelegate());
verify(helper).createOobChannel(eq(eag), eq(lbAuthority(0)));
verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
verifyNoMoreInteractions(pickFirstBalancerFactory);
verifyNoMoreInteractions(pickFirstBalancer);
verifyNoMoreInteractions(roundRobinBalancerFactory);
verifyNoMoreInteractions(roundRobinBalancer);
}
use of io.grpc.Attributes in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method grpclbBalanerCommErrors.
@Test
public void grpclbBalanerCommErrors() {
InOrder inOrder = inOrder(helper, mockLbService);
// Make the first LB address fail to connect
failingLbAuthorities.add(lbAuthority(0));
List<ResolvedServerInfoGroup> grpclbResolutionList = createResolvedServerInfoGroupList(true, true, true);
Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
// First LB addr fails to connect
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
inOrder.verify(helper).updatePicker(isA(ErrorPicker.class));
assertEquals(2, fakeOobChannels.size());
assertTrue(fakeOobChannels.poll().isShutdown());
// Will move on to second LB addr
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(1).toEquivalentAddressGroup()), eq(lbAuthority(1)));
inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
assertEquals(1, lbRequestObservers.size());
lbRequestObservers.poll();
assertEquals(1, fakeOobChannels.size());
assertFalse(fakeOobChannels.peek().isShutdown());
Status error1 = Status.UNAVAILABLE.withDescription("error1");
// Simulate that the stream on the second LB failed
lbResponseObserver.onError(error1.asException());
assertTrue(fakeOobChannels.poll().isShutdown());
inOrder.verify(helper).updatePicker(pickerCaptor.capture());
ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
assertEquals(error1.getCode(), errorPicker.result.getStatus().getCode());
assertTrue(errorPicker.result.getStatus().getDescription().contains(error1.getDescription()));
// Move on to the third LB.
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(2).toEquivalentAddressGroup()), eq(lbAuthority(2)));
inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
lbResponseObserver = lbResponseObserverCaptor.getValue();
assertEquals(1, lbRequestObservers.size());
lbRequestObservers.poll();
assertEquals(1, fakeOobChannels.size());
assertFalse(fakeOobChannels.peek().isShutdown());
// Simulate that the stream on the third LB closed without error. It is treated
// as an error.
lbResponseObserver.onCompleted();
assertTrue(fakeOobChannels.poll().isShutdown());
// Loop back to the first LB addr, which still fails.
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
inOrder.verify(helper).updatePicker(isA(ErrorPicker.class));
assertEquals(2, fakeOobChannels.size());
assertTrue(fakeOobChannels.poll().isShutdown());
// Will move on to second LB addr
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(1).toEquivalentAddressGroup()), eq(lbAuthority(1)));
inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
lbResponseObserver = lbResponseObserverCaptor.getValue();
assertEquals(1, lbRequestObservers.size());
assertEquals(1, fakeOobChannels.size());
assertFalse(fakeOobChannels.peek().isShutdown());
// Finally it works.
lbResponseObserver.onNext(buildInitialResponse());
List<ServerEntry> backends = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "token001"), new ServerEntry("127.0.0.1", 2010, "token002"));
lbResponseObserver.onNext(buildLbResponse(backends));
inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(0).addr)), any(Attributes.class));
inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(1).addr)), any(Attributes.class));
inOrder.verify(helper).updatePicker(same(GrpclbLoadBalancer.BUFFER_PICKER));
inOrder.verifyNoMoreInteractions();
}
use of io.grpc.Attributes in project grpc-java by grpc.
the class RoundRobinLoadBalancerTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
for (int i = 0; i < 3; i++) {
SocketAddress addr = new FakeSocketAddress("server" + i);
EquivalentAddressGroup eag = new EquivalentAddressGroup(addr);
servers.put(ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(addr)).build(), eag);
subchannels.put(eag, mock(Subchannel.class));
}
when(mockHelper.createSubchannel(any(EquivalentAddressGroup.class), any(Attributes.class))).then(new Answer<Subchannel>() {
@Override
public Subchannel answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Subchannel subchannel = subchannels.get(args[0]);
when(subchannel.getAttributes()).thenReturn((Attributes) args[1]);
return subchannel;
}
});
loadBalancer = (RoundRobinLoadBalancer) RoundRobinLoadBalancerFactory.getInstance().newLoadBalancer(mockHelper);
}
use of io.grpc.Attributes in project grpc-java by grpc.
the class ClientCallImplTest method getAttributes.
@Test
public void getAttributes() {
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
Attributes attrs = Attributes.newBuilder().set(Key.<String>of("fake key"), "fake value").build();
when(stream.getAttributes()).thenReturn(attrs);
assertNotEquals(attrs, call.getAttributes());
call.start(callListener, new Metadata());
assertEquals(attrs, call.getAttributes());
}
use of io.grpc.Attributes in project grpc-java by grpc.
the class CallCredentialsApplyingTest method parameterPropagation_overrideByTransport.
@Test
public void parameterPropagation_overrideByTransport() {
Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).set(CallCredentials.ATTR_AUTHORITY, "transport-override-authority").set(CallCredentials.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY).build();
when(mockTransport.getAttributes()).thenReturn(transportAttrs);
transport.newStream(method, origHeaders, callOptions, statsTraceCtx);
ArgumentCaptor<Attributes> attrsCaptor = ArgumentCaptor.forClass(null);
verify(mockCreds).applyRequestMetadata(same(method), attrsCaptor.capture(), same(mockExecutor), any(MetadataApplier.class));
Attributes attrs = attrsCaptor.getValue();
assertSame(ATTR_VALUE, attrs.get(ATTR_KEY));
assertEquals("transport-override-authority", attrs.get(CallCredentials.ATTR_AUTHORITY));
assertSame(SecurityLevel.INTEGRITY, attrs.get(CallCredentials.ATTR_SECURITY_LEVEL));
}
Aggregations