32
32
import io .grpc .ClientStreamTracer ;
33
33
import io .grpc .ClientStreamTracer .StreamInfo ;
34
34
import io .grpc .ConnectivityState ;
35
+ import io .grpc .EquivalentAddressGroup ;
35
36
import io .grpc .LoadBalancer ;
36
37
import io .grpc .LoadBalancerProvider ;
37
38
import io .grpc .Metadata ;
@@ -153,28 +154,37 @@ private static AtomicInteger getInFlights(ChildLbState childLbState) {
153
154
154
155
@ VisibleForTesting
155
156
static final class ReadyPicker extends SubchannelPicker {
156
- private final List <ChildLbState > childLbStates ; // non-empty
157
+ private final List <SubchannelPicker > childPickers ; // non-empty
158
+ private final List <AtomicInteger > childInFlights ; // 1:1 with childPickers
159
+ private final List <EquivalentAddressGroup > childEags ; // 1:1 with childPickers
157
160
private final int choiceCount ;
158
161
private final ThreadSafeRandom random ;
159
162
private final int hashCode ;
160
163
161
164
ReadyPicker (List <ChildLbState > childLbStates , int choiceCount , ThreadSafeRandom random ) {
162
165
checkArgument (!childLbStates .isEmpty (), "empty list" );
163
- this .childLbStates = childLbStates ;
166
+ this .childPickers = new ArrayList <>(childLbStates .size ());
167
+ this .childInFlights = new ArrayList <>(childLbStates .size ());
168
+ this .childEags = new ArrayList <>(childLbStates .size ());
169
+ for (ChildLbState state : childLbStates ) {
170
+ childPickers .add (state .getCurrentPicker ());
171
+ childInFlights .add (getInFlights (state ));
172
+ childEags .add (state .getEag ());
173
+ }
164
174
this .choiceCount = choiceCount ;
165
175
this .random = checkNotNull (random , "random" );
166
176
167
177
int sum = 0 ;
168
- for (ChildLbState child : childLbStates ) {
178
+ for (SubchannelPicker child : childPickers ) {
169
179
sum += child .hashCode ();
170
180
}
171
181
this .hashCode = sum ^ choiceCount ;
172
182
}
173
183
174
184
@ Override
175
185
public PickResult pickSubchannel (PickSubchannelArgs args ) {
176
- final ChildLbState childLbState = nextChildToUse ();
177
- PickResult childResult = childLbState . getCurrentPicker ( ).pickSubchannel (args );
186
+ int child = nextChildToUse ();
187
+ PickResult childResult = childPickers . get ( child ).pickSubchannel (args );
178
188
179
189
if (!childResult .getStatus ().isOk () || childResult .getSubchannel () == null ) {
180
190
return childResult ;
@@ -186,33 +196,38 @@ public PickResult pickSubchannel(PickSubchannelArgs args) {
186
196
} else {
187
197
// Wrap the subchannel
188
198
OutstandingRequestsTracingFactory factory =
189
- new OutstandingRequestsTracingFactory (getInFlights ( childLbState ));
199
+ new OutstandingRequestsTracingFactory (childInFlights . get ( child ));
190
200
return PickResult .withSubchannel (childResult .getSubchannel (), factory );
191
201
}
192
202
}
193
203
194
204
@ Override
195
205
public String toString () {
196
206
return MoreObjects .toStringHelper (ReadyPicker .class )
197
- .add ("list" , childLbStates )
207
+ .add ("list" , childPickers )
198
208
.add ("choiceCount" , choiceCount )
199
209
.toString ();
200
210
}
201
211
202
- private ChildLbState nextChildToUse () {
203
- ChildLbState candidate = childLbStates . get ( random .nextInt (childLbStates .size () ));
212
+ private int nextChildToUse () {
213
+ int candidate = random .nextInt (childPickers .size ());
204
214
for (int i = 0 ; i < choiceCount - 1 ; ++i ) {
205
- ChildLbState sampled = childLbStates . get ( random .nextInt (childLbStates .size () ));
206
- if (getInFlights (sampled ).get () < getInFlights (candidate ).get ()) {
215
+ int sampled = random .nextInt (childPickers .size ());
216
+ if (childInFlights . get (sampled ).get () < childInFlights . get (candidate ).get ()) {
207
217
candidate = sampled ;
208
218
}
209
219
}
210
220
return candidate ;
211
221
}
212
222
213
223
@ VisibleForTesting
214
- List <ChildLbState > getChildLbStates () {
215
- return childLbStates ;
224
+ List <SubchannelPicker > getChildPickers () {
225
+ return childPickers ;
226
+ }
227
+
228
+ @ VisibleForTesting
229
+ List <EquivalentAddressGroup > getChildEags () {
230
+ return childEags ;
216
231
}
217
232
218
233
@ Override
@@ -232,8 +247,8 @@ public boolean equals(Object o) {
232
247
// the lists cannot contain duplicate children
233
248
return hashCode == other .hashCode
234
249
&& choiceCount == other .choiceCount
235
- && childLbStates .size () == other .childLbStates .size ()
236
- && new HashSet <>(childLbStates ).containsAll (other .childLbStates );
250
+ && childPickers .size () == other .childPickers .size ()
251
+ && new HashSet <>(childPickers ).containsAll (other .childPickers );
237
252
}
238
253
}
239
254
0 commit comments