Skip to content

Commit c4bfaa5

Browse files
committed
Replace clusters array with single cluster in ClusterResolverLB
1 parent 3867cc7 commit c4bfaa5

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ public LoadBalancer newLoadBalancer(Helper helper) {
168168
*/
169169
private final class ClusterResolverLbState extends LoadBalancer {
170170
private final Helper helper;
171-
private final List<String> clusters = new ArrayList<>();
172171
private final Map<String, ClusterState> clusterStates = new HashMap<>();
172+
private String cluster;
173173
private Object endpointLbConfig;
174174
private ResolvedAddresses resolvedAddresses;
175175
private LoadBalancer childLb;
@@ -186,7 +186,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
186186
(ClusterResolverConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
187187
endpointLbConfig = config.lbConfig;
188188
DiscoveryMechanism instance = config.discoveryMechanism;
189-
clusters.add(instance.cluster);
189+
cluster = instance.cluster;
190190
ClusterState state;
191191
if (instance.type == DiscoveryMechanism.Type.EDS) {
192192
state = new EdsClusterState(instance.cluster, instance.edsServiceName,
@@ -228,24 +228,22 @@ private void handleEndpointResourceUpdate() {
228228
List<String> priorities = new ArrayList<>(); // totally ordered priority list
229229

230230
Status endpointNotFound = Status.OK;
231-
for (String cluster : clusters) {
232-
ClusterState state = clusterStates.get(cluster);
233-
// Propagate endpoints to the child LB policy only after all clusters have been resolved.
234-
if (!state.resolved && state.status.isOk()) {
235-
return;
236-
}
237-
if (state.result != null) {
238-
addresses.addAll(state.result.addresses);
239-
priorityChildConfigs.putAll(state.result.priorityChildConfigs);
240-
priorities.addAll(state.result.priorities);
241-
} else {
242-
endpointNotFound = state.status;
243-
}
231+
ClusterState state = clusterStates.get(cluster);
232+
// Propagate endpoints to the child LB policy only after all clusters have been resolved.
233+
if (!state.resolved && state.status.isOk()) {
234+
return;
235+
}
236+
if (state.result != null) {
237+
addresses.addAll(state.result.addresses);
238+
priorityChildConfigs.putAll(state.result.priorityChildConfigs);
239+
priorities.addAll(state.result.priorities);
240+
} else {
241+
endpointNotFound = state.status;
244242
}
245243
if (addresses.isEmpty()) {
246244
if (endpointNotFound.isOk()) {
247245
endpointNotFound = Status.UNAVAILABLE.withDescription(
248-
"No usable endpoint from cluster(s): " + clusters);
246+
"No usable endpoint from cluster(s): " + cluster);
249247
} else {
250248
endpointNotFound =
251249
Status.UNAVAILABLE.withCause(endpointNotFound.getCause())
@@ -273,22 +271,13 @@ private void handleEndpointResourceUpdate() {
273271
}
274272

275273
private void handleEndpointResolutionError() {
276-
boolean allInError = true;
277-
Status error = null;
278-
for (String cluster : clusters) {
279-
ClusterState state = clusterStates.get(cluster);
280-
if (state.status.isOk()) {
281-
allInError = false;
282-
} else {
283-
error = state.status;
284-
}
285-
}
286-
if (allInError) {
274+
ClusterState state = clusterStates.get(cluster);
275+
if (!state.status.isOk()) {
287276
if (childLb != null) {
288-
childLb.handleNameResolutionError(error);
277+
childLb.handleNameResolutionError(state.status);
289278
} else {
290279
helper.updateBalancingState(
291-
TRANSIENT_FAILURE, new FixedResultPicker(PickResult.withError(error)));
280+
TRANSIENT_FAILURE, new FixedResultPicker(PickResult.withError(state.status)));
292281
}
293282
}
294283
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ public void onlyEdsClusters_resourceNeverExist_returnErrorPicker() {
661661
assertPicker(
662662
pickerCaptor.getValue(),
663663
Status.UNAVAILABLE.withDescription(
664-
"No usable endpoint from cluster(s): " + Arrays.asList(CLUSTER1)),
664+
"No usable endpoint from cluster(s): " + CLUSTER1),
665665
null);
666666
}
667667

@@ -690,7 +690,7 @@ public void edsCluster_resourcesRevoked_shutDownChildLbPolicy() {
690690
verify(helper).updateBalancingState(
691691
eq(ConnectivityState.TRANSIENT_FAILURE), pickerCaptor.capture());
692692
Status expectedError = Status.UNAVAILABLE.withDescription(
693-
"No usable endpoint from cluster(s): " + Arrays.asList(CLUSTER1));
693+
"No usable endpoint from cluster(s): " + CLUSTER1);
694694
assertPicker(pickerCaptor.getValue(), expectedError, null);
695695
}
696696

@@ -789,7 +789,7 @@ public void handleEdsResource_noHealthyEndpoint() {
789789
assertPicker(
790790
pickerCaptor.getValue(),
791791
Status.UNAVAILABLE.withDescription(
792-
"No usable endpoint from cluster(s): " + Collections.singleton(CLUSTER1)),
792+
"No usable endpoint from cluster(s): " + CLUSTER1),
793793
null);
794794
}
795795

0 commit comments

Comments
 (0)