4242import java .util .logging .Level ;
4343import java .util .logging .Logger ;
4444import javax .annotation .Nullable ;
45+ import org .checkerframework .checker .units .qual .A ;
4546
4647/**
4748 * A {@link ManagedChannel} that will send requests round-robin via a set of channels.
@@ -61,6 +62,7 @@ public class BigtableChannelPool extends ManagedChannel {
6162 private final BigtableChannelPoolSettings settings ;
6263 private final ChannelFactory channelFactory ;
6364 private final ScheduledExecutorService executor ;
65+ private final ScheduledExecutorService channelHealthProbingExecutor = Executors .newSingleThreadScheduledExecutor ();
6466
6567 private final Object entryWriteLock = new Object ();
6668 @ VisibleForTesting final AtomicReference <ImmutableList <Entry >> entries = new AtomicReference <>();
@@ -94,7 +96,7 @@ public static BigtableChannelPool create(
9496 ImmutableList .Builder <Entry > initialListBuilder = ImmutableList .builder ();
9597
9698 for (int i = 0 ; i < settings .getInitialChannelCount (); i ++) {
97- initialListBuilder .add (new Entry (channelFactory .createSingleChannel ()));
99+ initialListBuilder .add (new Entry (channelFactory .createSingleChannel (), channelHealthProbingExecutor ));
98100 }
99101
100102 entries .set (initialListBuilder .build ());
@@ -152,6 +154,9 @@ public ManagedChannel shutdown() {
152154 // shutdownNow will cancel scheduled tasks
153155 executor .shutdownNow ();
154156 }
157+ if (channelHealthProbingExecutor != null ){
158+ channelHealthProbingExecutor .shutdownNow ();
159+ }
155160 return this ;
156161 }
157162
@@ -192,6 +197,9 @@ public ManagedChannel shutdownNow() {
192197 if (executor != null ) {
193198 executor .shutdownNow ();
194199 }
200+ if (channelHealthProbingExecutor != null ) {
201+ channelHealthProbingExecutor .shutdownNow ();
202+ }
195203 return this ;
196204 }
197205
@@ -318,7 +326,7 @@ private void expand(int desiredSize) {
318326
319327 for (int i = 0 ; i < desiredSize - localEntries .size (); i ++) {
320328 try {
321- newEntries .add (new Entry (channelFactory .createSingleChannel ()));
329+ newEntries .add (new Entry (channelFactory .createSingleChannel (), channelHealthProbingExecutor ));
322330 } catch (IOException e ) {
323331 LOG .log (Level .WARNING , "Failed to add channel" , e );
324332 }
@@ -356,7 +364,7 @@ void refresh() {
356364
357365 for (int i = 0 ; i < newEntries .size (); i ++) {
358366 try {
359- newEntries .set (i , new Entry (channelFactory .createSingleChannel ()));
367+ newEntries .set (i , new Entry (channelFactory .createSingleChannel (), channelHealthProbingExecutor ));
360368 } catch (IOException e ) {
361369 LOG .log (Level .WARNING , "Failed to refresh channel, leaving old channel" , e );
362370 }
@@ -433,19 +441,40 @@ static class Entry {
433441
434442 private final AtomicInteger maxOutstanding = new AtomicInteger ();
435443
444+ /** Number of probes in flight plus number of probe results. (No-op stub) */
445+ AtomicInteger recentProbesSent () {
446+ return new AtomicInteger (0 );
447+ }
448+
449+ /** Number of recently failed probes. (No-op stub) */
450+ AtomicInteger recentlyFailedProbes () {
451+ return new AtomicInteger (0 );
452+ }
453+
454+ private final AtomicInteger probesInFlight = new AtomicInteger (0 );
455+
456+
436457 // Flag that the channel should be closed once all of the outstanding RPC complete.
437458 private final AtomicBoolean shutdownRequested = new AtomicBoolean ();
438459 // Flag that the channel has been closed.
439460 private final AtomicBoolean shutdownInitiated = new AtomicBoolean ();
461+ private final ChannelHealthChecker healthChecker ;
440462
441- private Entry (ManagedChannel channel ) {
463+ private Entry (ManagedChannel channel , ScheduledExecutorService executor ) {
442464 this .channel = channel ;
465+ this .healthChecker = new ChannelHealthChecker (this , executor );
443466 }
444467
445468 ManagedChannel getManagedChannel () {
446469 return this .channel ;
447470 }
448471
472+ // Add a getter for the healthChecker
473+ public ChannelHealthChecker getHealthChecker () {
474+ return healthChecker ;
475+ }
476+
477+
449478 int getAndResetMaxOutstanding () {
450479 return maxOutstanding .getAndSet (outstandingRpcs .get ());
451480 }
@@ -460,7 +489,7 @@ private boolean retain() {
460489 // register desire to start RPC
461490 int currentOutstanding = outstandingRpcs .incrementAndGet ();
462491
463- // Rough book keeping
492+ // Rough bookkeeping
464493 int prevMax = maxOutstanding .get ();
465494 if (currentOutstanding > prevMax ) {
466495 maxOutstanding .incrementAndGet ();
@@ -497,6 +526,9 @@ private void release() {
497526 */
498527 private void requestShutdown () {
499528 shutdownRequested .set (true );
529+ if (healthChecker != null ) {
530+ healthChecker .stop ();
531+ }
500532 if (outstandingRpcs .get () == 0 ) {
501533 shutdown ();
502534 }
0 commit comments