szetszwo commented on code in PR #1686:
URL: https://github.com/apache/ratis/pull/1686#discussion_r4499524626


##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcUtil.java:
##########
@@ -236,138 +234,66 @@ Metadata build() {
     }
   }
 
-  /**
-   * Tries to gracefully shut down the managed channel. Falls back to forceful 
shutdown if
-   * graceful shutdown times out.
-   */
-  static void shutdownManagedChannel(ManagedChannel managedChannel) {
-    shutdownManagedChannel(managedChannel, 
GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_GRACEFUL_TIMEOUT_DEFAULT,
+  /** Shut down the given channel using the default timeouts. */
+  static void shutdownManagedChannel(ManagedChannel channel) {
+    shutdownManagedChannel(channel,
+        GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_GRACEFUL_TIMEOUT_DEFAULT,
         GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_FORCEFUL_TIMEOUT_DEFAULT);
   }
 
-  /**
-   * Shut down the given channel gracefully, waiting up to the graceful 
timeout for its calls to finish,
-   * and then forcefully, cancelling the remaining calls, if it has not 
terminated.
-   *
-   * @param gracefulTimeout zero skips the graceful shutdown.
-   */
-  static void shutdownManagedChannel(ManagedChannel managedChannel,
+  /** The same as shutdownManagedChannels(Collections.singleton(channel), 
gracefulTimeout, forcefulTimeout). */
+  static void shutdownManagedChannel(ManagedChannel channel,
       TimeDuration gracefulTimeout, TimeDuration forcefulTimeout) {
-    // Close the gRPC managed-channel if not shut down already.
-    if (!managedChannel.isShutdown() && gracefulTimeout.isPositive()) {
-      try {
-        managedChannel.shutdown();
-        if (!managedChannel.awaitTermination(gracefulTimeout.getDuration(), 
gracefulTimeout.getUnit())) {
-          LOG.warn("Timed out gracefully shutting down connection: {}. ", 
managedChannel);
-        }
-      } catch (InterruptedException e) {
-        Thread.currentThread().interrupt();
-      } catch (Exception e) {
-        LOG.error("Unexpected exception while waiting for channel 
termination", e);
-      }
-    }
+    shutdownManagedChannels(Collections.singleton(channel), gracefulTimeout, 
forcefulTimeout);
+  }
 
-    // Forceful shut down if still not terminated.
-    if (!managedChannel.isTerminated()) {
-      try {
-        managedChannel.shutdownNow();
-        if (!managedChannel.awaitTermination(forcefulTimeout.getDuration(), 
forcefulTimeout.getUnit())) {
-          LOG.warn("Timed out forcefully shutting down connection: {}. ", 
managedChannel);
-        }
-      } catch (InterruptedException e) {
-        Thread.currentThread().interrupt();
-      } catch (Exception e) {
-        LOG.error("Unexpected exception while waiting for channel 
termination", e);
-      }
-    }
+  /** Shut down the given channels using the default timeouts. */
+  static void shutdownManagedChannels(Collection<ManagedChannel> channels) {
+    shutdownManagedChannels(channels,
+        GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_GRACEFUL_TIMEOUT_DEFAULT,
+        GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_FORCEFUL_TIMEOUT_DEFAULT);
   }
 
   /**
-   * Shut down the given channels as {@link 
#shutdownManagedChannel(ManagedChannel)} does, but in parallel:
-   * all of them are shut down before any is awaited, so the total wait is 
bounded by one graceful and one forceful
-   * timeout instead of one of each per channel.
+   * Shut down the given channels using the given timeouts.
+   * It first tries {@link ManagedChannel#shutdown()} and then {@link 
ManagedChannel#shutdownNow()}.
+   *
+   * @param gracefulTimeout the wait time after {@link 
ManagedChannel#shutdown()}; non-positive skips this step.
+   * @param forcefulTimeout the wait time after {@link 
ManagedChannel#shutdownNow()}.
    */
-  static void shutdownManagedChannels(Collection<ManagedChannel> channels) {
-    // Close the gRPC managed-channels if not shut down already.
-    final List<ManagedChannel> open = channels.stream().filter(c -> 
!c.isShutdown()).collect(Collectors.toList());
-    open.forEach(ManagedChannel::shutdown);
-    awaitTermination(open, 
GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_GRACEFUL_TIMEOUT_DEFAULT, "gracefully");
-
-    // Forceful shut down if still not terminated.
-    final List<ManagedChannel> remaining = channels.stream().filter(c -> 
!c.isTerminated())
-        .collect(Collectors.toList());
-    remaining.forEach(ManagedChannel::shutdownNow);
-    awaitTermination(remaining, 
GrpcConfigKeys.Client.CHANNEL_SHUTDOWN_FORCEFUL_TIMEOUT_DEFAULT, "forcefully");
+  static void shutdownManagedChannels(Collection<ManagedChannel> channels,
+      TimeDuration gracefulTimeout, TimeDuration forcefulTimeout) {
+    if (gracefulTimeout.isPositive()) {
+      // Graceful shutdown
+      acceptEach(channels, ManagedChannel::shutdown, "shutdown", LOG);
+      awaitTermination(channels, gracefulTimeout, "shutdown");
+    }
+
+    // Forceful shutdown
+    acceptEach(channels, ManagedChannel::shutdownNow, "shutdownNow", LOG);
+    awaitTermination(channels, forcefulTimeout, "shutdownNow");
   }
 
   /** Wait for the given channels to terminate, up to the given timeout for 
all of them together. */
-  static void awaitTermination(List<ManagedChannel> channels, TimeDuration 
timeout, String mode) {
+  static void awaitTermination(Collection<ManagedChannel> channels, 
TimeDuration timeout, String op) {
+    if (Thread.currentThread().isInterrupted()) {
+      return;
+    }
     final long deadline = System.nanoTime() + 
timeout.toLong(TimeUnit.NANOSECONDS);
     for (ManagedChannel channel : channels) {
       try {
         if (!channel.awaitTermination(Math.max(0, deadline - 
System.nanoTime()), TimeUnit.NANOSECONDS)) {
-          LOG.warn("Timed out {} shutting down connection: {}. ", mode, 
channel);
+          LOG.warn("Timed out ({}) waiting for {} of channel: {}", timeout, 
op, channel);
         }
       } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
         return;
       } catch (Exception e) {
-        LOG.error("Unexpected exception while waiting for channel 
termination", e);
+        LOG.error("Failed to awaitTermination for channel {}", channel, e);
       }
     }
   }
 
-  static SslContextBuilder initSslContextBuilderForServer(KeyManagerConf 
keyManagerConfig) {

Review Comment:
   These code are duplicated in NettyUtils.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to