Search in sources :

Example 16 with LoadBalanceRequest

use of io.grpc.lb.v1.LoadBalanceRequest in project grpc-java by grpc.

the class GrpclbState method startLbRpc.

private void startLbRpc() {
    checkState(lbStream == null, "previous lbStream has not been cleared yet");
    LoadBalancerGrpc.LoadBalancerStub stub = LoadBalancerGrpc.newStub(lbCommChannel);
    lbStream = new LbStream(stub);
    Context prevContext = context.attach();
    try {
        lbStream.start();
    } finally {
        context.detach(prevContext);
    }
    stopwatch.reset().start();
    LoadBalanceRequest initRequest = LoadBalanceRequest.newBuilder().setInitialRequest(InitialLoadBalanceRequest.newBuilder().setName(serviceName).build()).build();
    logger.log(ChannelLogLevel.DEBUG, "[grpclb-<{0}>] Sent initial grpclb request {1}", serviceName, initRequest);
    try {
        lbStream.lbRequestWriter.onNext(initRequest);
    } catch (Exception e) {
        lbStream.close(e);
    }
}
Also used : Context(io.grpc.Context) SynchronizationContext(io.grpc.SynchronizationContext) LoadBalanceRequest(io.grpc.lb.v1.LoadBalanceRequest) InitialLoadBalanceRequest(io.grpc.lb.v1.InitialLoadBalanceRequest) LoadBalancerGrpc(io.grpc.lb.v1.LoadBalancerGrpc) UnknownHostException(java.net.UnknownHostException)

Example 17 with LoadBalanceRequest

use of io.grpc.lb.v1.LoadBalanceRequest in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method raceBetweenHandleAddressesAndLbStreamClosure.

@SuppressWarnings("unchecked")
@Test
public void raceBetweenHandleAddressesAndLbStreamClosure() {
    InOrder inOrder = inOrder(mockLbService, backoffPolicyProvider, backoffPolicy1);
    deliverResolvedAddresses(Collections.<EquivalentAddressGroup>emptyList(), createResolvedBalancerAddresses(1));
    assertEquals(1, fakeOobChannels.size());
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
    assertEquals(1, lbRequestObservers.size());
    StreamObserver<LoadBalanceRequest> lbRequestObserver = lbRequestObservers.poll();
    verify(lbRequestObserver).onNext(eq(LoadBalanceRequest.newBuilder().setInitialRequest(InitialLoadBalanceRequest.newBuilder().setName(SERVICE_AUTHORITY).build()).build()));
    // Close lbStream
    lbResponseObserver.onCompleted();
    inOrder.verify(backoffPolicyProvider).get();
    inOrder.verify(backoffPolicy1).nextBackoffNanos();
    // Retry task scheduled
    assertEquals(1, fakeClock.numPendingTasks(LB_RPC_RETRY_TASK_FILTER));
    FakeClock.ScheduledTask retryTask = Iterables.getOnlyElement(fakeClock.getPendingTasks(LB_RPC_RETRY_TASK_FILTER));
    assertEquals(10L, retryTask.getDelay(TimeUnit.NANOSECONDS));
    // Receive the same Lb address again
    deliverResolvedAddresses(Collections.<EquivalentAddressGroup>emptyList(), createResolvedBalancerAddresses(1));
    // Retry task cancelled
    assertEquals(0, fakeClock.numPendingTasks(LB_RPC_RETRY_TASK_FILTER));
    // Reuse the existing OOB channel
    assertEquals(1, fakeOobChannels.size());
    // Start a new LoadBalance RPC
    inOrder.verify(mockLbService).balanceLoad(any(StreamObserver.class));
    assertEquals(1, lbRequestObservers.size());
    lbRequestObserver = lbRequestObservers.poll();
    verify(lbRequestObserver).onNext(eq(LoadBalanceRequest.newBuilder().setInitialRequest(InitialLoadBalanceRequest.newBuilder().setName(SERVICE_AUTHORITY).build()).build()));
    // Simulate a race condition where the task has just started when it's cancelled
    retryTask.command.run();
    inOrder.verifyNoMoreInteractions();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) InOrder(org.mockito.InOrder) FakeClock(io.grpc.internal.FakeClock) LoadBalanceRequest(io.grpc.lb.v1.LoadBalanceRequest) InitialLoadBalanceRequest(io.grpc.lb.v1.InitialLoadBalanceRequest) InitialLoadBalanceResponse(io.grpc.lb.v1.InitialLoadBalanceResponse) LoadBalanceResponse(io.grpc.lb.v1.LoadBalanceResponse) Test(org.junit.Test)

Aggregations

InitialLoadBalanceRequest (io.grpc.lb.v1.InitialLoadBalanceRequest)17 LoadBalanceRequest (io.grpc.lb.v1.LoadBalanceRequest)17 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)16 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)16 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 InOrder (org.mockito.InOrder)15 Test (org.junit.Test)14 Subchannel (io.grpc.LoadBalancer.Subchannel)12 ConnectivityState (io.grpc.ConnectivityState)9 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)9 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 Attributes (io.grpc.Attributes)8 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)7 ManagedChannel (io.grpc.ManagedChannel)7 PickResult (io.grpc.LoadBalancer.PickResult)6 Status (io.grpc.Status)5 BackendEntry (io.grpc.grpclb.GrpclbState.BackendEntry)5 RoundRobinPicker (io.grpc.grpclb.GrpclbState.RoundRobinPicker)5 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)4 DropEntry (io.grpc.grpclb.GrpclbState.DropEntry)3