Search in sources :

Example 1 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup in project grpc-java by grpc.

the class OobChannel method setSubchannel.

// Must be called only once, right after the OobChannel is created.
void setSubchannel(final InternalSubchannel subchannel) {
    log.log(Level.FINE, "[{0}] Created with [{1}]", new Object[] { this, subchannel });
    subchannelImpl = new SubchannelImpl() {

        @Override
        public void shutdown() {
            subchannel.shutdown();
        }

        @Override
        ClientTransport obtainActiveTransport() {
            return subchannel.obtainActiveTransport();
        }

        @Override
        public void requestConnection() {
            subchannel.obtainActiveTransport();
        }

        @Override
        public EquivalentAddressGroup getAddresses() {
            return subchannel.getAddressGroup();
        }

        @Override
        public Attributes getAttributes() {
            return Attributes.EMPTY;
        }
    };
    subchannelPicker = new SubchannelPicker() {

        final PickResult result = PickResult.withSubchannel(subchannelImpl);

        @Override
        public PickResult pickSubchannel(PickSubchannelArgs args) {
            return result;
        }
    };
    delayedTransport.reprocess(subchannelPicker);
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) PickResult(io.grpc.LoadBalancer.PickResult) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs)

Example 2 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup 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);
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 3 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup 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();
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) InOrder(org.mockito.InOrder) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 4 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup in project sharding-jdbc by shardingjdbc.

the class EtcdNameSolverFactory method newNameResolver.

@Nullable
@Override
public NameResolver newNameResolver(final URI targetUri, final Attributes params) {
    if (!scheme.equals(targetUri.getPath())) {
        return null;
    }
    return new NameResolver() {

        @Override
        public String getServiceAuthority() {
            return scheme;
        }

        @Override
        public void start(final Listener listener) {
            if (shutdown) {
                return;
            }
            List<EquivalentAddressGroup> equivalentAddressGroups = Lists.newArrayList();
            for (String each : endpoints) {
                try {
                    URI uri = new URI(each.trim());
                    if (!Strings.isNullOrEmpty(uri.getAuthority()) && SCHEMAS.matcher(uri.getScheme()).matches()) {
                        InetSocketAddress inetSocketAddress = new InetSocketAddress(uri.getHost(), uri.getPort());
                        equivalentAddressGroups.add(new EquivalentAddressGroup(inetSocketAddress));
                    }
                } catch (final URISyntaxException ex) {
                    listener.onError(Status.INVALID_ARGUMENT);
                    log.warn("Ignored illegal endpoint, %s", ex.getMessage());
                }
            }
            listener.onAddresses(equivalentAddressGroups, Attributes.EMPTY);
        }

        @Override
        public void shutdown() {
            if (!shutdown) {
                shutdown = true;
                executor = SharedResourceHolder.release(GrpcUtil.SHARED_CHANNEL_EXECUTOR, executor);
            }
        }
    };
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NameResolver(io.grpc.NameResolver) Nullable(javax.annotation.Nullable)

Example 5 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup in project grpc-java by grpc.

the class DnsNameResolverTest method assertAnswerMatches.

private static void assertAnswerMatches(List<InetAddress> addrs, int port, ResolutionResult resolutionResult) {
    assertThat(resolutionResult.getAddresses()).hasSize(addrs.size());
    for (int i = 0; i < addrs.size(); i++) {
        EquivalentAddressGroup addrGroup = resolutionResult.getAddresses().get(i);
        InetSocketAddress socketAddr = (InetSocketAddress) Iterables.getOnlyElement(addrGroup.getAddresses());
        assertEquals("Addr " + i, port, socketAddr.getPort());
        assertEquals("Addr " + i, addrs.get(i), socketAddr.getAddress());
    }
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)173 Test (org.junit.Test)133 Subchannel (io.grpc.LoadBalancer.Subchannel)54 SocketAddress (java.net.SocketAddress)42 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)41 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)39 Status (io.grpc.Status)39 Attributes (io.grpc.Attributes)37 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)33 InOrder (org.mockito.InOrder)31 Helper (io.grpc.LoadBalancer.Helper)30 Metadata (io.grpc.Metadata)29 ArrayList (java.util.ArrayList)26 InetSocketAddress (java.net.InetSocketAddress)24 PickResult (io.grpc.LoadBalancer.PickResult)22 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)20 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)20 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)19 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)19 ClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder)18