FrankYang0529 commented on code in PR #16808: URL: https://github.com/apache/kafka/pull/16808#discussion_r2183230808
########## test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/KafkaClusterTestKit.java: ########## @@ -639,46 +647,146 @@ public MockFaultHandler nonFatalFaultHandler() { return faultHandlerFactory.nonFatalFaultHandler(); } + public void shutdown(boolean keepData) throws Exception { + if (isStarted.compareAndSet(true, false)) { + List<Entry<String, Future<?>>> futureEntries = new ArrayList<>(); + try { + // Note the shutdown order here is chosen to be consistent with + // `KafkaRaftServer`. See comments in that class for an explanation. + for (Entry<Integer, BrokerServer> entry : brokers.entrySet()) { + int brokerId = entry.getKey(); + BrokerServer broker = entry.getValue(); + if (keepData) { + nodeIdToListeners.computeIfAbsent(brokerId, __ -> new HashSet<>()); + Set<String> listeners = nodeIdToListeners.get(brokerId); + broker.socketServer().dataPlaneAcceptors().forEach((endpoint, acceptor) -> { + listeners.add(endpoint.listener() + "://" + endpoint.host() + ":" + acceptor.localPort()); + }); + } + futureEntries.add(new SimpleImmutableEntry<>("broker" + brokerId, + executorService.submit((Runnable) broker::shutdown))); + } + waitForAllFutures(futureEntries); + futureEntries.clear(); + for (Entry<Integer, ControllerServer> entry : controllers.entrySet()) { + int controllerId = entry.getKey(); + ControllerServer controller = entry.getValue(); + if (keepData) { + nodeIdToListeners.computeIfAbsent(controllerId, __ -> new HashSet<>()); + Set<String> listeners = nodeIdToListeners.get(controllerId); + controller.socketServer().dataPlaneAcceptors().forEach((endpoint, acceptor) -> { + listeners.add(endpoint.listener() + "://" + endpoint.host() + ":" + acceptor.localPort()); + }); + } + futureEntries.add(new SimpleImmutableEntry<>("controller" + controllerId, + executorService.submit(controller::shutdown))); + } + waitForAllFutures(futureEntries); + futureEntries.clear(); + } catch (Exception e) { + for (Entry<String, Future<?>> entry : futureEntries) { + entry.getValue().cancel(true); + } + throw e; + } finally { + ThreadUtils.shutdownExecutorServiceQuietly(executorService, 5, TimeUnit.MINUTES); + socketFactoryManager.close(); + } + waitForAllThreads(); + } + } + + public void restart(Map<Integer, Map<String, Object>> perServerOverriddenConfig) throws Exception { Review Comment: @chia7712 I moved some duplicated code to functions `createSharedServerAndController`, `createSharedServerAndBroker`, and `createSharedServer`. Could you take a look when you have time? Thanks. -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org