Skip to content

Commit fe752a2

Browse files
committed
xds: Move specialized APIs out of XdsResourceType
StructOrError is a more generic API, but we have StatusOr now so we don't want new usages of StructOrError. Moving StructOrError out of io.grpc.xds.client will make it easier to delete StructOrError once we've migrated to StatusOr in the future. TRANSPORT_SOCKET_NAME_TLS should also move, but it wasn't immediately clear to me where it should go.
1 parent a0982ca commit fe752a2

File tree

5 files changed

+77
-56
lines changed

5 files changed

+77
-56
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.xds;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.annotations.VisibleForTesting;
22+
import javax.annotation.Nullable;
23+
24+
/** An object or a String error. */
25+
final class StructOrError<T> {
26+
27+
/**
28+
* Returns a {@link StructOrError} for the successfully converted data object.
29+
*/
30+
public static <T> StructOrError<T> fromStruct(T struct) {
31+
return new StructOrError<>(struct);
32+
}
33+
34+
/**
35+
* Returns a {@link StructOrError} for the failure to convert the data object.
36+
*/
37+
public static <T> StructOrError<T> fromError(String errorDetail) {
38+
return new StructOrError<>(errorDetail);
39+
}
40+
41+
private final String errorDetail;
42+
private final T struct;
43+
44+
private StructOrError(T struct) {
45+
this.struct = checkNotNull(struct, "struct");
46+
this.errorDetail = null;
47+
}
48+
49+
private StructOrError(String errorDetail) {
50+
this.struct = null;
51+
this.errorDetail = checkNotNull(errorDetail, "errorDetail");
52+
}
53+
54+
/**
55+
* Returns struct if exists, otherwise null.
56+
*/
57+
@VisibleForTesting
58+
@Nullable
59+
public T getStruct() {
60+
return struct;
61+
}
62+
63+
/**
64+
* Returns error detail if exists, otherwise null.
65+
*/
66+
@VisibleForTesting
67+
@Nullable
68+
public String getErrorDetail() {
69+
return errorDetail;
70+
}
71+
}
72+

xds/src/main/java/io/grpc/xds/XdsClusterResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class XdsClusterResource extends XdsResourceType<CdsUpdate> {
6767
static final String AGGREGATE_CLUSTER_TYPE_NAME = "envoy.clusters.aggregate";
6868
static final String ADS_TYPE_URL_CDS =
6969
"type.googleapis.com/envoy.config.cluster.v3.Cluster";
70+
private static final String TYPE_URL_CLUSTER_CONFIG =
71+
"type.googleapis.com/envoy.extensions.clusters.aggregate.v3.ClusterConfig";
7072
private static final String TYPE_URL_UPSTREAM_TLS_CONTEXT =
7173
"type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext";
7274
private static final String TYPE_URL_UPSTREAM_TLS_CONTEXT_V2 =

xds/src/main/java/io/grpc/xds/XdsRouteConfigureResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class XdsRouteConfigureResource extends XdsResourceType<RdsUpdate> {
7878
"type.googleapis.com/envoy.config.route.v3.RouteConfiguration";
7979
private static final String TYPE_URL_FILTER_CONFIG =
8080
"type.googleapis.com/envoy.config.route.v3.FilterConfig";
81+
@VisibleForTesting
82+
static final String HASH_POLICY_FILTER_STATE_KEY = "io.grpc.channel_id";
8183
// TODO(zdapeng): need to discuss how to handle unsupported values.
8284
private static final Set<Status.Code> SUPPORTED_RETRYABLE_CODES =
8385
Collections.unmodifiableSet(EnumSet.of(

xds/src/main/java/io/grpc/xds/client/XdsResourceType.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static io.grpc.xds.client.XdsClient.canonifyResourceName;
2121
import static io.grpc.xds.client.XdsClient.isResourceNameValid;
2222

23-
import com.google.common.annotations.VisibleForTesting;
2423
import com.google.protobuf.Any;
2524
import com.google.protobuf.InvalidProtocolBufferException;
2625
import com.google.protobuf.Message;
@@ -41,11 +40,7 @@ public abstract class XdsResourceType<T extends ResourceUpdate> {
4140
static final String TYPE_URL_RESOURCE =
4241
"type.googleapis.com/envoy.service.discovery.v3.Resource";
4342
protected static final String TRANSPORT_SOCKET_NAME_TLS = "envoy.transport_sockets.tls";
44-
@VisibleForTesting
45-
public static final String HASH_POLICY_FILTER_STATE_KEY = "io.grpc.channel_id";
4643

47-
protected static final String TYPE_URL_CLUSTER_CONFIG =
48-
"type.googleapis.com/envoy.extensions.clusters.aggregate.v3.ClusterConfig";
4944
protected static final String TYPE_URL_TYPED_STRUCT_UDPA =
5045
"type.googleapis.com/udpa.type.v1.TypedStruct";
5146
protected static final String TYPE_URL_TYPED_STRUCT =
@@ -249,53 +244,4 @@ public ValidatedResourceUpdate(Map<String, ParsedResource<T>> parsedResources,
249244
this.errors = errors;
250245
}
251246
}
252-
253-
@VisibleForTesting
254-
public static final class StructOrError<T> {
255-
256-
/**
257-
* Returns a {@link StructOrError} for the successfully converted data object.
258-
*/
259-
public static <T> StructOrError<T> fromStruct(T struct) {
260-
return new StructOrError<>(struct);
261-
}
262-
263-
/**
264-
* Returns a {@link StructOrError} for the failure to convert the data object.
265-
*/
266-
public static <T> StructOrError<T> fromError(String errorDetail) {
267-
return new StructOrError<>(errorDetail);
268-
}
269-
270-
private final String errorDetail;
271-
private final T struct;
272-
273-
private StructOrError(T struct) {
274-
this.struct = checkNotNull(struct, "struct");
275-
this.errorDetail = null;
276-
}
277-
278-
private StructOrError(String errorDetail) {
279-
this.struct = null;
280-
this.errorDetail = checkNotNull(errorDetail, "errorDetail");
281-
}
282-
283-
/**
284-
* Returns struct if exists, otherwise null.
285-
*/
286-
@VisibleForTesting
287-
@Nullable
288-
public T getStruct() {
289-
return struct;
290-
}
291-
292-
/**
293-
* Returns error detail if exists, otherwise null.
294-
*/
295-
@VisibleForTesting
296-
@Nullable
297-
public String getErrorDetail() {
298-
return errorDetail;
299-
}
300-
}
301247
}

xds/src/test/java/io/grpc/xds/GrpcXdsClientImplDataTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140
import io.grpc.xds.client.XdsClient;
141141
import io.grpc.xds.client.XdsResourceType;
142142
import io.grpc.xds.client.XdsResourceType.ResourceInvalidException;
143-
import io.grpc.xds.client.XdsResourceType.StructOrError;
144143
import io.grpc.xds.internal.Matchers;
145144
import io.grpc.xds.internal.Matchers.FractionMatcher;
146145
import io.grpc.xds.internal.Matchers.HeaderMatcher;
@@ -939,7 +938,7 @@ public void parseRouteAction_withHashPolicies() {
939938
io.envoyproxy.envoy.config.route.v3.RouteAction.HashPolicy.newBuilder()
940939
.setFilterState(
941940
FilterState.newBuilder()
942-
.setKey(XdsResourceType.HASH_POLICY_FILTER_STATE_KEY)))
941+
.setKey(XdsRouteConfigureResource.HASH_POLICY_FILTER_STATE_KEY)))
943942
.addHashPolicy(
944943
io.envoyproxy.envoy.config.route.v3.RouteAction.HashPolicy.newBuilder()
945944
.setQueryParameter(

0 commit comments

Comments
 (0)