Search in sources :

Example 21 with SubchannelStateListener

use of io.grpc.LoadBalancer.SubchannelStateListener in project grpc-java by grpc.

the class OrcaOobUtilTest method singlePolicyTypicalWorkflow.

@Test
@SuppressWarnings("unchecked")
public void singlePolicyTypicalWorkflow() {
    setOrcaReportConfig(orcaHelperWrapper, SHORT_INTERVAL_CONFIG);
    verify(origHelper, atLeast(0)).getSynchronizationContext();
    verifyNoMoreInteractions(origHelper);
    // Calling createSubchannel() on orcaHelper correctly passes augmented CreateSubchannelArgs
    // to origHelper.
    ArgumentCaptor<CreateSubchannelArgs> createArgsCaptor = ArgumentCaptor.forClass(null);
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        String subchannelAttrValue = "eag attr " + i;
        Attributes attrs = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, subchannelAttrValue).build();
        assertThat(unwrap(createSubchannel(orcaHelperWrapper.asHelper(), i, attrs))).isSameInstanceAs(subchannels[i]);
        verify(origHelper, times(i + 1)).createSubchannel(createArgsCaptor.capture());
        assertThat(createArgsCaptor.getValue().getAddresses()).isEqualTo(eagLists[i]);
        assertThat(createArgsCaptor.getValue().getAttributes().get(SUBCHANNEL_ATTR_KEY)).isEqualTo(subchannelAttrValue);
    }
    // ORCA reporting does not start until underlying Subchannel is READY.
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        FakeSubchannel subchannel = subchannels[i];
        OpenRcaServiceImp orcaServiceImp = orcaServiceImps[i];
        SubchannelStateListener mockStateListener = mockStateListeners[i];
        InOrder inOrder = inOrder(mockStateListener);
        deliverSubchannelState(i, ConnectivityStateInfo.forNonError(IDLE));
        deliverSubchannelState(i, ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE));
        deliverSubchannelState(i, ConnectivityStateInfo.forNonError(CONNECTING));
        inOrder.verify(mockStateListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(IDLE)));
        inOrder.verify(mockStateListener).onSubchannelState(eq(ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE)));
        inOrder.verify(mockStateListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(CONNECTING)));
        verifyNoMoreInteractions(mockStateListener);
        assertThat(subchannel.logs).isEmpty();
        assertThat(orcaServiceImp.calls).isEmpty();
        verifyNoMoreInteractions(mockOrcaListener0);
        deliverSubchannelState(i, ConnectivityStateInfo.forNonError(READY));
        verify(mockStateListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(READY)));
        assertThat(orcaServiceImp.calls).hasSize(1);
        ServerSideCall serverCall = orcaServiceImp.calls.peek();
        assertThat(serverCall.request).isEqualTo(buildOrcaRequestFromConfig(SHORT_INTERVAL_CONFIG));
        assertLog(subchannel.logs, "DEBUG: Starting ORCA reporting for " + subchannel.getAllAddresses());
        // Simulate an ORCA service response. Registered listener will receive an ORCA report for
        // each backend.
        OrcaLoadReport report = OrcaLoadReport.getDefaultInstance();
        serverCall.responseObserver.onNext(report);
        assertLog(subchannel.logs, "DEBUG: Received an ORCA report: " + report);
        verify(mockOrcaListener0, times(i + 1)).onLoadReport(eq(report));
    }
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        FakeSubchannel subchannel = subchannels[i];
        SubchannelStateListener mockStateListener = mockStateListeners[i];
        ServerSideCall serverCall = orcaServiceImps[i].calls.peek();
        assertThat(serverCall.cancelled).isFalse();
        verifyNoMoreInteractions(mockStateListener);
        // Shutting down the subchannel will cancel the ORCA reporting RPC.
        subchannel.shutdown();
        verify(mockStateListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(SHUTDOWN)));
        assertThat(serverCall.cancelled).isTrue();
        assertThat(subchannel.logs).isEmpty();
        verifyNoMoreInteractions(mockOrcaListener0);
    }
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        assertThat(orcaServiceImps[i].calls).hasSize(1);
    }
    verifyNoInteractions(backoffPolicyProvider);
}
Also used : SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) InOrder(org.mockito.InOrder) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) Attributes(io.grpc.Attributes) OrcaLoadReport(com.github.xds.data.orca.v3.OrcaLoadReport) Test(org.junit.Test)

Example 22 with SubchannelStateListener

use of io.grpc.LoadBalancer.SubchannelStateListener in project grpc-java by grpc.

the class OrcaOobUtilTest method orcReportingDisabledWhenServiceNotImplemented.

@Test
@SuppressWarnings("unchecked")
public void orcReportingDisabledWhenServiceNotImplemented() {
    setOrcaReportConfig(orcaHelperWrapper, SHORT_INTERVAL_CONFIG);
    createSubchannel(orcaHelperWrapper.asHelper(), 0, Attributes.EMPTY);
    FakeSubchannel subchannel = subchannels[0];
    OpenRcaServiceImp orcaServiceImp = orcaServiceImps[0];
    SubchannelStateListener mockStateListener = mockStateListeners[0];
    deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));
    verify(mockStateListener).onSubchannelState(eq(ConnectivityStateInfo.forNonError(READY)));
    assertThat(orcaServiceImp.calls).hasSize(1);
    ServerSideCall serverCall = orcaServiceImp.calls.poll();
    assertThat(serverCall.request).isEqualTo(buildOrcaRequestFromConfig(SHORT_INTERVAL_CONFIG));
    subchannel.logs.clear();
    serverCall.responseObserver.onError(Status.UNIMPLEMENTED.asException());
    assertLog(subchannel.logs, "ERROR: OpenRcaService disabled: " + Status.UNIMPLEMENTED);
    verifyNoMoreInteractions(mockOrcaListener0);
    // Re-connecting on Subchannel will reset the "disabled" flag and restart ORCA reporting.
    assertThat(orcaServiceImp.calls).hasSize(0);
    deliverSubchannelState(0, ConnectivityStateInfo.forNonError(IDLE));
    deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));
    assertLog(subchannel.logs, "DEBUG: Starting ORCA reporting for " + subchannel.getAllAddresses());
    assertThat(orcaServiceImp.calls).hasSize(1);
    serverCall = orcaServiceImp.calls.poll();
    OrcaLoadReport report = OrcaLoadReport.getDefaultInstance();
    serverCall.responseObserver.onNext(report);
    assertLog(subchannel.logs, "DEBUG: Received an ORCA report: " + report);
    verify(mockOrcaListener0).onLoadReport(eq(report));
    verifyNoInteractions(backoffPolicyProvider);
}
Also used : SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) OrcaLoadReport(com.github.xds.data.orca.v3.OrcaLoadReport) Test(org.junit.Test)

Aggregations

SubchannelStateListener (io.grpc.LoadBalancer.SubchannelStateListener)22 Test (org.junit.Test)19 Subchannel (io.grpc.LoadBalancer.Subchannel)13 InOrder (org.mockito.InOrder)13 Attributes (io.grpc.Attributes)11 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)7 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)6 Status (io.grpc.Status)6 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)5 OrcaLoadReport (com.github.xds.data.orca.v3.OrcaLoadReport)4 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)4 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)4 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)4 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)3 Helper (io.grpc.LoadBalancer.Helper)3 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)3 ClientTransportOptions (io.grpc.internal.ClientTransportFactory.ClientTransportOptions)3 SocketAddress (java.net.SocketAddress)3 ChannelLogger (io.grpc.ChannelLogger)2 ClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder)2