Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new classes for eliminating xds config tears #11740

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions xds/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
thirdpartyImplementation project(':grpc-protobuf'),
project(':grpc-stub')
compileOnly sourceSets.thirdparty.output
testCompileOnly sourceSets.thirdparty.output
implementation project(':grpc-stub'),
project(':grpc-core'),
project(':grpc-util'),
Expand All @@ -59,6 +60,7 @@ dependencies {
libraries.protobuf.java.util
def nettyDependency = implementation project(':grpc-netty')

testImplementation project(':grpc-api')
testImplementation project(':grpc-rls')
testImplementation project(':grpc-inprocess')
testImplementation testFixtures(project(':grpc-core')),
Expand Down
175 changes: 175 additions & 0 deletions xds/src/main/java/io/grpc/xds/XdsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.xds;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableMap;
import io.grpc.StatusOr;
import io.grpc.xds.XdsClusterResource.CdsUpdate;
import io.grpc.xds.XdsEndpointResource.EdsUpdate;
import io.grpc.xds.XdsListenerResource.LdsUpdate;
import io.grpc.xds.XdsRouteConfigureResource.RdsUpdate;
import java.io.Closeable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents the xDS configuration tree for a specified Listener.
*/
final class XdsConfig {
private final LdsUpdate listener;
private final RdsUpdate route;
private final ImmutableMap<String, StatusOr<XdsClusterConfig>> clusters;
private final int hashCode;

XdsConfig(LdsUpdate listener, RdsUpdate route, Map<String, StatusOr<XdsClusterConfig>> clusters) {
this(listener, route, ImmutableMap.copyOf(clusters));
}

public XdsConfig(LdsUpdate listener, RdsUpdate route, ImmutableMap<String,
StatusOr<XdsClusterConfig>> clusters) {
this.listener = listener;
this.route = route;
this.clusters = clusters;
ejona86 marked this conversation as resolved.
Show resolved Hide resolved

hashCode = Objects.hash(listener, route, clusters);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof XdsConfig)) {
return false;
}

XdsConfig o = (XdsConfig) obj;

return hashCode() == o.hashCode() && Objects.equals(listener, o.listener)
&& Objects.equals(route, o.route) && Objects.equals(clusters, o.clusters);
}

@Override
public int hashCode() {
return hashCode;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("XdsConfig{listener=").append(listener)
.append(", route=").append(route)
.append(", clusters=").append(clusters).append("}");
return builder.toString();
}

public LdsUpdate getListener() {
return listener;
}

public RdsUpdate getRoute() {
return route;
}

public ImmutableMap<String, StatusOr<XdsClusterConfig>> getClusters() {
return clusters;
}

static final class XdsClusterConfig {
private final String clusterName;
private final CdsUpdate clusterResource;
private final StatusOr<EdsUpdate> endpoint; //Will be null for non-EDS clusters

XdsClusterConfig(String clusterName, CdsUpdate clusterResource,
StatusOr<EdsUpdate> endpoint) {
this.clusterName = checkNotNull(clusterName, "clusterName");
this.clusterResource = checkNotNull(clusterResource, "clusterResource");
this.endpoint = endpoint;
}

@Override
public int hashCode() {
int endpointHash = (endpoint != null) ? endpoint.hashCode() : 0;
return clusterName.hashCode() + clusterResource.hashCode() + endpointHash;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof XdsClusterConfig)) {
return false;
}
XdsClusterConfig o = (XdsClusterConfig) obj;
return Objects.equals(clusterName, o.clusterName)
&& Objects.equals(clusterResource, o.clusterResource)
&& Objects.equals(endpoint, o.endpoint);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("XdsClusterConfig{clusterName=").append(clusterName)
.append(", clusterResource=").append(clusterResource)
.append(", endpoint=").append(endpoint).append("}");
return builder.toString();
}

public String getClusterName() {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
return clusterName;
}

public CdsUpdate getClusterResource() {
return clusterResource;
}

public StatusOr<EdsUpdate> getEndpoint() {
return endpoint;
}
}

static final class XdsConfigBuilder {
private LdsUpdate listener;
private RdsUpdate route;
private Map<String, StatusOr<XdsClusterConfig>> clusters = new HashMap<>();

XdsConfigBuilder setListener(LdsUpdate listener) {
this.listener = checkNotNull(listener, "listener");
return this;
}

XdsConfigBuilder setRoute(RdsUpdate route) {
this.route = checkNotNull(route, "route");
return this;
}

XdsConfigBuilder addCluster(String name, StatusOr<XdsClusterConfig> clusterConfig) {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
checkNotNull(name, "name");
checkNotNull(clusterConfig, "clusterConfig");
clusters.put(name, clusterConfig);
return this;
}

XdsConfig build() {
checkNotNull(listener, "listener");
checkNotNull(route, "route");
return new XdsConfig(listener, route, clusters);
}
}

public interface XdsClusterSubscriptionRegistry {
Closeable subscribeToCluster(String clusterName);
}
}
Loading
Loading