Skip to content

Commit cfecc47

Browse files
committed
Focus MultiChildLB updates around ResolvedAddresses of children
This makes ClusterManagerLB more straight-forward, focusing on just the things that are relevant to it, and it avoids specialized map key handling in updateChildrenWithResolvedAddresses().
1 parent 4cb6465 commit cfecc47

File tree

2 files changed

+38
-85
lines changed

2 files changed

+38
-85
lines changed

util/src/main/java/io/grpc/util/MultiChildLoadBalancer.java

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.grpc.util;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120
import static io.grpc.ConnectivityState.CONNECTING;
2221
import static io.grpc.ConnectivityState.IDLE;
@@ -80,20 +79,20 @@ protected MultiChildLoadBalancer(Helper helper) {
8079

8180
/**
8281
* Override to utilize parsing of the policy configuration or alternative helper/lb generation.
82+
* Override this if keys are not Endpoints or if child policies have configuration.
8383
*/
84-
protected Map<Object, ChildLbState> createChildLbMap(ResolvedAddresses resolvedAddresses) {
85-
Map<Object, ChildLbState> childLbMap = new HashMap<>();
86-
List<EquivalentAddressGroup> addresses = resolvedAddresses.getAddresses();
87-
for (EquivalentAddressGroup eag : addresses) {
88-
Endpoint endpoint = new Endpoint(eag); // keys need to be just addresses
89-
ChildLbState existingChildLbState = childLbStates.get(endpoint);
90-
if (existingChildLbState != null) {
91-
childLbMap.put(endpoint, existingChildLbState);
92-
} else {
93-
childLbMap.put(endpoint, createChildLbState(endpoint));
94-
}
95-
}
96-
return childLbMap;
84+
protected Map<Object, ResolvedAddresses> createChildAddressesMap(
85+
ResolvedAddresses resolvedAddresses) {
86+
Map<Object, ResolvedAddresses> childAddresses = new HashMap<>();
87+
for (EquivalentAddressGroup eag : resolvedAddresses.getAddresses()) {
88+
ResolvedAddresses addresses = resolvedAddresses.toBuilder()
89+
.setAddresses(Collections.singletonList(eag))
90+
.setAttributes(Attributes.newBuilder().set(IS_PETIOLE_POLICY, true).build())
91+
.setLoadBalancingPolicyConfig(null)
92+
.build();
93+
childAddresses.put(new Endpoint(eag), addresses);
94+
}
95+
return childAddresses;
9796
}
9897

9998
/**
@@ -128,39 +127,6 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
128127
}
129128
}
130129

131-
/**
132-
* Override this if your keys are not of type Endpoint.
133-
* @param key Key to identify the ChildLbState
134-
* @param resolvedAddresses list of addresses which include attributes
135-
* @return a fully loaded ResolvedAddresses object for the specified key
136-
*/
137-
protected ResolvedAddresses getChildAddresses(Object key, ResolvedAddresses resolvedAddresses) {
138-
Endpoint endpointKey;
139-
if (key instanceof EquivalentAddressGroup) {
140-
endpointKey = new Endpoint((EquivalentAddressGroup) key);
141-
} else {
142-
checkArgument(key instanceof Endpoint, "key is wrong type");
143-
endpointKey = (Endpoint) key;
144-
}
145-
146-
// Retrieve the non-stripped version
147-
EquivalentAddressGroup eagToUse = null;
148-
for (EquivalentAddressGroup currEag : resolvedAddresses.getAddresses()) {
149-
if (endpointKey.equals(new Endpoint(currEag))) {
150-
eagToUse = currEag;
151-
break;
152-
}
153-
}
154-
155-
checkNotNull(eagToUse, key + " no longer present in load balancer children");
156-
157-
return resolvedAddresses.toBuilder()
158-
.setAddresses(Collections.singletonList(eagToUse))
159-
.setAttributes(Attributes.newBuilder().set(IS_PETIOLE_POLICY, true).build())
160-
.setLoadBalancingPolicyConfig(null)
161-
.build();
162-
}
163-
164130
/**
165131
* Handle the name resolution error.
166132
*
@@ -192,41 +158,31 @@ protected final AcceptResolvedAddrRetVal acceptResolvedAddressesInternal(
192158
ResolvedAddresses resolvedAddresses) {
193159
logger.log(Level.FINE, "Received resolution result: {0}", resolvedAddresses);
194160

195-
// Subclass handles any special manipulation to create appropriate types of keyed ChildLbStates
196-
Map<Object, ChildLbState> newChildren = createChildLbMap(resolvedAddresses);
161+
Map<Object, ResolvedAddresses> newChildAddresses = createChildAddressesMap(resolvedAddresses);
197162

198163
// Handle error case
199-
if (newChildren.isEmpty()) {
164+
if (newChildAddresses.isEmpty()) {
200165
Status unavailableStatus = Status.UNAVAILABLE.withDescription(
201166
"NameResolver returned no usable address. " + resolvedAddresses);
202167
handleNameResolutionError(unavailableStatus);
203168
return new AcceptResolvedAddrRetVal(unavailableStatus, null);
204169
}
205170

206-
addMissingChildren(newChildren);
171+
updateChildrenWithResolvedAddresses(newChildAddresses);
207172

208-
updateChildrenWithResolvedAddresses(resolvedAddresses, newChildren);
209-
210-
return new AcceptResolvedAddrRetVal(Status.OK, getRemovedChildren(newChildren.keySet()));
173+
return new AcceptResolvedAddrRetVal(Status.OK, getRemovedChildren(newChildAddresses.keySet()));
211174
}
212175

213-
private void addMissingChildren(Map<Object, ChildLbState> newChildren) {
214-
// Do adds and identify reused children
215-
for (Map.Entry<Object, ChildLbState> entry : newChildren.entrySet()) {
216-
final Object key = entry.getKey();
217-
if (!childLbStates.containsKey(key)) {
218-
childLbStates.put(key, entry.getValue());
219-
}
220-
}
221-
}
222-
223-
private void updateChildrenWithResolvedAddresses(ResolvedAddresses resolvedAddresses,
224-
Map<Object, ChildLbState> newChildren) {
225-
for (Map.Entry<Object, ChildLbState> entry : newChildren.entrySet()) {
176+
private void updateChildrenWithResolvedAddresses(
177+
Map<Object, ResolvedAddresses> newChildAddresses) {
178+
for (Map.Entry<Object, ResolvedAddresses> entry : newChildAddresses.entrySet()) {
226179
ChildLbState childLbState = childLbStates.get(entry.getKey());
227-
ResolvedAddresses childAddresses = getChildAddresses(entry.getKey(), resolvedAddresses);
228-
childLbState.setResolvedAddresses(childAddresses); // update child
229-
childLbState.lb.handleResolvedAddresses(childAddresses); // update child LB
180+
if (childLbState == null) {
181+
childLbState = createChildLbState(entry.getKey());
182+
childLbStates.put(entry.getKey(), childLbState);
183+
}
184+
childLbState.setResolvedAddresses(entry.getValue()); // update child
185+
childLbState.lb.handleResolvedAddresses(entry.getValue()); // update child LB
230186
}
231187
}
232188

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,28 @@ class ClusterManagerLoadBalancer extends MultiChildLoadBalancer {
7070
}
7171

7272
@Override
73-
protected ResolvedAddresses getChildAddresses(Object key, ResolvedAddresses resolvedAddresses) {
74-
ClusterManagerConfig config = (ClusterManagerConfig)
75-
resolvedAddresses.getLoadBalancingPolicyConfig();
76-
Object childConfig = config.childPolicies.get(key);
77-
return resolvedAddresses.toBuilder().setLoadBalancingPolicyConfig(childConfig).build();
73+
protected ChildLbState createChildLbState(Object key) {
74+
return new ClusterManagerLbState(key, GracefulSwitchLoadBalancerFactory.INSTANCE);
7875
}
7976

8077
@Override
81-
protected Map<Object, ChildLbState> createChildLbMap(ResolvedAddresses resolvedAddresses) {
78+
protected Map<Object, ResolvedAddresses> createChildAddressesMap(
79+
ResolvedAddresses resolvedAddresses) {
8280
ClusterManagerConfig config = (ClusterManagerConfig)
8381
resolvedAddresses.getLoadBalancingPolicyConfig();
84-
Map<Object, ChildLbState> newChildPolicies = new HashMap<>();
82+
Map<Object, ResolvedAddresses> childAddresses = new HashMap<>();
8583
if (config != null) {
86-
for (String key : config.childPolicies.keySet()) {
87-
ChildLbState child = getChildLbState(key);
88-
if (child == null) {
89-
child = new ClusterManagerLbState(key, GracefulSwitchLoadBalancerFactory.INSTANCE);
90-
}
91-
newChildPolicies.put(key, child);
84+
for (Map.Entry<String, Object> childPolicy : config.childPolicies.entrySet()) {
85+
ResolvedAddresses addresses = resolvedAddresses.toBuilder()
86+
.setLoadBalancingPolicyConfig(childPolicy.getValue())
87+
.build();
88+
childAddresses.put(childPolicy.getKey(), addresses);
9289
}
9390
}
9491
logger.log(
9592
XdsLogLevel.INFO,
96-
"Received cluster_manager lb config: child names={0}", newChildPolicies.keySet());
97-
return newChildPolicies;
93+
"Received cluster_manager lb config: child names={0}", childAddresses.keySet());
94+
return childAddresses;
9895
}
9996

10097
/**

0 commit comments

Comments
 (0)