• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #20282

18 May 2026 12:47PM UTC coverage: 88.845% (-0.002%) from 88.847%
#20282

push

github

web-flow
Remove JSR-305 ThreadSafe annotation and replace with JavaDoc (#12762)

For non-final public classes and interfaces only, this PR removes JSR-305 ThreadSafe annotations but instead of replacing with ErrorProne's ThreadSafe, sticks to adding a JavaDoc comment. This should basically keep things inline with what JSR-305 ThreadSafe affords.

Adding ErrorProne's ThreadSafe can be considered in the future, as it
expects more things than JSR-305.

Removing the JSR-305 dependency here allows Java applications that have
moved away from javax to compile and avoids a bug in Immutables and
Lombok (and possibly other annotation processors) from failing when
JSR-305 is not present.

36269 of 40823 relevant lines covered (88.84%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.65
/../api/src/main/java/io/grpc/LoadBalancer.java
1
/*
2
 * Copyright 2016 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;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static com.google.common.base.Preconditions.checkNotNull;
21

22
import com.google.common.base.MoreObjects;
23
import com.google.common.base.Objects;
24
import com.google.common.base.Preconditions;
25
import java.util.ArrayList;
26
import java.util.Arrays;
27
import java.util.Collections;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.concurrent.ScheduledExecutorService;
31
import javax.annotation.Nonnull;
32
import javax.annotation.Nullable;
33
import javax.annotation.concurrent.Immutable;
34
import javax.annotation.concurrent.NotThreadSafe;
35

36
/**
37
 * A pluggable component that receives resolved addresses from {@link NameResolver} and provides the
38
 * channel a usable subchannel when asked.
39
 *
40
 * <h3>Overview</h3>
41
 *
42
 * <p>A LoadBalancer typically implements three interfaces:
43
 * <ol>
44
 *   <li>{@link LoadBalancer} is the main interface.  All methods on it are invoked sequentially
45
 *       in the same <strong>synchronization context</strong> (see next section) as returned by
46
 *       {@link io.grpc.LoadBalancer.Helper#getSynchronizationContext}.  It receives the results
47
 *       from the {@link NameResolver}, updates of subchannels' connectivity states, and the
48
 *       channel's request for the LoadBalancer to shutdown.</li>
49
 *   <li>{@link SubchannelPicker SubchannelPicker} does the actual load-balancing work.  It selects
50
 *       a {@link Subchannel Subchannel} for each new RPC.</li>
51
 *   <li>{@link Factory Factory} creates a new {@link LoadBalancer} instance.
52
 * </ol>
53
 *
54
 * <p>{@link Helper Helper} is implemented by gRPC library and provided to {@link Factory
55
 * Factory}. It provides functionalities that a {@code LoadBalancer} implementation would typically
56
 * need.
57
 *
58
 * <h3>The Synchronization Context</h3>
59
 *
60
 * <p>All methods on the {@link LoadBalancer} interface are called from a Synchronization Context,
61
 * meaning they are serialized, thus the balancer implementation doesn't need to worry about
62
 * synchronization among them.  {@link io.grpc.LoadBalancer.Helper#getSynchronizationContext}
63
 * allows implementations to schedule tasks to be run in the same Synchronization Context, with or
64
 * without a delay, thus those tasks don't need to worry about synchronizing with the balancer
65
 * methods.
66
 *
67
 * <p>However, the actual running thread may be the network thread, thus the following rules must be
68
 * followed to prevent blocking or even dead-locking in a network:
69
 *
70
 * <ol>
71
 *
72
 *   <li><strong>Never block in the Synchronization Context</strong>.  The callback methods must
73
 *   return quickly.  Examples or work that must be avoided: CPU-intensive calculation, waiting on
74
 *   synchronization primitives, blocking I/O, blocking RPCs, etc.</li>
75
 *
76
 *   <li><strong>Avoid calling into other components with lock held</strong>.  The Synchronization
77
 *   Context may be under a lock, e.g., the transport lock of OkHttp.  If your LoadBalancer holds a
78
 *   lock in a callback method (e.g., {@link #handleResolvedAddresses handleResolvedAddresses()})
79
 *   while calling into another method that also involves locks, be cautious of deadlock.  Generally
80
 *   you wouldn't need any locking in the LoadBalancer if you follow the canonical implementation
81
 *   pattern below.</li>
82
 *
83
 * </ol>
84
 *
85
 * <h3>The canonical implementation pattern</h3>
86
 *
87
 * <p>A {@link LoadBalancer} keeps states like the latest addresses from NameResolver, the
88
 * Subchannel(s) and their latest connectivity states.  These states are mutated within the
89
 * Synchronization Context,
90
 *
91
 * <p>A typical {@link SubchannelPicker SubchannelPicker} holds a snapshot of these states.  It may
92
 * have its own states, e.g., a picker from a round-robin load-balancer may keep a pointer to the
93
 * next Subchannel, which are typically mutated by multiple threads.  The picker should only mutate
94
 * its own state, and should not mutate or re-acquire the states of the LoadBalancer.  This way the
95
 * picker only needs to synchronize its own states, which is typically trivial to implement.
96
 *
97
 * <p>When the LoadBalancer states changes, e.g., Subchannels has become or stopped being READY, and
98
 * we want subsequent RPCs to use the latest list of READY Subchannels, LoadBalancer would create a
99
 * new picker, which holds a snapshot of the latest Subchannel list.  Refer to the javadoc of {@link
100
 * io.grpc.LoadBalancer.SubchannelStateListener#onSubchannelState onSubchannelState()} how to do
101
 * this properly.
102
 *
103
 * <p>No synchronization should be necessary between LoadBalancer and its pickers if you follow
104
 * the pattern above.  It may be possible to implement in a different way, but that would usually
105
 * result in more complicated threading.
106
 *
107
 * @since 1.2.0
108
 */
109
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
110
@NotThreadSafe
111
public abstract class LoadBalancer {
1✔
112

113
  @Internal
114
  @NameResolver.ResolutionResultAttr
115
  public static final Attributes.Key<Map<String, ?>> ATTR_HEALTH_CHECKING_CONFIG =
1✔
116
      Attributes.Key.create("internal:health-checking-config");
1✔
117

118
  @Internal
119
  public static final LoadBalancer.CreateSubchannelArgs.Key<LoadBalancer.SubchannelStateListener>
120
      HEALTH_CONSUMER_LISTENER_ARG_KEY =
1✔
121
      LoadBalancer.CreateSubchannelArgs.Key.create("internal:health-check-consumer-listener");
1✔
122

123
  @Internal
124
  public static final LoadBalancer.CreateSubchannelArgs.Key<Boolean>
125
      DISABLE_SUBCHANNEL_RECONNECT_KEY =
1✔
126
      LoadBalancer.CreateSubchannelArgs.Key.createWithDefault(
1✔
127
          "internal:disable-subchannel-reconnect", Boolean.FALSE);
128

129
  @Internal
130
  public static final Attributes.Key<Boolean>
131
      HAS_HEALTH_PRODUCER_LISTENER_KEY =
1✔
132
      Attributes.Key.create("internal:has-health-check-producer-listener");
1✔
133

134
  public static final Attributes.Key<Boolean> IS_PETIOLE_POLICY =
1✔
135
      Attributes.Key.create("io.grpc.IS_PETIOLE_POLICY");
1✔
136

137
  /**
138
   * A picker that always returns an erring pick.
139
   *
140
   * @deprecated Use {@code new FixedResultPicker(PickResult.withNoResult())} instead.
141
   */
142
  @Deprecated
143
  public static final SubchannelPicker EMPTY_PICKER = new SubchannelPicker() {
1✔
144
    @Override
145
    public PickResult pickSubchannel(PickSubchannelArgs args) {
146
      return PickResult.withNoResult();
×
147
    }
148

149
    @Override
150
    public String toString() {
151
      return "EMPTY_PICKER";
×
152
    }
153
  };
154

155
  private int recursionCount;
156

157
  /**
158
   * Handles newly resolved addresses and metadata attributes from name resolution system.
159
   * Addresses in {@link EquivalentAddressGroup} should be considered equivalent but may be
160
   * flattened into a single list if needed.
161
   *
162
   * @param resolvedAddresses the resolved server addresses, attributes, and config.
163
   * @since 1.21.0
164
   *
165
   * @deprecated  Use instead {@link #acceptResolvedAddresses(ResolvedAddresses)}
166
   */
167
  @Deprecated
168
  public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
169
    if (recursionCount++ == 0) {
1✔
170
      // Note that the information about the addresses actually being accepted will be lost
171
      // if you rely on this method for backward compatibility.
172
      acceptResolvedAddresses(resolvedAddresses);
1✔
173
    }
174
    recursionCount = 0;
1✔
175
  }
1✔
176

177
  /**
178
   * Accepts newly resolved addresses from the name resolution system. The {@link
179
   * EquivalentAddressGroup} addresses should be considered equivalent but may be flattened into a
180
   * single list if needed.
181
   *
182
   * @param resolvedAddresses the resolved server addresses, attributes, and config
183
   * @return {@code Status.OK} if the resolved addresses were accepted, otherwise an error to report
184
   *     to the name resolver
185
   *
186
   * @since 1.49.0
187
   */
188
  public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
189
    if (resolvedAddresses.getAddresses().isEmpty()
1✔
190
        && !canHandleEmptyAddressListFromNameResolution()) {
×
191
      Status unavailableStatus = Status.UNAVAILABLE.withDescription(
×
192
              "NameResolver returned no usable address. addrs=" + resolvedAddresses.getAddresses()
×
193
                      + ", attrs=" + resolvedAddresses.getAttributes());
×
194
      handleNameResolutionError(unavailableStatus);
×
195
      return unavailableStatus;
×
196
    } else {
197
      if (recursionCount++ == 0) {
1✔
198
        handleResolvedAddresses(resolvedAddresses);
×
199
      }
200
      recursionCount = 0;
1✔
201

202
      return Status.OK;
1✔
203
    }
204
  }
205

206
  /**
207
   * Represents a combination of the resolved server address, associated attributes and a load
208
   * balancing policy config.  The config is from the {@link
209
   * LoadBalancerProvider#parseLoadBalancingPolicyConfig(Map)}.
210
   *
211
   * @since 1.21.0
212
   */
213
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/11657")
214
  public static final class ResolvedAddresses {
215
    private final List<EquivalentAddressGroup> addresses;
216
    @NameResolver.ResolutionResultAttr
217
    private final Attributes attributes;
218
    @Nullable
219
    private final Object loadBalancingPolicyConfig;
220
    // Make sure to update toBuilder() below!
221

222
    private ResolvedAddresses(
223
        List<EquivalentAddressGroup> addresses,
224
        @NameResolver.ResolutionResultAttr Attributes attributes,
225
        Object loadBalancingPolicyConfig) {
1✔
226
      this.addresses =
1✔
227
          Collections.unmodifiableList(new ArrayList<>(checkNotNull(addresses, "addresses")));
1✔
228
      this.attributes = checkNotNull(attributes, "attributes");
1✔
229
      this.loadBalancingPolicyConfig = loadBalancingPolicyConfig;
1✔
230
    }
1✔
231

232
    /**
233
     * Factory for constructing a new Builder.
234
     *
235
     * @since 1.21.0
236
     */
237
    public static Builder newBuilder() {
238
      return new Builder();
1✔
239
    }
240

241
    /**
242
     * Converts this back to a builder.
243
     *
244
     * @since 1.21.0
245
     */
246
    public Builder toBuilder() {
247
      return newBuilder()
1✔
248
          .setAddresses(addresses)
1✔
249
          .setAttributes(attributes)
1✔
250
          .setLoadBalancingPolicyConfig(loadBalancingPolicyConfig);
1✔
251
    }
252

253
    /**
254
     * Gets the server addresses.
255
     *
256
     * @since 1.21.0
257
     */
258
    public List<EquivalentAddressGroup> getAddresses() {
259
      return addresses;
1✔
260
    }
261

262
    /**
263
     * Gets the attributes associated with these addresses.  If this was not previously set,
264
     * {@link Attributes#EMPTY} will be returned.
265
     *
266
     * @since 1.21.0
267
     */
268
    @NameResolver.ResolutionResultAttr
269
    public Attributes getAttributes() {
270
      return attributes;
1✔
271
    }
272

273
    /**
274
     * Gets the domain specific load balancing policy.  This is the config produced by
275
     * {@link LoadBalancerProvider#parseLoadBalancingPolicyConfig(Map)}.
276
     *
277
     * @since 1.21.0
278
     */
279
    @Nullable
280
    public Object getLoadBalancingPolicyConfig() {
281
      return loadBalancingPolicyConfig;
1✔
282
    }
283

284
    /**
285
     * Builder for {@link ResolvedAddresses}.
286
     */
287
    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
288
    public static final class Builder {
289
      private List<EquivalentAddressGroup> addresses;
290
      @NameResolver.ResolutionResultAttr
1✔
291
      private Attributes attributes = Attributes.EMPTY;
292
      @Nullable
293
      private Object loadBalancingPolicyConfig;
294

295
      Builder() {}
1✔
296

297
      /**
298
       * Sets the addresses.  This field is required.
299
       *
300
       * @return this.
301
       */
302
      public Builder setAddresses(List<EquivalentAddressGroup> addresses) {
303
        this.addresses = addresses;
1✔
304
        return this;
1✔
305
      }
306

307
      /**
308
       * Sets the attributes.  This field is optional; if not called, {@link Attributes#EMPTY}
309
       * will be used.
310
       *
311
       * @return this.
312
       */
313
      public Builder setAttributes(@NameResolver.ResolutionResultAttr Attributes attributes) {
314
        this.attributes = attributes;
1✔
315
        return this;
1✔
316
      }
317

318
      /**
319
       * Sets the load balancing policy config. This field is optional.
320
       *
321
       * @return this.
322
       */
323
      public Builder setLoadBalancingPolicyConfig(@Nullable Object loadBalancingPolicyConfig) {
324
        this.loadBalancingPolicyConfig = loadBalancingPolicyConfig;
1✔
325
        return this;
1✔
326
      }
327

328
      /**
329
       * Constructs the {@link ResolvedAddresses}.
330
       */
331
      public ResolvedAddresses build() {
332
        return new ResolvedAddresses(addresses, attributes, loadBalancingPolicyConfig);
1✔
333
      }
334
    }
335

336
    @Override
337
    public String toString() {
338
      return MoreObjects.toStringHelper(this)
1✔
339
          .add("addresses", addresses)
1✔
340
          .add("attributes", attributes)
1✔
341
          .add("loadBalancingPolicyConfig", loadBalancingPolicyConfig)
1✔
342
          .toString();
1✔
343
    }
344

345
    @Override
346
    public int hashCode() {
347
      return Objects.hashCode(addresses, attributes, loadBalancingPolicyConfig);
×
348
    }
349

350
    @Override
351
    public boolean equals(Object obj) {
352
      if (!(obj instanceof ResolvedAddresses)) {
1✔
353
        return false;
×
354
      }
355
      ResolvedAddresses that = (ResolvedAddresses) obj;
1✔
356
      return Objects.equal(this.addresses, that.addresses)
1✔
357
          && Objects.equal(this.attributes, that.attributes)
1✔
358
          && Objects.equal(this.loadBalancingPolicyConfig, that.loadBalancingPolicyConfig);
1✔
359
    }
360
  }
361

362
  /**
363
   * Handles an error from the name resolution system.
364
   *
365
   * @param error a non-OK status
366
   * @since 1.2.0
367
   */
368
  public abstract void handleNameResolutionError(Status error);
369

370
  /**
371
   * Handles a state change on a Subchannel.
372
   *
373
   * <p>The initial state of a Subchannel is IDLE. You won't get a notification for the initial IDLE
374
   * state.
375
   *
376
   * <p>If the new state is not SHUTDOWN, this method should create a new picker and call {@link
377
   * Helper#updateBalancingState Helper.updateBalancingState()}.  Failing to do so may result in
378
   * unnecessary delays of RPCs. Please refer to {@link PickResult#withSubchannel
379
   * PickResult.withSubchannel()}'s javadoc for more information.
380
   *
381
   * <p>SHUTDOWN can only happen in two cases.  One is that LoadBalancer called {@link
382
   * Subchannel#shutdown} earlier, thus it should have already discarded this Subchannel.  The other
383
   * is that Channel is doing a {@link ManagedChannel#shutdownNow forced shutdown} or has already
384
   * terminated, thus there won't be further requests to LoadBalancer.  Therefore, the LoadBalancer
385
   * usually don't need to react to a SHUTDOWN state.
386
   *
387
   * @param subchannel the involved Subchannel
388
   * @param stateInfo the new state
389
   * @since 1.2.0
390
   * @deprecated This method will be removed.  Stop overriding it.  Instead, pass {@link
391
   *             SubchannelStateListener} to {@link Subchannel#start} to receive Subchannel state
392
   *             updates
393
   */
394
  @Deprecated
395
  public void handleSubchannelState(
396
      Subchannel subchannel, ConnectivityStateInfo stateInfo) {
397
    // Do nothing.  If the implementation doesn't implement this, it will get subchannel states from
398
    // the new API.  We don't throw because there may be forwarding LoadBalancers still plumb this.
399
  }
×
400

401
  /**
402
   * The channel asks the load-balancer to shutdown.  No more methods on this class will be called
403
   * after this method.  The implementation should shutdown all Subchannels and OOB channels, and do
404
   * any other cleanup as necessary.
405
   *
406
   * @since 1.2.0
407
   */
408
  public abstract void shutdown();
409

410
  /**
411
   * Whether this LoadBalancer can handle empty address group list to be passed to {@link
412
   * #handleResolvedAddresses(ResolvedAddresses)}.  The default implementation returns
413
   * {@code false}, meaning that if the NameResolver returns an empty list, the Channel will turn
414
   * that into an error and call {@link #handleNameResolutionError}.  LoadBalancers that want to
415
   * accept empty lists should override this method and return {@code true}.
416
   *
417
   * <p>This method should always return a constant value.  It's not specified when this will be
418
   * called.
419
   *
420
   * <p>Note that this method is only called when implementing {@code handleResolvedAddresses()}
421
   * instead of {@code acceptResolvedAddresses()}.
422
   *
423
   * @deprecated Instead of overwriting this and {@code handleResolvedAddresses()}, only
424
   *     overwrite {@code acceptResolvedAddresses()} which indicates if the addresses provided
425
   *     by the name resolver are acceptable with the {@code boolean} return value.
426
   */
427
  @Deprecated
428
  @SuppressWarnings("InlineMeSuggester")
429
  public boolean canHandleEmptyAddressListFromNameResolution() {
430
    return false;
×
431
  }
432

433
  /**
434
   * The channel asks the LoadBalancer to establish connections now (if applicable) so that the
435
   * upcoming RPC may then just pick a ready connection without waiting for connections.  This
436
   * is triggered by {@link ManagedChannel#getState ManagedChannel.getState(true)}.
437
   *
438
   * <p>If LoadBalancer doesn't override it, this is no-op.  If it infeasible to create connections
439
   * given the current state, e.g. no Subchannel has been created yet, LoadBalancer can ignore this
440
   * request.
441
   *
442
   * @since 1.22.0
443
   */
444
  public void requestConnection() {}
1✔
445

446
  /**
447
   * The main balancing logic.  It <strong>must be thread-safe</strong>. Typically it should only
448
   * synchronize on its own state, and avoid synchronizing with the LoadBalancer's state.
449
   *
450
   * @since 1.2.0
451
   */
452
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
453
  public abstract static class SubchannelPicker {
1✔
454
    /**
455
     * Make a balancing decision for a new RPC.
456
     *
457
     * @param args the pick arguments
458
     * @since 1.3.0
459
     */
460
    public abstract PickResult pickSubchannel(PickSubchannelArgs args);
461
  }
462

463
  /**
464
   * Provides arguments for a {@link SubchannelPicker#pickSubchannel(
465
   * LoadBalancer.PickSubchannelArgs)}.
466
   *
467
   * @since 1.2.0
468
   */
469
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
470
  public abstract static class PickSubchannelArgs {
1✔
471

472
    /**
473
     * Call options.
474
     *
475
     * @since 1.2.0
476
     */
477
    public abstract CallOptions getCallOptions();
478

479
    /**
480
     * Headers of the call. {@link SubchannelPicker#pickSubchannel} may mutate it before before
481
     * returning.
482
     *
483
     * @since 1.2.0
484
     */
485
    public abstract Metadata getHeaders();
486

487
    /**
488
     * Call method.
489
     *
490
     * @since 1.2.0
491
     */
492
    public abstract MethodDescriptor<?, ?> getMethodDescriptor();
493

494
    /**
495
     * Gets an object that can be informed about what sort of pick was made.
496
     */
497
    @Internal
498
    public PickDetailsConsumer getPickDetailsConsumer() {
499
      return new PickDetailsConsumer() {};
×
500
    }
501
  }
502

503
  /** Receives information about the pick being chosen. */
504
  @Internal
505
  public interface PickDetailsConsumer {
506
    /**
507
     * Optional labels that provide context of how the pick was routed. Particularly helpful for
508
     * per-RPC metrics.
509
     *
510
     * @throws NullPointerException if key or value is {@code null}
511
     */
512
    default void addOptionalLabel(String key, String value) {
513
      checkNotNull(key, "key");
1✔
514
      checkNotNull(value, "value");
1✔
515
    }
1✔
516
  }
517

518
  /**
519
   * A balancing decision made by {@link SubchannelPicker SubchannelPicker} for an RPC.
520
   *
521
   * <p>The outcome of the decision will be one of the following:
522
   * <ul>
523
   *   <li>Proceed: if a Subchannel is provided via {@link #withSubchannel withSubchannel()}, and is
524
   *       in READY state when the RPC tries to start on it, the RPC will proceed on that
525
   *       Subchannel.</li>
526
   *   <li>Error: if an error is provided via {@link #withError withError()}, and the RPC is not
527
   *       wait-for-ready (i.e., {@link CallOptions#withWaitForReady} was not called), the RPC will
528
   *       fail immediately with the given error.</li>
529
   *   <li>Buffer: in all other cases, the RPC will be buffered in the Channel, until the next
530
   *       picker is provided via {@link Helper#updateBalancingState Helper.updateBalancingState()},
531
   *       when the RPC will go through the same picking process again.</li>
532
   * </ul>
533
   *
534
   * @since 1.2.0
535
   */
536
  @Immutable
537
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
538
  public static final class PickResult {
539
    private static final PickResult NO_RESULT = new PickResult(null, null, Status.OK, false);
1✔
540

541
    @Nullable private final Subchannel subchannel;
542
    @Nullable private final ClientStreamTracer.Factory streamTracerFactory;
543
    // An error to be propagated to the application if subchannel == null
544
    // Or OK if there is no error.
545
    // subchannel being null and error being OK means RPC needs to wait
546
    private final Status status;
547
    // True if the result is created by withDrop()
548
    private final boolean drop;
549
    @Nullable private final String authorityOverride;
550

551
    private PickResult(
552
        @Nullable Subchannel subchannel, @Nullable ClientStreamTracer.Factory streamTracerFactory,
553
        Status status, boolean drop) {
1✔
554
      this.subchannel = subchannel;
1✔
555
      this.streamTracerFactory = streamTracerFactory;
1✔
556
      this.status = checkNotNull(status, "status");
1✔
557
      this.drop = drop;
1✔
558
      this.authorityOverride = null;
1✔
559
    }
1✔
560

561
    private PickResult(
562
        @Nullable Subchannel subchannel, @Nullable ClientStreamTracer.Factory streamTracerFactory,
563
        Status status, boolean drop, @Nullable String authorityOverride) {
1✔
564
      this.subchannel = subchannel;
1✔
565
      this.streamTracerFactory = streamTracerFactory;
1✔
566
      this.status = checkNotNull(status, "status");
1✔
567
      this.drop = drop;
1✔
568
      this.authorityOverride = authorityOverride;
1✔
569
    }
1✔
570

571
    /**
572
     * A decision to proceed the RPC on a Subchannel.
573
     *
574
     * <p>The Subchannel should either be an original Subchannel returned by {@link
575
     * Helper#createSubchannel Helper.createSubchannel()}, or a wrapper of it preferably based on
576
     * {@code ForwardingSubchannel}.  At the very least its {@link Subchannel#getInternalSubchannel
577
     * getInternalSubchannel()} must return the same object as the one returned by the original.
578
     * Otherwise the Channel cannot use it for the RPC.
579
     *
580
     * <p>When the RPC tries to use the return Subchannel, which is briefly after this method
581
     * returns, the state of the Subchannel will decide where the RPC would go:
582
     *
583
     * <ul>
584
     *   <li>READY: the RPC will proceed on this Subchannel.</li>
585
     *   <li>IDLE: the RPC will be buffered.  Subchannel will attempt to create connection.</li>
586
     *   <li>All other states: the RPC will be buffered.</li>
587
     * </ul>
588
     *
589
     * <p><strong>All buffered RPCs will stay buffered</strong> until the next call of {@link
590
     * Helper#updateBalancingState Helper.updateBalancingState()}, which will trigger a new picking
591
     * process.
592
     *
593
     * <p>Note that Subchannel's state may change at the same time the picker is making the
594
     * decision, which means the decision may be made with (to-be) outdated information.  For
595
     * example, a picker may return a Subchannel known to be READY, but it has become IDLE when is
596
     * about to be used by the RPC, which makes the RPC to be buffered.  The LoadBalancer will soon
597
     * learn about the Subchannels' transition from READY to IDLE, create a new picker and allow the
598
     * RPC to use another READY transport if there is any.
599
     *
600
     * <p>You will want to avoid running into a situation where there are READY Subchannels out
601
     * there but some RPCs are still buffered for longer than a brief time.
602
     * <ul>
603
     *   <li>This can happen if you return Subchannels with states other than READY and IDLE.  For
604
     *       example, suppose you round-robin on 2 Subchannels, in READY and CONNECTING states
605
     *       respectively.  If the picker ignores the state and pick them equally, 50% of RPCs will
606
     *       be stuck in buffered state until both Subchannels are READY.</li>
607
     *   <li>This can also happen if you don't create a new picker at key state changes of
608
     *       Subchannels.  Take the above round-robin example again.  Suppose you do pick only READY
609
     *       and IDLE Subchannels, and initially both Subchannels are READY.  Now one becomes IDLE,
610
     *       then CONNECTING and stays CONNECTING for a long time.  If you don't create a new picker
611
     *       in response to the CONNECTING state to exclude that Subchannel, 50% of RPCs will hit it
612
     *       and be buffered even though the other Subchannel is READY.</li>
613
     * </ul>
614
     *
615
     * <p>In order to prevent unnecessary delay of RPCs, the rules of thumb are:
616
     * <ol>
617
     *   <li>The picker should only pick Subchannels that are known as READY or IDLE.  Whether to
618
     *       pick IDLE Subchannels depends on whether you want Subchannels to connect on-demand or
619
     *       actively:
620
     *       <ul>
621
     *         <li>If you want connect-on-demand, include IDLE Subchannels in your pick results,
622
     *             because when an RPC tries to use an IDLE Subchannel, the Subchannel will try to
623
     *             connect.</li>
624
     *         <li>If you want Subchannels to be always connected even when there is no RPC, you
625
     *             would call {@link Subchannel#requestConnection Subchannel.requestConnection()}
626
     *             whenever the Subchannel has transitioned to IDLE, then you don't need to include
627
     *             IDLE Subchannels in your pick results.</li>
628
     *       </ul></li>
629
     *   <li>Always create a new picker and call {@link Helper#updateBalancingState
630
     *       Helper.updateBalancingState()} whenever {@link #handleSubchannelState
631
     *       handleSubchannelState()} is called, unless the new state is SHUTDOWN. See
632
     *       {@code handleSubchannelState}'s javadoc for more details.</li>
633
     * </ol>
634
     *
635
     * @param subchannel the picked Subchannel.  It must have been {@link Subchannel#start started}
636
     * @param streamTracerFactory if not null, will be used to trace the activities of the stream
637
     *                            created as a result of this pick. Note it's possible that no
638
     *                            stream is created at all in some cases.
639
     * @since 1.3.0
640
     */
641
    // TODO(shivaspeaks): Need to deprecate old APIs and create new ones,
642
    // per https://github.com/grpc/grpc-java/issues/12662.
643
    public static PickResult withSubchannel(
644
        Subchannel subchannel, @Nullable ClientStreamTracer.Factory streamTracerFactory) {
645
      return new PickResult(
1✔
646
          checkNotNull(subchannel, "subchannel"), streamTracerFactory, Status.OK,
1✔
647
          false);
648
    }
649

650
    /**
651
     * Same as {@code withSubchannel(subchannel, streamTracerFactory)} but with an authority name
652
     * to override in the host header.
653
     */
654
    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/11656")
655
    public static PickResult withSubchannel(
656
        Subchannel subchannel, @Nullable ClientStreamTracer.Factory streamTracerFactory,
657
        @Nullable String authorityOverride) {
658
      return new PickResult(
1✔
659
          checkNotNull(subchannel, "subchannel"), streamTracerFactory, Status.OK,
1✔
660
          false, authorityOverride);
661
    }
662

663
    /**
664
     * Equivalent to {@code withSubchannel(subchannel, null)}.
665
     *
666
     * @since 1.2.0
667
     */
668
    public static PickResult withSubchannel(Subchannel subchannel) {
669
      return withSubchannel(subchannel, null);
1✔
670
    }
671

672
    /**
673
     * Creates a new {@code PickResult} with the given {@code subchannel},
674
     * but retains all other properties from this {@code PickResult}.
675
     *
676
     * @since 1.80.0
677
     */
678
    public PickResult copyWithSubchannel(Subchannel subchannel) {
679
      return new PickResult(checkNotNull(subchannel, "subchannel"), streamTracerFactory,
1✔
680
          status, drop, authorityOverride);
681
    }
682

683
    /**
684
     * Creates a new {@code PickResult} with the given {@code streamTracerFactory},
685
     * but retains all other properties from this {@code PickResult}.
686
     *
687
     * @since 1.80.0
688
     */
689
    public PickResult copyWithStreamTracerFactory(
690
        @Nullable ClientStreamTracer.Factory streamTracerFactory) {
691
      return new PickResult(subchannel, streamTracerFactory, status, drop, authorityOverride);
1✔
692
    }
693

694
    /**
695
     * A decision to report a connectivity error to the RPC.  If the RPC is {@link
696
     * CallOptions#withWaitForReady wait-for-ready}, it will stay buffered.  Otherwise, it will fail
697
     * with the given error.
698
     *
699
     * @param error the error status.  Must not be OK.
700
     * @since 1.2.0
701
     */
702
    public static PickResult withError(Status error) {
703
      Preconditions.checkArgument(!error.isOk(), "error status shouldn't be OK");
1✔
704
      return new PickResult(null, null, error, false);
1✔
705
    }
706

707
    /**
708
     * A decision to fail an RPC immediately.  This is a final decision and will ignore retry
709
     * policy.
710
     *
711
     * @param status the status with which the RPC will fail.  Must not be OK.
712
     * @since 1.8.0
713
     */
714
    public static PickResult withDrop(Status status) {
715
      Preconditions.checkArgument(!status.isOk(), "drop status shouldn't be OK");
1✔
716
      return new PickResult(null, null, status, true);
1✔
717
    }
718

719
    /**
720
     * No decision could be made.  The RPC will stay buffered.
721
     *
722
     * @since 1.2.0
723
     */
724
    public static PickResult withNoResult() {
725
      return NO_RESULT;
1✔
726
    }
727

728
    /** Returns the authority override if any. */
729
    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/11656")
730
    @Nullable
731
    public String getAuthorityOverride() {
732
      return authorityOverride;
1✔
733
    }
734

735
    /**
736
     * The Subchannel if this result was created by {@link #withSubchannel withSubchannel()}, or
737
     * null otherwise.
738
     *
739
     * @since 1.2.0
740
     */
741
    @Nullable
742
    public Subchannel getSubchannel() {
743
      return subchannel;
1✔
744
    }
745

746
    /**
747
     * The stream tracer factory this result was created with.
748
     *
749
     * @since 1.3.0
750
     */
751
    @Nullable
752
    public ClientStreamTracer.Factory getStreamTracerFactory() {
753
      return streamTracerFactory;
1✔
754
    }
755

756
    /**
757
     * The status associated with this result.  Non-{@code OK} if created with {@link #withError
758
     * withError}, or {@code OK} otherwise.
759
     *
760
     * @since 1.2.0
761
     */
762
    public Status getStatus() {
763
      return status;
1✔
764
    }
765

766
    /**
767
     * Returns {@code true} if this result was created by {@link #withDrop withDrop()}.
768
     *
769
     * @since 1.8.0
770
     */
771
    public boolean isDrop() {
772
      return drop;
1✔
773
    }
774

775
    /**
776
     * Returns {@code true} if the pick was not created with {@link #withNoResult()}.
777
     */
778
    public boolean hasResult() {
779
      return !(subchannel == null && status.isOk());
1✔
780
    }
781

782
    @Override
783
    public String toString() {
784
      return MoreObjects.toStringHelper(this)
1✔
785
          .add("subchannel", subchannel)
1✔
786
          .add("streamTracerFactory", streamTracerFactory)
1✔
787
          .add("status", status)
1✔
788
          .add("drop", drop)
1✔
789
          .add("authority-override", authorityOverride)
1✔
790
          .toString();
1✔
791
    }
792

793
    @Override
794
    public int hashCode() {
795
      return Objects.hashCode(subchannel, status, streamTracerFactory, drop);
1✔
796
    }
797

798
    /**
799
     * Returns true if the {@link Subchannel}, {@link Status}, and
800
     * {@link ClientStreamTracer.Factory} all match.
801
     */
802
    @Override
803
    public boolean equals(Object other) {
804
      if (!(other instanceof PickResult)) {
1✔
805
        return false;
×
806
      }
807
      PickResult that = (PickResult) other;
1✔
808
      return Objects.equal(subchannel, that.subchannel) && Objects.equal(status, that.status)
1✔
809
          && Objects.equal(streamTracerFactory, that.streamTracerFactory)
1✔
810
          && drop == that.drop;
811
    }
812
  }
813

814
  /**
815
   * Arguments for creating a {@link Subchannel}.
816
   *
817
   * @since 1.22.0
818
   */
819
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
820
  public static final class CreateSubchannelArgs {
821
    private final List<EquivalentAddressGroup> addrs;
822
    private final Attributes attrs;
823
    private final Object[][] customOptions;
824

825
    private CreateSubchannelArgs(
826
        List<EquivalentAddressGroup> addrs, Attributes attrs, Object[][] customOptions) {
1✔
827
      this.addrs = checkNotNull(addrs, "addresses are not set");
1✔
828
      this.attrs = checkNotNull(attrs, "attrs");
1✔
829
      this.customOptions = checkNotNull(customOptions, "customOptions");
1✔
830
    }
1✔
831

832
    /**
833
     * Returns the addresses, which is an unmodifiable list.
834
     */
835
    public List<EquivalentAddressGroup> getAddresses() {
836
      return addrs;
1✔
837
    }
838

839
    /**
840
     * Returns the attributes.
841
     */
842
    public Attributes getAttributes() {
843
      return attrs;
1✔
844
    }
845

846
    /**
847
     * Get the value for a custom option or its inherent default.
848
     *
849
     * @param key Key identifying option
850
     */
851
    @SuppressWarnings("unchecked")
852
    public <T> T getOption(Key<T> key) {
853
      Preconditions.checkNotNull(key, "key");
1✔
854
      for (int i = 0; i < customOptions.length; i++) {
1✔
855
        if (key.equals(customOptions[i][0])) {
1✔
856
          return (T) customOptions[i][1];
1✔
857
        }
858
      }
859
      return key.defaultValue;
1✔
860
    }
861

862
    /**
863
     * Returns a builder with the same initial values as this object.
864
     */
865
    public Builder toBuilder() {
866
      return newBuilder().setAddresses(addrs).setAttributes(attrs).copyCustomOptions(customOptions);
1✔
867
    }
868

869
    /**
870
     * Creates a new builder.
871
     */
872
    public static Builder newBuilder() {
873
      return new Builder();
1✔
874
    }
875

876
    @Override
877
    public String toString() {
878
      return MoreObjects.toStringHelper(this)
1✔
879
          .add("addrs", addrs)
1✔
880
          .add("attrs", attrs)
1✔
881
          .add("customOptions", Arrays.deepToString(customOptions))
1✔
882
          .toString();
1✔
883
    }
884

885
    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
886
    public static final class Builder {
887

888
      private static final Object[][] EMPTY_CUSTOM_OPTIONS = new Object[0][2];
1✔
889

890
      private List<EquivalentAddressGroup> addrs;
891
      private Attributes attrs = Attributes.EMPTY;
1✔
892
      private Object[][] customOptions = EMPTY_CUSTOM_OPTIONS;
1✔
893

894
      Builder() {
1✔
895
      }
1✔
896

897
      private Builder copyCustomOptions(Object[][] options) {
898
        customOptions = new Object[options.length][2];
1✔
899
        System.arraycopy(options, 0, customOptions, 0, options.length);
1✔
900
        return this;
1✔
901
      }
902

903
      /**
904
       * Add a custom option. Any existing value for the key is overwritten.
905
       *
906
       * <p>This is an <strong>optional</strong> property.
907
       *
908
       * @param key the option key
909
       * @param value the option value
910
       */
911
      public <T> Builder addOption(Key<T> key, T value) {
912
        Preconditions.checkNotNull(key, "key");
1✔
913
        Preconditions.checkNotNull(value, "value");
1✔
914

915
        int existingIdx = -1;
1✔
916
        for (int i = 0; i < customOptions.length; i++) {
1✔
917
          if (key.equals(customOptions[i][0])) {
1✔
918
            existingIdx = i;
1✔
919
            break;
1✔
920
          }
921
        }
922

923
        if (existingIdx == -1) {
1✔
924
          Object[][] newCustomOptions = new Object[customOptions.length + 1][2];
1✔
925
          System.arraycopy(customOptions, 0, newCustomOptions, 0, customOptions.length);
1✔
926
          customOptions = newCustomOptions;
1✔
927
          existingIdx = customOptions.length - 1;
1✔
928
        }
929
        customOptions[existingIdx] = new Object[]{key, value};
1✔
930
        return this;
1✔
931
      }
932

933
      /**
934
       * The addresses to connect to.  All addresses are considered equivalent and will be tried
935
       * in the order they are provided.
936
       */
937
      public Builder setAddresses(EquivalentAddressGroup addrs) {
938
        this.addrs = Collections.singletonList(addrs);
1✔
939
        return this;
1✔
940
      }
941

942
      /**
943
       * The addresses to connect to.  All addresses are considered equivalent and will
944
       * be tried in the order they are provided.
945
       *
946
       * <p>This is a <strong>required</strong> property.
947
       *
948
       * @throws IllegalArgumentException if {@code addrs} is empty
949
       */
950
      public Builder setAddresses(List<EquivalentAddressGroup> addrs) {
951
        checkArgument(!addrs.isEmpty(), "addrs is empty");
1✔
952
        this.addrs = Collections.unmodifiableList(new ArrayList<>(addrs));
1✔
953
        return this;
1✔
954
      }
955

956
      /**
957
       * Attributes provided here will be included in {@link Subchannel#getAttributes}.
958
       *
959
       * <p>This is an <strong>optional</strong> property.  Default is empty if not set.
960
       */
961
      public Builder setAttributes(Attributes attrs) {
962
        this.attrs = checkNotNull(attrs, "attrs");
1✔
963
        return this;
1✔
964
      }
965

966
      /**
967
       * Creates a new args object.
968
       */
969
      public CreateSubchannelArgs build() {
970
        return new CreateSubchannelArgs(addrs, attrs, customOptions);
1✔
971
      }
972
    }
973

974
    /**
975
     * Key for a key-value pair. Uses reference equality.
976
     */
977
    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
978
    public static final class Key<T> {
979

980
      private final String debugString;
981
      private final T defaultValue;
982

983
      private Key(String debugString, T defaultValue) {
1✔
984
        this.debugString = debugString;
1✔
985
        this.defaultValue = defaultValue;
1✔
986
      }
1✔
987

988
      /**
989
       * Factory method for creating instances of {@link Key}. The default value of the key is
990
       * {@code null}.
991
       *
992
       * @param debugString a debug string that describes this key.
993
       * @param <T> Key type
994
       * @return Key object
995
       */
996
      public static <T> Key<T> create(String debugString) {
997
        Preconditions.checkNotNull(debugString, "debugString");
1✔
998
        return new Key<>(debugString, /*defaultValue=*/ null);
1✔
999
      }
1000

1001
      /**
1002
       * Factory method for creating instances of {@link Key}.
1003
       *
1004
       * @param debugString a debug string that describes this key.
1005
       * @param defaultValue default value to return when value for key not set
1006
       * @param <T> Key type
1007
       * @return Key object
1008
       */
1009
      public static <T> Key<T> createWithDefault(String debugString, T defaultValue) {
1010
        Preconditions.checkNotNull(debugString, "debugString");
1✔
1011
        return new Key<>(debugString, defaultValue);
1✔
1012
      }
1013

1014
      /**
1015
       * Returns the user supplied default value for this key.
1016
       */
1017
      public T getDefault() {
1018
        return defaultValue;
×
1019
      }
1020

1021
      @Override
1022
      public String toString() {
1023
        return debugString;
1✔
1024
      }
1025
    }
1026
  }
1027

1028
  /**
1029
   * Provides essentials for LoadBalancer implementations.
1030
   *
1031
   * <p>This class is thread-safe.
1032
   *
1033
   * @since 1.2.0
1034
   */
1035
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
1036
  public abstract static class Helper {
1✔
1037
    /**
1038
     * Creates a Subchannel, which is a logical connection to the given group of addresses which are
1039
     * considered equivalent.  The {@code attrs} are custom attributes associated with this
1040
     * Subchannel, and can be accessed later through {@link Subchannel#getAttributes
1041
     * Subchannel.getAttributes()}.
1042
     *
1043
     * <p>The LoadBalancer is responsible for closing unused Subchannels, and closing all
1044
     * Subchannels within {@link #shutdown}.
1045
     *
1046
     * <p>It must be called from {@link #getSynchronizationContext the Synchronization Context}
1047
     *
1048
     * @return Must return a valid Subchannel object, may not return null.
1049
     *
1050
     * @since 1.22.0
1051
     */
1052
    public Subchannel createSubchannel(CreateSubchannelArgs args) {
1053
      throw new UnsupportedOperationException();
1✔
1054
    }
1055

1056
    /**
1057
     * Create an out-of-band channel for the LoadBalancer’s own RPC needs, e.g., talking to an
1058
     * external load-balancer service.
1059
     *
1060
     * <p>The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB
1061
     * channels within {@link #shutdown}.
1062
     *
1063
     * @since 1.4.0
1064
     */
1065
    public abstract ManagedChannel createOobChannel(EquivalentAddressGroup eag, String authority);
1066

1067
    /**
1068
     * Create an out-of-band channel for the LoadBalancer's own RPC needs, e.g., talking to an
1069
     * external load-balancer service. This version of the method allows multiple EAGs, so different
1070
     * addresses can have different authorities.
1071
     *
1072
     * <p>The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB
1073
     * channels within {@link #shutdown}.
1074
     * */
1075
    public ManagedChannel createOobChannel(List<EquivalentAddressGroup> eag,
1076
        String authority) {
1077
      throw new UnsupportedOperationException();
×
1078
    }
1079

1080
    /**
1081
     * Updates the addresses used for connections in the {@code Channel} that was created by {@link
1082
     * #createOobChannel(EquivalentAddressGroup, String)}. This is superior to {@link
1083
     * #createOobChannel(EquivalentAddressGroup, String)} when the old and new addresses overlap,
1084
     * since the channel can continue using an existing connection.
1085
     *
1086
     * @throws IllegalArgumentException if {@code channel} was not returned from {@link
1087
     *     #createOobChannel}
1088
     * @since 1.4.0
1089
     */
1090
    public void updateOobChannelAddresses(ManagedChannel channel, EquivalentAddressGroup eag) {
1091
      throw new UnsupportedOperationException();
×
1092
    }
1093

1094
    /**
1095
     * Updates the addresses with a new EAG list. Connection is continued when old and new addresses
1096
     * overlap.
1097
     * */
1098
    public void updateOobChannelAddresses(ManagedChannel channel,
1099
        List<EquivalentAddressGroup> eag) {
1100
      throw new UnsupportedOperationException();
×
1101
    }
1102

1103
    /**
1104
     * Creates an out-of-band channel for LoadBalancer's own RPC needs, e.g., talking to an external
1105
     * load-balancer service, that is specified by a target string.  See the documentation on
1106
     * {@link ManagedChannelBuilder#forTarget} for the format of a target string.
1107
     *
1108
     * <p>The target string will be resolved by a {@link NameResolver} created according to the
1109
     * target string.
1110
     *
1111
     * <p>The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB
1112
     * channels within {@link #shutdown}.
1113
     *
1114
     * @since 1.20.0
1115
     */
1116
    public ManagedChannel createResolvingOobChannel(String target) {
1117
      return createResolvingOobChannelBuilder(target).build();
1✔
1118
    }
1119

1120
    /**
1121
     * Creates an out-of-band channel builder for LoadBalancer's own RPC needs, e.g., talking to an
1122
     * external load-balancer service, that is specified by a target string.  See the documentation
1123
     * on {@link ManagedChannelBuilder#forTarget} for the format of a target string.
1124
     *
1125
     * <p>The target string will be resolved by a {@link NameResolver} created according to the
1126
     * target string.
1127
     *
1128
     * <p>The returned oob-channel builder defaults to use the same authority and ChannelCredentials
1129
     * (without bearer tokens) as the parent channel's for authentication. This is different from
1130
     * {@link #createResolvingOobChannelBuilder(String, ChannelCredentials)}.
1131
     *
1132
     * <p>The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB
1133
     * channels within {@link #shutdown}.
1134
     *
1135
     * @deprecated Use {@link #createResolvingOobChannelBuilder(String, ChannelCredentials)}
1136
     *     instead.
1137
     * @since 1.31.0
1138
     */
1139
    @Deprecated
1140
    public ManagedChannelBuilder<?> createResolvingOobChannelBuilder(String target) {
1141
      throw new UnsupportedOperationException("Not implemented");
×
1142
    }
1143

1144
    /**
1145
     * Creates an out-of-band channel builder for LoadBalancer's own RPC needs, e.g., talking to an
1146
     * external load-balancer service, that is specified by a target string and credentials.  See
1147
     * the documentation on {@link Grpc#newChannelBuilder} for the format of a target string.
1148
     *
1149
     * <p>The target string will be resolved by a {@link NameResolver} created according to the
1150
     * target string.
1151
     *
1152
     * <p>The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB
1153
     * channels within {@link #shutdown}.
1154
     *
1155
     * @since 1.35.0
1156
     */
1157
    public ManagedChannelBuilder<?> createResolvingOobChannelBuilder(
1158
        String target, ChannelCredentials creds) {
1159
      throw new UnsupportedOperationException();
×
1160
    }
1161

1162
    /**
1163
     * Set a new state with a new picker to the channel.
1164
     *
1165
     * <p>When a new picker is provided via {@code updateBalancingState()}, the channel will apply
1166
     * the picker on all buffered RPCs, by calling {@link SubchannelPicker#pickSubchannel(
1167
     * LoadBalancer.PickSubchannelArgs)}.
1168
     *
1169
     * <p>The channel will hold the picker and use it for all RPCs, until {@code
1170
     * updateBalancingState()} is called again and a new picker replaces the old one.  If {@code
1171
     * updateBalancingState()} has never been called, the channel will buffer all RPCs until a
1172
     * picker is provided.
1173
     *
1174
     * <p>It should be called from the Synchronization Context.  Currently will log a warning if
1175
     * violated.  It will become an exception eventually.  See <a
1176
     * href="https://github.com/grpc/grpc-java/issues/5015">#5015</a> for the background.
1177
     *
1178
     * <p>The passed state will be the channel's new state. The SHUTDOWN state should not be passed
1179
     * and its behavior is undefined.
1180
     *
1181
     * @since 1.6.0
1182
     */
1183
    public abstract void updateBalancingState(
1184
        @Nonnull ConnectivityState newState, @Nonnull SubchannelPicker newPicker);
1185

1186
    /**
1187
     * Call {@link NameResolver#refresh} on the channel's resolver.
1188
     *
1189
     * <p>It should be called from the Synchronization Context.  Currently will log a warning if
1190
     * violated.  It will become an exception eventually.  See <a
1191
     * href="https://github.com/grpc/grpc-java/issues/5015">#5015</a> for the background.
1192
     *
1193
     * @since 1.18.0
1194
     */
1195
    public void refreshNameResolution() {
1196
      throw new UnsupportedOperationException();
×
1197
    }
1198

1199
    /**
1200
     * Historically the channel automatically refreshes name resolution if any subchannel
1201
     * connection is broken. It's transitioning to let load balancers make the decision. To
1202
     * avoid silent breakages, the channel checks if {@link #refreshNameResolution} is called
1203
     * by the load balancer. If not, it will do it and log a warning. This will be removed in
1204
     * the future and load balancers are completely responsible for triggering the refresh.
1205
     * See <a href="https://github.com/grpc/grpc-java/issues/8088">#8088</a> for the background.
1206
     *
1207
     * <p>This should rarely be used, but sometimes the address for the subchannel wasn't
1208
     * provided by the name resolver and a refresh needs to be directed somewhere else instead.
1209
     * Then you can call this method to disable the short-tem check for detecting LoadBalancers
1210
     * that need to be updated for the new expected behavior.
1211
     *
1212
     * @since 1.38.0
1213
     * @deprecated Warning has been removed
1214
     */
1215
    @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8088")
1216
    @Deprecated
1217
    public void ignoreRefreshNameResolutionCheck() {
1218
      // no-op
1219
    }
×
1220

1221
    /**
1222
     * Returns a {@link SynchronizationContext} that runs tasks in the same Synchronization Context
1223
     * as that the callback methods on the {@link LoadBalancer} interface are run in.
1224
     *
1225
     * <p>Work added to the synchronization context might not run immediately, so LB implementations
1226
     * must be careful to ensure that any assumptions still hold when it is executed. In particular,
1227
     * the LB might have been shut down or subchannels might have changed state.
1228
     *
1229
     * <p>Pro-tip: in order to call {@link SynchronizationContext#schedule}, you need to provide a
1230
     * {@link ScheduledExecutorService}.  {@link #getScheduledExecutorService} is provided for your
1231
     * convenience.
1232
     *
1233
     * @since 1.17.0
1234
     */
1235
    public SynchronizationContext getSynchronizationContext() {
1236
      // TODO(zhangkun): make getSynchronizationContext() abstract after runSerialized() is deleted
1237
      throw new UnsupportedOperationException();
×
1238
    }
1239

1240
    /**
1241
     * Returns a {@link ScheduledExecutorService} for scheduling delayed tasks.
1242
     *
1243
     * <p>This service is a shared resource and is only meant for quick tasks.  DO NOT block or run
1244
     * time-consuming tasks.
1245
     *
1246
     * <p>The returned service doesn't support {@link ScheduledExecutorService#shutdown shutdown()}
1247
     * and {@link ScheduledExecutorService#shutdownNow shutdownNow()}.  They will throw if called.
1248
     *
1249
     * @since 1.17.0
1250
     */
1251
    public ScheduledExecutorService getScheduledExecutorService() {
1252
      throw new UnsupportedOperationException();
×
1253
    }
1254

1255
    /**
1256
     * Returns the authority string of the channel, which is derived from the DNS-style target name.
1257
     * If overridden by a load balancer, {@link #getUnsafeChannelCredentials} must also be
1258
     * overridden to call {@link #getChannelCredentials} or provide appropriate credentials.
1259
     *
1260
     * @since 1.2.0
1261
     */
1262
    public abstract String getAuthority();
1263

1264
    /**
1265
     * Returns the target string of the channel, guaranteed to include its scheme.
1266
     */
1267
    public String getChannelTarget() {
1268
      throw new UnsupportedOperationException();
×
1269
    }
1270

1271
    /**
1272
     * Returns the ChannelCredentials used to construct the channel, without bearer tokens.
1273
     *
1274
     * @since 1.35.0
1275
     */
1276
    public ChannelCredentials getChannelCredentials() {
1277
      return getUnsafeChannelCredentials().withoutBearerTokens();
×
1278
    }
1279

1280
    /**
1281
     * Returns the UNSAFE ChannelCredentials used to construct the channel,
1282
     * including bearer tokens. Load balancers should generally have no use for
1283
     * these credentials and use of them is heavily discouraged. These must be used
1284
     * <em>very</em> carefully to avoid sending bearer tokens to untrusted servers
1285
     * as the server could then impersonate the client. Generally it is only safe
1286
     * to use these credentials when communicating with the backend.
1287
     *
1288
     * @since 1.35.0
1289
     */
1290
    public ChannelCredentials getUnsafeChannelCredentials() {
1291
      throw new UnsupportedOperationException();
×
1292
    }
1293

1294
    /**
1295
     * Returns the {@link ChannelLogger} for the Channel served by this LoadBalancer.
1296
     *
1297
     * @since 1.17.0
1298
     */
1299
    public ChannelLogger getChannelLogger() {
1300
      throw new UnsupportedOperationException();
×
1301
    }
1302

1303
    /**
1304
     * Returns the {@link NameResolver.Args} that the Channel uses to create {@link NameResolver}s.
1305
     *
1306
     * @since 1.22.0
1307
     */
1308
    public NameResolver.Args getNameResolverArgs() {
1309
      throw new UnsupportedOperationException();
×
1310
    }
1311

1312
    /**
1313
     * Returns the {@link NameResolverRegistry} that the Channel uses to look for {@link
1314
     * NameResolver}s.
1315
     *
1316
     * @since 1.22.0
1317
     */
1318
    public NameResolverRegistry getNameResolverRegistry() {
1319
      throw new UnsupportedOperationException();
×
1320
    }
1321

1322
    /**
1323
     * Returns the {@link MetricRecorder} that the channel uses to record metrics.
1324
     *
1325
     * @since 1.64.0
1326
     */
1327
    @Internal
1328
    public MetricRecorder getMetricRecorder() {
1329
      return new MetricRecorder() {};
×
1330
    }
1331
  }
1332

1333
  /**
1334
   * A logical connection to a server, or a group of equivalent servers represented by an {@link
1335
   * EquivalentAddressGroup}.
1336
   *
1337
   * <p>It maintains at most one physical connection (aka transport) for sending new RPCs, while
1338
   * also keeps track of previous transports that has been shut down but not terminated yet.
1339
   *
1340
   * <p>If there isn't an active transport yet, and an RPC is assigned to the Subchannel, it will
1341
   * create a new transport.  It won't actively create transports otherwise.  {@link
1342
   * #requestConnection requestConnection()} can be used to ask Subchannel to create a transport if
1343
   * there isn't any.
1344
   *
1345
   * <p>{@link #start} must be called prior to calling any other methods, with the exception of
1346
   * {@link #shutdown}, which can be called at any time.
1347
   *
1348
   * @since 1.2.0
1349
   */
1350
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
1351
  public abstract static class Subchannel {
1✔
1352
    /**
1353
     * Starts the Subchannel.  Can only be called once.
1354
     *
1355
     * <p>Must be called prior to any other method on this class, except for {@link #shutdown} which
1356
     * may be called at any time.
1357
     *
1358
     * <p>Must be called from the {@link Helper#getSynchronizationContext Synchronization Context},
1359
     * otherwise it may throw.  See <a href="https://github.com/grpc/grpc-java/issues/5015">
1360
     * #5015</a> for more discussions.
1361
     *
1362
     * @param listener receives state updates for this Subchannel.
1363
     */
1364
    public void start(SubchannelStateListener listener) {
1365
      throw new UnsupportedOperationException("Not implemented");
×
1366
    }
1367

1368
    /**
1369
     * Shuts down the Subchannel.  After this method is called, this Subchannel should no longer
1370
     * be returned by the latest {@link SubchannelPicker picker}, and can be safely discarded.
1371
     *
1372
     * <p>Calling it on an already shut-down Subchannel has no effect.
1373
     *
1374
     * <p>It should be called from the Synchronization Context.  Currently will log a warning if
1375
     * violated.  It will become an exception eventually.  See <a
1376
     * href="https://github.com/grpc/grpc-java/issues/5015">#5015</a> for the background.
1377
     *
1378
     * @since 1.2.0
1379
     */
1380
    public abstract void shutdown();
1381

1382
    /**
1383
     * Asks the Subchannel to create a connection (aka transport), if there isn't an active one.
1384
     *
1385
     * <p>It should be called from the Synchronization Context.  Currently will log a warning if
1386
     * violated.  It will become an exception eventually.  See <a
1387
     * href="https://github.com/grpc/grpc-java/issues/5015">#5015</a> for the background.
1388
     *
1389
     * @since 1.2.0
1390
     */
1391
    public abstract void requestConnection();
1392

1393
    /**
1394
     * Returns the addresses that this Subchannel is bound to.  This can be called only if
1395
     * the Subchannel has only one {@link EquivalentAddressGroup}.  Under the hood it calls
1396
     * {@link #getAllAddresses}.
1397
     *
1398
     * <p>It should be called from the Synchronization Context.  Currently will log a warning if
1399
     * violated.  It will become an exception eventually.  See <a
1400
     * href="https://github.com/grpc/grpc-java/issues/5015">#5015</a> for the background.
1401
     *
1402
     * @throws IllegalStateException if this subchannel has more than one EquivalentAddressGroup.
1403
     *         Use {@link #getAllAddresses} instead
1404
     * @since 1.2.0
1405
     */
1406
    public final EquivalentAddressGroup getAddresses() {
1407
      List<EquivalentAddressGroup> groups = getAllAddresses();
1✔
1408
      Preconditions.checkState(groups != null && groups.size() == 1,
1✔
1409
          "%s does not have exactly one group", groups);
1410
      return groups.get(0);
1✔
1411
    }
1412

1413
    /**
1414
     * Returns the addresses that this Subchannel is bound to. The returned list will not be empty.
1415
     *
1416
     * <p>It should be called from the Synchronization Context.  Currently will log a warning if
1417
     * violated.  It will become an exception eventually.  See <a
1418
     * href="https://github.com/grpc/grpc-java/issues/5015">#5015</a> for the background.
1419
     *
1420
     * @since 1.14.0
1421
     */
1422
    public List<EquivalentAddressGroup> getAllAddresses() {
1423
      throw new UnsupportedOperationException();
×
1424
    }
1425

1426
    /**
1427
     * The same attributes passed to {@link Helper#createSubchannel Helper.createSubchannel()}.
1428
     * LoadBalancer can use it to attach additional information here, e.g., the shard this
1429
     * Subchannel belongs to.
1430
     *
1431
     * @since 1.2.0
1432
     */
1433
    public abstract Attributes getAttributes();
1434

1435
    /**
1436
     * (Internal use only) returns a {@link Channel} that is backed by this Subchannel.  This allows
1437
     * a LoadBalancer to issue its own RPCs for auxiliary purposes, such as health-checking, on
1438
     * already-established connections.  This channel has certain restrictions:
1439
     * <ol>
1440
     *   <li>It can issue RPCs only if the Subchannel is {@code READY}. If {@link
1441
     *   Channel#newCall} is called when the Subchannel is not {@code READY}, the RPC will fail
1442
     *   immediately.</li>
1443
     *   <li>It doesn't support {@link CallOptions#withWaitForReady wait-for-ready} RPCs. Such RPCs
1444
     *   will fail immediately.</li>
1445
     * </ol>
1446
     *
1447
     * <p>RPCs made on this Channel is not counted when determining ManagedChannel's {@link
1448
     * ManagedChannelBuilder#idleTimeout idle mode}.  In other words, they won't prevent
1449
     * ManagedChannel from entering idle mode.
1450
     *
1451
     * <p>Warning: RPCs made on this channel will prevent a shut-down transport from terminating. If
1452
     * you make long-running RPCs, you need to make sure they will finish in time after the
1453
     * Subchannel has transitioned away from {@code READY} state
1454
     * (notified through {@link #handleSubchannelState}).
1455
     *
1456
     * <p>Warning: this is INTERNAL API, is not supposed to be used by external users, and may
1457
     * change without notice. If you think you must use it, please file an issue.
1458
     */
1459
    @Internal
1460
    public Channel asChannel() {
1461
      throw new UnsupportedOperationException();
×
1462
    }
1463

1464
    /**
1465
     * Returns a {@link ChannelLogger} for this Subchannel.
1466
     *
1467
     * @since 1.17.0
1468
     */
1469
    public ChannelLogger getChannelLogger() {
1470
      throw new UnsupportedOperationException();
×
1471
    }
1472

1473
    /**
1474
     * Replaces the existing addresses used with this {@code Subchannel}. If the new and old
1475
     * addresses overlap, the Subchannel can continue using an existing connection.
1476
     *
1477
     * <p>It must be called from the Synchronization Context or will throw.
1478
     *
1479
     * @throws IllegalArgumentException if {@code addrs} is empty
1480
     * @since 1.22.0
1481
     */
1482
    public void updateAddresses(List<EquivalentAddressGroup> addrs) {
1483
      throw new UnsupportedOperationException();
×
1484
    }
1485

1486
    /**
1487
     * (Internal use only) returns an object that represents the underlying subchannel that is used
1488
     * by the Channel for sending RPCs when this {@link Subchannel} is picked.  This is an opaque
1489
     * object that is both provided and consumed by the Channel.  Its type <strong>is not</strong>
1490
     * {@code Subchannel}.
1491
     *
1492
     * <p>Warning: this is INTERNAL API, is not supposed to be used by external users, and may
1493
     * change without notice. If you think you must use it, please file an issue and we can consider
1494
     * removing its "internal" status.
1495
     */
1496
    @Internal
1497
    public Object getInternalSubchannel() {
1498
      throw new UnsupportedOperationException();
×
1499
    }
1500

1501
    /**
1502
     * (Internal use only) returns attributes of the address subchannel is connected to.
1503
     *
1504
     * <p>Warning: this is INTERNAL API, is not supposed to be used by external users, and may
1505
     * change without notice. If you think you must use it, please file an issue and we can consider
1506
     * removing its "internal" status.
1507
     */
1508
    @Internal
1509
    public Attributes getConnectedAddressAttributes() {
1510
      throw new UnsupportedOperationException();
×
1511
    }
1512
  }
1513

1514
  /**
1515
   * Receives state changes for one {@link Subchannel}. All methods are run under {@link
1516
   * Helper#getSynchronizationContext}.
1517
   *
1518
   * @since 1.22.0
1519
   */
1520
  public interface SubchannelStateListener {
1521
    /**
1522
     * Handles a state change on a Subchannel.
1523
     *
1524
     * <p>The initial state of a Subchannel is IDLE. You won't get a notification for the initial
1525
     * IDLE state.
1526
     *
1527
     * <p>If the new state is not SHUTDOWN, this method should create a new picker and call {@link
1528
     * Helper#updateBalancingState Helper.updateBalancingState()}.  Failing to do so may result in
1529
     * unnecessary delays of RPCs. Please refer to {@link PickResult#withSubchannel
1530
     * PickResult.withSubchannel()}'s javadoc for more information.
1531
     *
1532
     * <p>When a subchannel's state is IDLE or TRANSIENT_FAILURE and the address for the subchannel
1533
     * was received in {@link LoadBalancer#handleResolvedAddresses}, load balancers should call
1534
     * {@link Helper#refreshNameResolution} to inform polling name resolvers that it is an
1535
     * appropriate time to refresh the addresses. Without the refresh, changes to the addresses may
1536
     * never be detected.
1537
     *
1538
     * <p>SHUTDOWN can only happen in two cases.  One is that LoadBalancer called {@link
1539
     * Subchannel#shutdown} earlier, thus it should have already discarded this Subchannel.  The
1540
     * other is that Channel is doing a {@link ManagedChannel#shutdownNow forced shutdown} or has
1541
     * already terminated, thus there won't be further requests to LoadBalancer.  Therefore, the
1542
     * LoadBalancer usually don't need to react to a SHUTDOWN state.
1543
     *
1544
     * @param newState the new state
1545
     * @since 1.22.0
1546
     */
1547
    void onSubchannelState(ConnectivityStateInfo newState);
1548
  }
1549

1550
  /**
1551
   * Factory to create {@link LoadBalancer} instance.
1552
   *
1553
   * <p>This class is thread-safe.
1554
   *
1555
   * @since 1.2.0
1556
   */
1557
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
1558
  public abstract static class Factory {
1✔
1559
    /**
1560
     * Creates a {@link LoadBalancer} that will be used inside a channel.
1561
     *
1562
     * @since 1.2.0
1563
     */
1564
    public abstract LoadBalancer newLoadBalancer(Helper helper);
1565
  }
1566

1567
  /**
1568
   * A picker that always returns an erring pick.
1569
   *
1570
   * @deprecated Use {@code new FixedResultPicker(PickResult.withError(error))} instead.
1571
   */
1572
  @Deprecated
1573
  public static final class ErrorPicker extends SubchannelPicker {
1574

1575
    private final Status error;
1576

1577
    public ErrorPicker(Status error) {
×
1578
      this.error = checkNotNull(error, "error");
×
1579
    }
×
1580

1581
    @Override
1582
    public PickResult pickSubchannel(PickSubchannelArgs args) {
1583
      return PickResult.withError(error);
×
1584
    }
1585

1586
    @Override
1587
    public String toString() {
1588
      return MoreObjects.toStringHelper(this)
×
1589
          .add("error", error)
×
1590
          .toString();
×
1591
    }
1592
  }
1593

1594
  /** A picker that always returns the same result. */
1595
  public static final class FixedResultPicker extends SubchannelPicker {
1596
    private final PickResult result;
1597

1598
    public FixedResultPicker(PickResult result) {
1✔
1599
      this.result = Preconditions.checkNotNull(result, "result");
1✔
1600
    }
1✔
1601

1602
    @Override
1603
    public PickResult pickSubchannel(PickSubchannelArgs args) {
1604
      return result;
1✔
1605
    }
1606

1607
    @Override
1608
    public String toString() {
1609
      return "FixedResultPicker(" + result + ")";
1✔
1610
    }
1611

1612
    @Override
1613
    public int hashCode() {
1614
      return result.hashCode();
1✔
1615
    }
1616

1617
    @Override
1618
    public boolean equals(Object o) {
1619
      if (!(o instanceof FixedResultPicker)) {
1✔
1620
        return false;
1✔
1621
      }
1622
      FixedResultPicker that = (FixedResultPicker) o;
1✔
1623
      return this.result.equals(that.result);
1✔
1624
    }
1625
  }
1626
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc