Search in sources :

Example 1 with Cluster

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

the class ClientXdsClient method parseAggregateCluster.

private static StructOrError<CdsUpdate.Builder> parseAggregateCluster(Cluster cluster) {
    String clusterName = cluster.getName();
    CustomClusterType customType = cluster.getClusterType();
    String typeName = customType.getName();
    if (!typeName.equals(AGGREGATE_CLUSTER_TYPE_NAME)) {
        return StructOrError.fromError("Cluster " + clusterName + ": unsupported custom cluster type: " + typeName);
    }
    io.envoyproxy.envoy.extensions.clusters.aggregate.v3.ClusterConfig clusterConfig;
    try {
        clusterConfig = unpackCompatibleType(customType.getTypedConfig(), io.envoyproxy.envoy.extensions.clusters.aggregate.v3.ClusterConfig.class, TYPE_URL_CLUSTER_CONFIG, TYPE_URL_CLUSTER_CONFIG_V2);
    } catch (InvalidProtocolBufferException e) {
        return StructOrError.fromError("Cluster " + clusterName + ": malformed ClusterConfig: " + e);
    }
    return StructOrError.fromStruct(CdsUpdate.forAggregate(clusterName, clusterConfig.getClustersList()));
}
Also used : CustomClusterType(io.envoyproxy.envoy.config.cluster.v3.Cluster.CustomClusterType) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 2 with Cluster

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

the class ClientXdsClient method handleCdsResponse.

@Override
public void handleCdsResponse(ServerInfo serverInfo, String versionInfo, List<Any> resources, String nonce) {
    syncContext.throwIfNotInThisSynchronizationContext();
    Map<String, ParsedResource> parsedResources = new HashMap<>(resources.size());
    Set<String> unpackedResources = new HashSet<>(resources.size());
    Set<String> invalidResources = new HashSet<>();
    List<String> errors = new ArrayList<>();
    Set<String> retainedEdsResources = new HashSet<>();
    for (int i = 0; i < resources.size(); i++) {
        Any resource = resources.get(i);
        // Unpack the Cluster.
        Cluster cluster;
        try {
            cluster = unpackCompatibleType(resource, Cluster.class, ResourceType.CDS.typeUrl(), ResourceType.CDS.typeUrlV2());
        } catch (InvalidProtocolBufferException e) {
            errors.add("CDS response Resource index " + i + " - can't decode Cluster: " + e);
            continue;
        }
        if (!isResourceNameValid(cluster.getName(), resource.getTypeUrl())) {
            errors.add("Unsupported resource name: " + cluster.getName() + " for type: " + ResourceType.CDS);
            continue;
        }
        String clusterName = canonifyResourceName(cluster.getName());
        // unrequested resources.
        if (!cdsResourceSubscribers.containsKey(clusterName)) {
            continue;
        }
        unpackedResources.add(clusterName);
        // Process Cluster into CdsUpdate.
        CdsUpdate cdsUpdate;
        try {
            Set<String> certProviderInstances = null;
            if (getBootstrapInfo() != null && getBootstrapInfo().certProviders() != null) {
                certProviderInstances = getBootstrapInfo().certProviders().keySet();
            }
            cdsUpdate = processCluster(cluster, retainedEdsResources, certProviderInstances, serverInfo);
        } catch (ResourceInvalidException e) {
            errors.add("CDS response Cluster '" + clusterName + "' validation error: " + e.getMessage());
            invalidResources.add(clusterName);
            continue;
        }
        parsedResources.put(clusterName, new ParsedResource(cdsUpdate, resource));
    }
    logger.log(XdsLogLevel.INFO, "Received CDS Response version {0} nonce {1}. Parsed resources: {2}", versionInfo, nonce, unpackedResources);
    handleResourceUpdate(serverInfo, ResourceType.CDS, parsedResources, invalidResources, retainedEdsResources, versionInfo, nonce, errors);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Cluster(io.envoyproxy.envoy.config.cluster.v3.Cluster) Any(com.google.protobuf.Any) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) HashSet(java.util.HashSet)

Example 3 with Cluster

use of io.envoyproxy.envoy.config.cluster.v3.Cluster 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)

Example 4 with Cluster

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

the class ClientXdsClientDataTest method parseCluster_validateEdsSourceConfig.

@Test
public void parseCluster_validateEdsSourceConfig() throws ResourceInvalidException {
    Set<String> retainedEdsResources = new HashSet<>();
    Cluster cluster1 = Cluster.newBuilder().setName("cluster-foo.googleapis.com").setType(DiscoveryType.EDS).setEdsClusterConfig(EdsClusterConfig.newBuilder().setEdsConfig(ConfigSource.newBuilder().setAds(AggregatedConfigSource.getDefaultInstance())).setServiceName("service-foo.googleapis.com")).setLbPolicy(LbPolicy.ROUND_ROBIN).build();
    ClientXdsClient.processCluster(cluster1, retainedEdsResources, null, LRS_SERVER_INFO);
    Cluster cluster2 = Cluster.newBuilder().setName("cluster-foo.googleapis.com").setType(DiscoveryType.EDS).setEdsClusterConfig(EdsClusterConfig.newBuilder().setEdsConfig(ConfigSource.newBuilder().setSelf(SelfConfigSource.getDefaultInstance())).setServiceName("service-foo.googleapis.com")).setLbPolicy(LbPolicy.ROUND_ROBIN).build();
    ClientXdsClient.processCluster(cluster2, retainedEdsResources, null, LRS_SERVER_INFO);
    Cluster cluster3 = Cluster.newBuilder().setName("cluster-foo.googleapis.com").setType(DiscoveryType.EDS).setEdsClusterConfig(EdsClusterConfig.newBuilder().setEdsConfig(ConfigSource.newBuilder().setPath("foo-path")).setServiceName("service-foo.googleapis.com")).setLbPolicy(LbPolicy.ROUND_ROBIN).build();
    thrown.expect(ResourceInvalidException.class);
    thrown.expectMessage("Cluster cluster-foo.googleapis.com: field eds_cluster_config must be set to indicate to" + " use EDS over ADS or self ConfigSource");
    ClientXdsClient.processCluster(cluster3, retainedEdsResources, null, LRS_SERVER_INFO);
}
Also used : WeightedCluster(io.envoyproxy.envoy.config.route.v3.WeightedCluster) Cluster(io.envoyproxy.envoy.config.cluster.v3.Cluster) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with Cluster

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

the class ClientXdsClientDataTest method parseCluster_leastRequestLbPolicy_defaultLbConfig.

@Test
public void parseCluster_leastRequestLbPolicy_defaultLbConfig() throws ResourceInvalidException {
    ClientXdsClient.enableLeastRequest = true;
    Cluster cluster = Cluster.newBuilder().setName("cluster-foo.googleapis.com").setType(DiscoveryType.EDS).setEdsClusterConfig(EdsClusterConfig.newBuilder().setEdsConfig(ConfigSource.newBuilder().setAds(AggregatedConfigSource.getDefaultInstance())).setServiceName("service-foo.googleapis.com")).setLbPolicy(LbPolicy.LEAST_REQUEST).build();
    CdsUpdate update = ClientXdsClient.processCluster(cluster, new HashSet<String>(), null, LRS_SERVER_INFO);
    assertThat(update.lbPolicy()).isEqualTo(CdsUpdate.LbPolicy.LEAST_REQUEST);
    assertThat(update.choiceCount()).isEqualTo(ClientXdsClient.DEFAULT_LEAST_REQUEST_CHOICE_COUNT);
}
Also used : WeightedCluster(io.envoyproxy.envoy.config.route.v3.WeightedCluster) Cluster(io.envoyproxy.envoy.config.cluster.v3.Cluster) CdsUpdate(io.grpc.xds.XdsClient.CdsUpdate) Test(org.junit.Test)

Aggregations

Cluster (io.envoyproxy.envoy.config.cluster.v3.Cluster)10 Test (org.junit.Test)8 WeightedCluster (io.envoyproxy.envoy.config.route.v3.WeightedCluster)7 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 LbEndpoint (io.grpc.xds.Endpoints.LbEndpoint)3 CdsUpdate (io.grpc.xds.XdsClient.CdsUpdate)2 HashSet (java.util.HashSet)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Any (com.google.protobuf.Any)1 Thresholds (io.envoyproxy.envoy.config.cluster.v3.CircuitBreakers.Thresholds)1 CustomClusterType (io.envoyproxy.envoy.config.cluster.v3.Cluster.CustomClusterType)1 DiscoveryType (io.envoyproxy.envoy.config.cluster.v3.Cluster.DiscoveryType)1 LeastRequestLbConfig (io.envoyproxy.envoy.config.cluster.v3.Cluster.LeastRequestLbConfig)1 RingHashLbConfig (io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig)1 SocketAddress (io.envoyproxy.envoy.config.core.v3.SocketAddress)1 ClusterLoadAssignment (io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment)1 DiscoveryResponse (io.envoyproxy.envoy.service.discovery.v3.DiscoveryResponse)1 ServerInfo (io.grpc.xds.Bootstrapper.ServerInfo)1 UpstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext)1 ClientInterceptorBuilder (io.grpc.xds.Filter.ClientInterceptorBuilder)1