Search in sources :

Example 1 with RingHashLbConfig

use of io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig in project grpc-java by grpc.

the class ClientXdsClient method processCluster.

@VisibleForTesting
static CdsUpdate processCluster(Cluster cluster, Set<String> retainedEdsResources, Set<String> certProviderInstances, ServerInfo serverInfo) throws ResourceInvalidException {
    StructOrError<CdsUpdate.Builder> structOrError;
    switch(cluster.getClusterDiscoveryTypeCase()) {
        case TYPE:
            structOrError = parseNonAggregateCluster(cluster, retainedEdsResources, certProviderInstances, serverInfo);
            break;
        case CLUSTER_TYPE:
            structOrError = parseAggregateCluster(cluster);
            break;
        case CLUSTERDISCOVERYTYPE_NOT_SET:
        default:
            throw new ResourceInvalidException("Cluster " + cluster.getName() + ": unspecified cluster discovery type");
    }
    if (structOrError.getErrorDetail() != null) {
        throw new ResourceInvalidException(structOrError.getErrorDetail());
    }
    CdsUpdate.Builder updateBuilder = structOrError.getStruct();
    if (cluster.getLbPolicy() == LbPolicy.RING_HASH) {
        RingHashLbConfig lbConfig = cluster.getRingHashLbConfig();
        long minRingSize = lbConfig.hasMinimumRingSize() ? lbConfig.getMinimumRingSize().getValue() : DEFAULT_RING_HASH_LB_POLICY_MIN_RING_SIZE;
        long maxRingSize = lbConfig.hasMaximumRingSize() ? lbConfig.getMaximumRingSize().getValue() : DEFAULT_RING_HASH_LB_POLICY_MAX_RING_SIZE;
        if (lbConfig.getHashFunction() != RingHashLbConfig.HashFunction.XX_HASH || minRingSize > maxRingSize || maxRingSize > MAX_RING_HASH_LB_POLICY_RING_SIZE) {
            throw new ResourceInvalidException("Cluster " + cluster.getName() + ": invalid ring_hash_lb_config: " + lbConfig);
        }
        updateBuilder.ringHashLbPolicy(minRingSize, maxRingSize);
    } else if (cluster.getLbPolicy() == LbPolicy.ROUND_ROBIN) {
        updateBuilder.roundRobinLbPolicy();
    } else if (enableLeastRequest && cluster.getLbPolicy() == LbPolicy.LEAST_REQUEST) {
        LeastRequestLbConfig lbConfig = cluster.getLeastRequestLbConfig();
        int choiceCount = lbConfig.hasChoiceCount() ? lbConfig.getChoiceCount().getValue() : DEFAULT_LEAST_REQUEST_CHOICE_COUNT;
        if (choiceCount < DEFAULT_LEAST_REQUEST_CHOICE_COUNT) {
            throw new ResourceInvalidException("Cluster " + cluster.getName() + ": invalid least_request_lb_config: " + lbConfig);
        }
        updateBuilder.leastRequestLbPolicy(choiceCount);
    } else {
        throw new ResourceInvalidException("Cluster " + cluster.getName() + ": unsupported lb policy: " + cluster.getLbPolicy());
    }
    return updateBuilder.build();
}
Also used : LeastRequestLbConfig(io.envoyproxy.envoy.config.cluster.v3.Cluster.LeastRequestLbConfig) RingHashLbConfig(io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig) ServerInterceptorBuilder(io.grpc.xds.Filter.ServerInterceptorBuilder) ClientInterceptorBuilder(io.grpc.xds.Filter.ClientInterceptorBuilder) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LeastRequestLbConfig (io.envoyproxy.envoy.config.cluster.v3.Cluster.LeastRequestLbConfig)1 RingHashLbConfig (io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig)1 LbEndpoint (io.grpc.xds.Endpoints.LbEndpoint)1 ClientInterceptorBuilder (io.grpc.xds.Filter.ClientInterceptorBuilder)1 ServerInterceptorBuilder (io.grpc.xds.Filter.ServerInterceptorBuilder)1