|
97 | 97 | import org.apache.commons.lang3.StringUtils; |
98 | 98 | import org.apache.commons.lang3.mutable.MutableBoolean; |
99 | 99 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 100 | +import org.apache.commons.lang3.tuple.Pair; |
100 | 101 | import org.apache.pulsar.bookie.rackawareness.IsolatedBookieEnsemblePlacementPolicy; |
101 | 102 | import org.apache.pulsar.broker.PulsarServerException; |
102 | 103 | import org.apache.pulsar.broker.PulsarService; |
@@ -206,7 +207,6 @@ public class BrokerService implements Closeable { |
206 | 207 | private static final TimeoutException FAILED_TO_LOAD_TOPIC_TIMEOUT_EXCEPTION = |
207 | 208 | FutureUtil.createTimeoutException("Failed to load topic within timeout", BrokerService.class, |
208 | 209 | "futureWithDeadline(...)"); |
209 | | - private static final long GRACEFUL_SHUTDOWN_QUIET_PERIOD_MAX_MS = 5000L; |
210 | 210 | private static final double GRACEFUL_SHUTDOWN_QUIET_PERIOD_RATIO_OF_TOTAL_TIMEOUT = 0.25d; |
211 | 211 | private static final double GRACEFUL_SHUTDOWN_TIMEOUT_RATIO_OF_TOTAL_TIMEOUT = 0.5d; |
212 | 212 |
|
@@ -308,7 +308,7 @@ public class BrokerService implements Closeable { |
308 | 308 | // fallback if recover BucketDelayedDeliveryTracker failed. |
309 | 309 | private volatile DelayedDeliveryTrackerFactory fallbackDelayedDeliveryTrackerFactory; |
310 | 310 | private final ServerBootstrap defaultServerBootstrap; |
311 | | - private final List<EventLoopGroup> protocolHandlersWorkerGroups = new ArrayList<>(); |
| 311 | + private final List<Pair<String, EventLoopGroup>> protocolHandlersWorkerGroups = new ArrayList<>(); |
312 | 312 |
|
313 | 313 | @Getter |
314 | 314 | private final BundlesQuotas bundlesQuotas; |
@@ -524,7 +524,7 @@ private void startProtocolHandler(String protocol, |
524 | 524 | EventLoopGroup dedicatedWorkerGroup = |
525 | 525 | EventLoopUtil.newEventLoopGroup(configuration.getNumIOThreads(), false, defaultThreadFactory); |
526 | 526 | bootstrap.channel(EventLoopUtil.getServerSocketChannelClass(dedicatedWorkerGroup)); |
527 | | - protocolHandlersWorkerGroups.add(dedicatedWorkerGroup); |
| 527 | + protocolHandlersWorkerGroups.add(Pair.of(protocol, dedicatedWorkerGroup)); |
528 | 528 | bootstrap.group(this.acceptorGroup, dedicatedWorkerGroup); |
529 | 529 | } else { |
530 | 530 | bootstrap = defaultServerBootstrap.clone(); |
@@ -835,10 +835,10 @@ public CompletableFuture<Void> closeAsync() { |
835 | 835 | CompletableFuture<CompletableFuture<Void>> cancellableDownstreamFutureReference = new CompletableFuture<>(); |
836 | 836 | log.info("Event loops shutting down gracefully..."); |
837 | 837 | List<CompletableFuture<?>> shutdownEventLoops = new ArrayList<>(); |
838 | | - shutdownEventLoops.add(shutdownEventLoopGracefully(acceptorGroup)); |
839 | | - shutdownEventLoops.add(shutdownEventLoopGracefully(workerGroup)); |
840 | | - for (EventLoopGroup group : protocolHandlersWorkerGroups) { |
841 | | - shutdownEventLoops.add(shutdownEventLoopGracefully(group)); |
| 838 | + shutdownEventLoops.add(shutdownEventLoopGracefully("acceptor", acceptorGroup)); |
| 839 | + shutdownEventLoops.add(shutdownEventLoopGracefully("worker", workerGroup)); |
| 840 | + for (final var pair : protocolHandlersWorkerGroups) { |
| 841 | + shutdownEventLoops.add(shutdownEventLoopGracefully(pair.getLeft(), pair.getRight())); |
842 | 842 | } |
843 | 843 |
|
844 | 844 | CompletableFuture<Void> shutdownFuture = |
@@ -933,15 +933,21 @@ public CompletableFuture<Void> closeAsync() { |
933 | 933 | } |
934 | 934 | } |
935 | 935 |
|
936 | | - CompletableFuture<Void> shutdownEventLoopGracefully(EventLoopGroup eventLoopGroup) { |
| 936 | + CompletableFuture<Void> shutdownEventLoopGracefully(String name, EventLoopGroup eventLoopGroup) { |
937 | 937 | long brokerShutdownTimeoutMs = pulsar.getConfiguration().getBrokerShutdownTimeoutMs(); |
938 | | - long quietPeriod = Math.min((long) ( |
939 | | - GRACEFUL_SHUTDOWN_QUIET_PERIOD_RATIO_OF_TOTAL_TIMEOUT * brokerShutdownTimeoutMs), |
940 | | - GRACEFUL_SHUTDOWN_QUIET_PERIOD_MAX_MS); |
941 | 938 | long timeout = (long) (GRACEFUL_SHUTDOWN_TIMEOUT_RATIO_OF_TOTAL_TIMEOUT * brokerShutdownTimeoutMs); |
942 | | - return NettyFutureUtil.toCompletableFutureVoid( |
943 | | - eventLoopGroup.shutdownGracefully(quietPeriod, |
944 | | - timeout, MILLISECONDS)); |
| 939 | + long periodMs = (timeout > 0) ? 1 : 0; |
| 940 | + long startNs = System.nanoTime(); |
| 941 | + return NettyFutureUtil.toCompletableFutureVoid(eventLoopGroup.shutdownGracefully( |
| 942 | + periodMs, timeout, MILLISECONDS) |
| 943 | + ).whenComplete((__, e) -> { |
| 944 | + final var elapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); |
| 945 | + if (e == null) { |
| 946 | + log.info("Event loop {} shut down after {} ms", name, elapsedMs); |
| 947 | + } else { |
| 948 | + log.warn("Failed to shut down event loop {} after {} ms: {}", name, elapsedMs, e.getMessage()); |
| 949 | + } |
| 950 | + }); |
945 | 951 | } |
946 | 952 |
|
947 | 953 | private CompletableFuture<Void> closeChannel(Channel channel) { |
|
0 commit comments