Search in sources :

Example 26 with CreateSubchannelArgs

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

the class PickFirstLoadBalancerTest method requestConnection.

@Test
public void requestConnection() {
    loadBalancer.requestConnection();
    verify(mockSubchannel, never()).requestConnection();
    loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(affinity).build());
    verify(mockSubchannel).requestConnection();
    verify(mockHelper).createSubchannel(createArgsCaptor.capture());
    verify(mockSubchannel).start(stateListenerCaptor.capture());
    CreateSubchannelArgs args = createArgsCaptor.getValue();
    assertThat(args.getAddresses()).isEqualTo(servers);
    SubchannelStateListener stateListener = stateListenerCaptor.getValue();
    stateListener.onSubchannelState(ConnectivityStateInfo.forNonError(IDLE));
    verify(mockHelper).updateBalancingState(eq(IDLE), any(SubchannelPicker.class));
    verify(mockSubchannel).requestConnection();
    loadBalancer.requestConnection();
    verify(mockSubchannel, times(2)).requestConnection();
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) Test(org.junit.Test)

Example 27 with CreateSubchannelArgs

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

the class OrcaOobUtilTest method twoLevelPoliciesTypicalWorkflow.

@Test
public void twoLevelPoliciesTypicalWorkflow() {
    setOrcaReportConfig(childHelperWrapper, SHORT_INTERVAL_CONFIG);
    setOrcaReportConfig(parentHelperWrapper, SHORT_INTERVAL_CONFIG);
    verify(origHelper, atLeast(0)).getSynchronizationContext();
    verifyNoMoreInteractions(origHelper);
    // Calling createSubchannel() on child helper 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(childHelperWrapper.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(mockOrcaListener1);
        verifyNoMoreInteractions(mockOrcaListener2);
        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(mockOrcaListener1, times(i + 1)).onLoadReport(eq(report));
        verify(mockOrcaListener2, 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(mockOrcaListener1, mockOrcaListener2);
    }
    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 28 with CreateSubchannelArgs

use of io.grpc.LoadBalancer.CreateSubchannelArgs 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 29 with CreateSubchannelArgs

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

the class ClusterImplLoadBalancerTest method endpointAddressesAttachedWithClusterName.

@Test
public void endpointAddressesAttachedWithClusterName() {
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    // One locality with two endpoints.
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr1", locality);
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr2", locality);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    // one leaf balancer
    assertThat(downstreamBalancers).hasSize(1);
    FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(leafBalancer.name).isEqualTo("round_robin");
    // Simulates leaf load balancer creating subchannels.
    CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build();
    Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_CLUSTER_NAME)).isEqualTo(CLUSTER);
    }
}
Also used : CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalancerProvider(io.grpc.LoadBalancerProvider) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 30 with CreateSubchannelArgs

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

the class ClusterImplLoadBalancerTest method subtest_endpointAddressesAttachedWithTlsConfig.

private void subtest_endpointAddressesAttachedWithTlsConfig(boolean enableSecurity) {
    UpstreamTlsContext upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext("google_cloud_private_spiffe", true);
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), upstreamTlsContext);
    // One locality with two endpoints.
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr1", locality);
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr2", locality);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    // one leaf balancer
    assertThat(downstreamBalancers).hasSize(1);
    FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(leafBalancer.name).isEqualTo("round_robin");
    // Simulates leaf load balancer creating subchannels.
    CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build();
    Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        SslContextProviderSupplier supplier = eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
        if (enableSecurity) {
            assertThat(supplier.getTlsContext()).isEqualTo(upstreamTlsContext);
        } else {
            assertThat(supplier).isNull();
        }
    }
    // Removes UpstreamTlsContext from the config.
    config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    assertThat(Iterables.getOnlyElement(downstreamBalancers)).isSameInstanceAs(leafBalancer);
    // creates new connections
    subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER)).isNull();
    }
    // Config with a new UpstreamTlsContext.
    upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext("google_cloud_private_spiffe1", true);
    config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), upstreamTlsContext);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    assertThat(Iterables.getOnlyElement(downstreamBalancers)).isSameInstanceAs(leafBalancer);
    // creates new connections
    subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        SslContextProviderSupplier supplier = eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
        if (enableSecurity) {
            assertThat(supplier.isShutdown()).isFalse();
            assertThat(supplier.getTlsContext()).isEqualTo(upstreamTlsContext);
        } else {
            assertThat(supplier).isNull();
        }
    }
    loadBalancer.shutdown();
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        SslContextProviderSupplier supplier = eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
        if (enableSecurity) {
            assertThat(supplier.isShutdown()).isTrue();
        }
    }
    loadBalancer = null;
}
Also used : CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) UpstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) DropOverload(io.grpc.xds.Endpoints.DropOverload) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) SslContextProviderSupplier(io.grpc.xds.internal.sds.SslContextProviderSupplier)

Aggregations

CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)32 Test (org.junit.Test)29 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 Subchannel (io.grpc.LoadBalancer.Subchannel)14 InOrder (org.mockito.InOrder)13 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 Status (io.grpc.Status)9 SocketAddress (java.net.SocketAddress)8 Attributes (io.grpc.Attributes)6 ConnectivityState (io.grpc.ConnectivityState)6 Helper (io.grpc.LoadBalancer.Helper)6 SubchannelStateListener (io.grpc.LoadBalancer.SubchannelStateListener)6 AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)5 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)5 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)5 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)5 InetSocketAddress (java.net.InetSocketAddress)5 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)4 InitialLoadBalanceRequest (io.grpc.lb.v1.InitialLoadBalanceRequest)4 LoadBalanceRequest (io.grpc.lb.v1.LoadBalanceRequest)4