Skip to content

Commit 2faa080

Browse files
committed
Better localityWeights handling.
1 parent 8a02214 commit 2faa080

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ private final class ClusterResolverLbState extends LoadBalancer {
152152
private final Helper helper;
153153
private final List<String> clusters = new ArrayList<>();
154154
private final Map<String, ClusterState> clusterStates = new HashMap<>();
155-
private final Map<Locality, Integer> localityWeights = new HashMap<>();
156155
private PolicySelection endpointLbPolicy;
157156
private ResolvedAddresses resolvedAddresses;
158157
private LoadBalancer childLb;
@@ -206,6 +205,8 @@ private void handleEndpointResourceUpdate() {
206205
List<EquivalentAddressGroup> addresses = new ArrayList<>();
207206
Map<String, PriorityChildConfig> priorityChildConfigs = new HashMap<>();
208207
List<String> priorities = new ArrayList<>(); // totally ordered priority list
208+
Map<Locality, Integer> localityWeights = new HashMap<>();
209+
209210
Status endpointNotFound = Status.OK;
210211
for (String cluster : clusters) {
211212
ClusterState state = clusterStates.get(cluster);
@@ -217,6 +218,7 @@ private void handleEndpointResourceUpdate() {
217218
addresses.addAll(state.result.addresses);
218219
priorityChildConfigs.putAll(state.result.priorityChildConfigs);
219220
priorities.addAll(state.result.priorities);
221+
localityWeights.putAll(state.result.localityWeights);
220222
} else {
221223
endpointNotFound = state.status;
222224
}
@@ -248,7 +250,8 @@ private void handleEndpointResourceUpdate() {
248250
.setLoadBalancingPolicyConfig(childConfig)
249251
.setAddresses(Collections.unmodifiableList(addresses))
250252
.setAttributes(resolvedAddresses.getAttributes().toBuilder()
251-
.set(InternalXdsAttributes.ATTR_LOCALITY_WEIGHTS, localityWeights).build())
253+
.set(InternalXdsAttributes.ATTR_LOCALITY_WEIGHTS,
254+
Collections.unmodifiableMap(localityWeights)).build())
252255
.build());
253256
}
254257

@@ -316,8 +319,8 @@ private abstract class ClusterState {
316319
// True if has received resolution result.
317320
protected boolean resolved;
318321
// Most recently resolved addresses and config, or null if resource not exists.
319-
@Nullable
320322
protected ClusterResolutionResult result;
323+
321324
protected boolean shutdown;
322325

323326
private ClusterState(String name, @Nullable ServerInfo lrsServerInfo,
@@ -377,6 +380,7 @@ public void run() {
377380
}
378381
Map<Locality, LocalityLbEndpoints> localityLbEndpoints =
379382
update.localityLbEndpointsMap;
383+
Map<Locality, Integer> localityWeights = new HashMap<>();
380384
List<DropOverload> dropOverloads = update.dropPolicies;
381385
List<EquivalentAddressGroup> addresses = new ArrayList<>();
382386
Map<String, Map<Locality, Integer>> prioritizedLocalityWeights = new HashMap<>();
@@ -429,7 +433,8 @@ public void run() {
429433
endpointLbPolicy, lbRegistry, prioritizedLocalityWeights, dropOverloads);
430434
status = Status.OK;
431435
resolved = true;
432-
result = new ClusterResolutionResult(addresses, priorityChildConfigs, priorities);
436+
result = new ClusterResolutionResult(addresses, priorityChildConfigs, priorities,
437+
localityWeights);
433438
handleEndpointResourceUpdate();
434439
}
435440
}
@@ -635,18 +640,23 @@ private static class ClusterResolutionResult {
635640
private final Map<String, PriorityChildConfig> priorityChildConfigs;
636641
// List of priority names ordered in descending priorities.
637642
private final List<String> priorities;
643+
// Most recent view on how localities in the cluster should be wighted. Only set for EDS
644+
// clusters that support the concept.
645+
private final Map<Locality, Integer> localityWeights;
638646

639647
ClusterResolutionResult(List<EquivalentAddressGroup> addresses, String priority,
640648
PriorityChildConfig config) {
641649
this(addresses, Collections.singletonMap(priority, config),
642-
Collections.singletonList(priority));
650+
Collections.singletonList(priority), Collections.emptyMap());
643651
}
644652

645653
ClusterResolutionResult(List<EquivalentAddressGroup> addresses,
646-
Map<String, PriorityChildConfig> configs, List<String> priorities) {
654+
Map<String, PriorityChildConfig> configs, List<String> priorities,
655+
Map<Locality, Integer> localityWeights) {
647656
this.addresses = addresses;
648657
this.priorityChildConfigs = configs;
649658
this.priorities = priorities;
659+
this.localityWeights = localityWeights;
650660
}
651661
}
652662

0 commit comments

Comments
 (0)