Java Code Examples for io.grpc.LoadBalancer#Factory

The following examples show how to use io.grpc.LoadBalancer#Factory . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CdsLoadBalancer.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
  logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
  if (xdsClientPool == null) {
    xdsClientPool = resolvedAddresses.getAttributes().get(XdsAttributes.XDS_CLIENT_POOL);
    checkNotNull(xdsClientPool, "missing xDS client pool");
    xdsClient = xdsClientPool.getObject();
  }

  Object lbConfig = resolvedAddresses.getLoadBalancingPolicyConfig();
  checkNotNull(lbConfig, "missing CDS lb config");
  CdsConfig newCdsConfig = (CdsConfig) lbConfig;
  logger.log(
      XdsLogLevel.INFO,
      "Received CDS lb config: cluster={0}", newCdsConfig.name);

  // If cluster is changed, do a graceful switch.
  if (!newCdsConfig.name.equals(clusterName)) {
    LoadBalancer.Factory clusterBalancerFactory = new ClusterBalancerFactory(newCdsConfig.name);
    switchingLoadBalancer.switchTo(clusterBalancerFactory);
  }
  switchingLoadBalancer.handleResolvedAddresses(resolvedAddresses);
  clusterName = newCdsConfig.name;
}
 
Example 2
Source File: AbstractManagedChannelImplBuilderTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void loadBalancerFactory_null() {
  LoadBalancer.Factory defaultValue = builder.loadBalancerFactory;
  builder.loadBalancerFactory(mock(LoadBalancer.Factory.class));
  assertEquals(builder, builder.loadBalancerFactory(null));
  assertEquals(defaultValue, builder.loadBalancerFactory);
}
 
Example 3
Source File: AbstractManagedChannelImplBuilderTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void loadBalancerFactory_normal() {
  LoadBalancer.Factory loadBalancerFactory = mock(LoadBalancer.Factory.class);
  assertEquals(builder, builder.loadBalancerFactory(loadBalancerFactory));
  assertEquals(loadBalancerFactory, builder.loadBalancerFactory);
}
 
Example 4
Source File: GrpcEngine.java    From saluki with Apache License 2.0 4 votes vote down vote up
private LoadBalancer.Factory buildLoadBalanceFactory() {
  return GrpcRouteRoundRobinLbFactory.getInstance();
}
 
Example 5
Source File: GrpcClientAutoConfiguration.java    From spring-boot-starter-grpc with Apache License 2.0 4 votes vote down vote up
@ConditionalOnMissingBean
@Bean
public LoadBalancer.Factory defaultGrpcLoadBalancerFactory() {
	return RoundRobinLoadBalancerFactory.getInstance();
}
 
Example 6
Source File: EdsLoadBalancer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
  logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
  Object lbConfig = resolvedAddresses.getLoadBalancingPolicyConfig();
  if (lbConfig == null) {
    handleNameResolutionError(Status.UNAVAILABLE.withDescription("Missing EDS lb config"));
    return;
  }
  EdsConfig newEdsConfig = (EdsConfig) lbConfig;
  if (logger.isLoggable(XdsLogLevel.INFO)) {
    logger.log(
        XdsLogLevel.INFO,
        "Received EDS lb config: cluster={0}, child_policy={1}, "
            + "eds_service_name={2}, report_load={3}",
        newEdsConfig.clusterName,
        newEdsConfig.endpointPickingPolicy.getProvider().getPolicyName(),
        newEdsConfig.edsServiceName,
        newEdsConfig.lrsServerName != null);
  }
  boolean firstUpdate = false;
  if (clusterName == null) {
    firstUpdate = true;
  }
  clusterName = newEdsConfig.clusterName;
  if (xdsClientPool == null) {
    Attributes attributes = resolvedAddresses.getAttributes();
    xdsClientPool = attributes.get(XdsAttributes.XDS_CLIENT_POOL);
    if (xdsClientPool == null) {
      final BootstrapInfo bootstrapInfo;
      try {
        bootstrapInfo = bootstrapper.readBootstrap();
      } catch (Exception e) {
        helper.updateBalancingState(
            TRANSIENT_FAILURE,
            new ErrorPicker(
                Status.UNAVAILABLE.withDescription("Failed to bootstrap").withCause(e)));
        return;
      }

      final List<ServerInfo> serverList = bootstrapInfo.getServers();
      final Node node = bootstrapInfo.getNode();
      if (serverList.isEmpty()) {
        helper.updateBalancingState(
            TRANSIENT_FAILURE,
            new ErrorPicker(
                Status.UNAVAILABLE
                    .withDescription("No management server provided by bootstrap")));
        return;
      }
      XdsClientFactory xdsClientFactory = new XdsClientFactory() {
        @Override
        XdsClient createXdsClient() {
          return
              new XdsClientImpl(
                  helper.getAuthority(),
                  serverList,
                  channelFactory,
                  node,
                  helper.getSynchronizationContext(),
                  helper.getScheduledExecutorService(),
                  new ExponentialBackoffPolicy.Provider(),
                  GrpcUtil.STOPWATCH_SUPPLIER);
        }
      };
      xdsClientPool = new RefCountedXdsClientObjectPool(xdsClientFactory);
    } else {
      logger.log(XdsLogLevel.INFO, "Use xDS client from channel");
    }
    xdsClient = xdsClientPool.getObject();
  }

  // Note: childPolicy change will be handled in LocalityStore, to be implemented.
  // If edsServiceName in XdsConfig is changed, do a graceful switch.
  if (firstUpdate || !Objects.equals(newEdsConfig.edsServiceName, edsServiceName)) {
    LoadBalancer.Factory clusterEndpointsLoadBalancerFactory =
        new ClusterEndpointsBalancerFactory(newEdsConfig.edsServiceName);
    switchingLoadBalancer.switchTo(clusterEndpointsLoadBalancerFactory);
  }
  switchingLoadBalancer.handleResolvedAddresses(resolvedAddresses);
  this.edsServiceName = newEdsConfig.edsServiceName;
}
 
Example 7
Source File: GracefulSwitchLoadBalancer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Gracefully switch to a new policy defined by the given factory, if the given factory isn't
 * equal to the current one.
 */
public void switchTo(LoadBalancer.Factory newBalancerFactory) {
  checkNotNull(newBalancerFactory, "newBalancerFactory");

  if (newBalancerFactory.equals(pendingBalancerFactory)) {
    return;
  }
  pendingLb.shutdown();
  pendingLb = defaultBalancer;
  pendingBalancerFactory = null;
  pendingState = ConnectivityState.CONNECTING;
  pendingPicker = BUFFER_PICKER;

  if (newBalancerFactory.equals(currentBalancerFactory)) {
    return;
  }

  class PendingHelper extends ForwardingLoadBalancerHelper {
    LoadBalancer lb;

    @Override
    protected Helper delegate() {
      return helper;
    }

    @Override
    public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
      if (lb == pendingLb) {
        checkState(currentLbIsReady, "there's pending lb while current lb has been out of READY");
        pendingState = newState;
        pendingPicker = newPicker;
        if (newState == ConnectivityState.READY) {
          swap();
        }
      } else if (lb == currentLb) {
        currentLbIsReady = newState == ConnectivityState.READY;
        if (!currentLbIsReady && pendingLb != defaultBalancer) {
          swap(); // current policy exits READY, so swap
        } else {
          helper.updateBalancingState(newState, newPicker);
        }
      }
    }
  }

  PendingHelper pendingHelper = new PendingHelper();
  pendingHelper.lb = newBalancerFactory.newLoadBalancer(pendingHelper);
  pendingLb = pendingHelper.lb;
  pendingBalancerFactory = newBalancerFactory;
  if (!currentLbIsReady) {
    swap(); // the old policy is not READY at the moment, so swap to the new one right now
  }
}