chia7712 commented on code in PR #16991:
URL: https://github.com/apache/kafka/pull/16991#discussion_r1732488674


##########
core/src/test/java/kafka/testkit/TestKitNode.java:
##########
@@ -35,6 +37,12 @@ default Set<String> logDataDirectories() {
         return initialMetaPropertiesEnsemble().logDirProps().keySet();
     }
 
+    default Uuid metadataUuid() {

Review Comment:
   It looks like `directoryId` rather than `metadataUuid`. I means 
`DynamicVoter` use the word `directoryId` :)



##########
core/src/test/java/kafka/testkit/PreboundSocketFactoryManager.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.testkit;
+
+import kafka.server.ServerSocketFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.ServerSocketChannel;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class PreboundSocketFactoryManager implements AutoCloseable {
+    private static final Logger LOG = 
LoggerFactory.getLogger(PreboundSocketFactoryManager.class);
+
+    private class PreboundSocketFactory implements ServerSocketFactory {
+        private final int nodeId;
+
+        private PreboundSocketFactory(int nodeId) {
+            this.nodeId = nodeId;
+        }
+
+        @Override
+        public ServerSocketChannel openServerSocket(
+                String listenerName,
+                InetSocketAddress socketAddress,
+                int listenBacklogSize,
+                int recvBufferSize
+        ) throws IOException {
+            ServerSocketChannel socketChannel = 
getSocketForListenerAndMarkAsUsed(
+                nodeId,
+                listenerName);
+            if (socketChannel != null) {
+                return socketChannel;
+            }
+            return KafkaServerSocketFactory.INSTANCE.openServerSocket(
+                listenerName,
+                socketAddress,
+                listenBacklogSize,
+                recvBufferSize);
+        }
+    }
+
+    /**
+     * True if this manager is closed.
+     */
+    private boolean closed = false;
+
+    /**
+     * Maps node IDs to socket factory objects.
+     * Protected by the object lock.
+     */
+    private final Map<Integer, PreboundSocketFactory> factories = new 
HashMap<>();
+
+    /**
+     * Maps node IDs to maps of listener names to ports.
+     * Protected by the object lock.
+     */
+    private final Map<Integer, Map<String, ServerSocketChannel>> sockets = new 
HashMap<>();
+
+    /**
+     * Maps node IDs to set of the listeners that were used.
+     * Protected by the object lock.
+     */
+    private final Map<Integer, Set<String>> usedSockets = new HashMap<>();
+
+    /**
+     * Get a socket from this manager, mark it as used, and return it.
+     *
+     * @param nodeId        The ID of the node.
+     * @param listener      The listener for the socket.
+     *
+     * @return              null if the socket was not found; the socket, 
otherwise.
+     */
+    public synchronized ServerSocketChannel getSocketForListenerAndMarkAsUsed(
+        int nodeId,
+        String listener
+    ) {
+        Map<String, ServerSocketChannel> socketsForNode = sockets.get(nodeId);
+        if (socketsForNode == null) {
+            return null;
+        }
+        ServerSocketChannel socket = socketsForNode.get(listener);
+        if (socket == null) {
+            return null;
+        }
+        usedSockets.computeIfAbsent(nodeId, __ -> new 
HashSet<>()).add(listener);
+        return socket;
+    }
+
+    /**
+     * Get or create a socket factory object associated with a given node ID.
+     *
+     * @param nodeId        The ID of the node.
+     *
+     * @return              The socket factory.
+     */
+    public synchronized PreboundSocketFactory getOrCreateSocketFactory(int 
nodeId) {

Review Comment:
   Could you please return the interface `ServerSocketFactory` instead? 
`PreboundSocketFactory` is a private class.
   
   Or we can use lambda instead. for example:
   ```java
           return factories.computeIfAbsent(nodeId, __ -> (listenerName, 
socketAddress, listenBacklogSize, recvBufferSize) -> {
               ServerSocketChannel socketChannel = 
getSocketForListenerAndMarkAsUsed(
                       nodeId,
                       listenerName);
               if (socketChannel != null) {
                   return socketChannel;
               }
               return 
ServerSocketFactory.KafkaServerSocketFactory.INSTANCE.openServerSocket(
                       listenerName,
                       socketAddress,
                       listenBacklogSize,
                       recvBufferSize);
           });
   ```



##########
core/src/test/java/kafka/testkit/PreboundSocketFactoryManager.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.testkit;
+
+import kafka.server.ServerSocketFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.ServerSocketChannel;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class PreboundSocketFactoryManager implements AutoCloseable {
+    private static final Logger LOG = 
LoggerFactory.getLogger(PreboundSocketFactoryManager.class);
+
+    private class PreboundSocketFactory implements ServerSocketFactory {
+        private final int nodeId;
+
+        private PreboundSocketFactory(int nodeId) {
+            this.nodeId = nodeId;
+        }
+
+        @Override
+        public ServerSocketChannel openServerSocket(
+                String listenerName,
+                InetSocketAddress socketAddress,
+                int listenBacklogSize,
+                int recvBufferSize
+        ) throws IOException {
+            ServerSocketChannel socketChannel = 
getSocketForListenerAndMarkAsUsed(
+                nodeId,
+                listenerName);
+            if (socketChannel != null) {
+                return socketChannel;
+            }
+            return KafkaServerSocketFactory.INSTANCE.openServerSocket(
+                listenerName,
+                socketAddress,
+                listenBacklogSize,
+                recvBufferSize);
+        }
+    }
+
+    /**
+     * True if this manager is closed.
+     */
+    private boolean closed = false;
+
+    /**
+     * Maps node IDs to socket factory objects.
+     * Protected by the object lock.
+     */
+    private final Map<Integer, PreboundSocketFactory> factories = new 
HashMap<>();
+
+    /**
+     * Maps node IDs to maps of listener names to ports.
+     * Protected by the object lock.
+     */
+    private final Map<Integer, Map<String, ServerSocketChannel>> sockets = new 
HashMap<>();
+
+    /**
+     * Maps node IDs to set of the listeners that were used.
+     * Protected by the object lock.
+     */
+    private final Map<Integer, Set<String>> usedSockets = new HashMap<>();
+
+    /**
+     * Get a socket from this manager, mark it as used, and return it.
+     *
+     * @param nodeId        The ID of the node.
+     * @param listener      The listener for the socket.
+     *
+     * @return              null if the socket was not found; the socket, 
otherwise.
+     */
+    public synchronized ServerSocketChannel getSocketForListenerAndMarkAsUsed(
+        int nodeId,
+        String listener
+    ) {
+        Map<String, ServerSocketChannel> socketsForNode = sockets.get(nodeId);
+        if (socketsForNode == null) {
+            return null;
+        }
+        ServerSocketChannel socket = socketsForNode.get(listener);
+        if (socket == null) {
+            return null;
+        }
+        usedSockets.computeIfAbsent(nodeId, __ -> new 
HashSet<>()).add(listener);
+        return socket;
+    }
+
+    /**
+     * Get or create a socket factory object associated with a given node ID.
+     *
+     * @param nodeId        The ID of the node.
+     *
+     * @return              The socket factory.
+     */
+    public synchronized PreboundSocketFactory getOrCreateSocketFactory(int 
nodeId) {
+        return factories.computeIfAbsent(nodeId, __ -> new 
PreboundSocketFactory(nodeId));
+    }
+
+    /**
+     * Get a specific port number. The port will be created if it does not 
already exist.
+     *
+     * @param nodeId        The ID of the node.
+     * @param listener      The listener for the socket.
+     *
+     * @return              The port number.
+     */
+    public synchronized int getOrCreatePortForListener(
+        int nodeId,
+        String listener
+    ) throws IOException {
+        Map<String, ServerSocketChannel> socketsForNode =
+            sockets.computeIfAbsent(nodeId, __ -> new HashMap<>());
+        ServerSocketChannel socketChannel = socketsForNode.get(listener);
+        if (socketChannel == null) {
+            if (closed) {
+                throw new RuntimeException("Cannot open new socket: manager is 
closed.");
+            }
+            socketChannel = 
ServerSocketFactory.KafkaServerSocketFactory.INSTANCE.openServerSocket(
+                listener,
+                new InetSocketAddress(0),
+                -1,
+                -1);
+            socketsForNode.put(listener, socketChannel);
+        }
+        InetSocketAddress socketAddress = (InetSocketAddress) 
socketChannel.getLocalAddress();
+        return socketAddress.getPort();
+    }
+
+    @Override
+    public synchronized void close() throws Exception {
+        if (closed) {
+            return;
+        }
+        closed = true;
+        // Close all sockets that haven't been used by a SocketServer. (We 
don't want to close the
+        // ones that have been used by a SocketServer because that is the 
responsibility of that
+        // SocketServer.)
+        for (Entry<Integer, Map<String, ServerSocketChannel>> socketsEntry : 
sockets.entrySet()) {
+            Set<String> usedListeners = usedSockets.getOrDefault(
+                socketsEntry.getKey(), Collections.emptySet());
+            for (Entry<String, ServerSocketChannel> entry : 
socketsEntry.getValue().entrySet()) {
+                if (!usedListeners.contains(entry.getKey())) {
+                    try {

Review Comment:
   How about using `Utils.closeQuietly`



##########
metadata/src/main/java/org/apache/kafka/metadata/storage/Formatter.java:
##########
@@ -125,7 +125,7 @@ public class Formatter {
     /**
      * The metadata log directory.
      */
-    private String metadataLogDirectory = null;
+    private Optional<String> metadataLogDirectory = null;

Review Comment:
   Pardon me, why we can't have empty `metadataLogDirectory` (`Optional.empty`) 
by default?



##########
core/src/main/java/kafka/server/ServerSocketFactory.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.server;
+
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.network.Selectable;
+import org.apache.kafka.common.utils.Utils;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketException;
+import java.nio.channels.ServerSocketChannel;
+
+public interface ServerSocketFactory {
+    ServerSocketChannel openServerSocket(
+        String listenerName,
+        InetSocketAddress socketAddress,
+        int listenBacklogSize,
+        int recvBufferSize
+    ) throws IOException;
+
+    class KafkaServerSocketFactory implements ServerSocketFactory {
+        public static final KafkaServerSocketFactory INSTANCE = new 
KafkaServerSocketFactory();
+
+        @Override
+        public ServerSocketChannel openServerSocket(
+                String listenerName,
+                InetSocketAddress socketAddress,
+                int listenBacklogSize,
+                int recvBufferSize
+        ) throws IOException {
+            ServerSocketChannel socketChannel = null;
+            try {
+                socketChannel = ServerSocketChannel.open();

Review Comment:
   Could you please rewrite it by following style to avoid resource leak.
   ```java
               ServerSocketChannel socketChannel = ServerSocketChannel.open();
               try {
                   socketChannel.configureBlocking(false);
   ```
   



##########
core/src/test/java/kafka/testkit/KafkaClusterTestKit.java:
##########
@@ -404,22 +423,44 @@ private void formatNode(
         boolean writeMetadataDirectory
     ) {
         try {
-            MetaPropertiesEnsemble.Copier copier =
-                new 
MetaPropertiesEnsemble.Copier(MetaPropertiesEnsemble.EMPTY);
-            for (Entry<String, MetaProperties> entry : 
ensemble.logDirProps().entrySet()) {
-                String logDir = entry.getKey();
-                if (writeMetadataDirectory || 
(!ensemble.metadataLogDir().equals(Optional.of(logDir)))) {
-                    log.trace("Adding {} to the list of directories to 
format.", logDir);
-                    copier.setLogDirProps(logDir, entry.getValue());
+            Formatter formatter = new Formatter();
+            formatter.setNodeId(ensemble.nodeId().getAsInt());
+            formatter.setClusterId(ensemble.clusterId().get());
+            if (writeMetadataDirectory) {
+                formatter.setDirectories(ensemble.logDirProps().keySet());
+            } else {
+                
formatter.setDirectories(ensemble.logDirProps().keySet().stream().
+                    filter(d -> !ensemble.metadataLogDir().get().equals(d)).
+                    collect(Collectors.toSet()));
+            }
+            if (formatter.directories().isEmpty()) {
+                return;
+            }
+            
formatter.setReleaseVersion(nodes.bootstrapMetadata().metadataVersion());
+            formatter.setFeatureLevel(KRaftVersion.FEATURE_NAME,
+                
nodes.bootstrapMetadata().featureLevel(KRaftVersion.FEATURE_NAME));
+            formatter.setUnstableFeatureVersionsEnabled(true);
+            formatter.setIgnoreFormatted(false);
+            formatter.setControllerListenerName("CONTROLLER");
+            if (writeMetadataDirectory) {
+                
formatter.setMetadataLogDirectory(ensemble.metadataLogDir().get());
+            } else {
+                formatter.setMetadataLogDirectory(Optional.empty());
+            }
+            if 
(nodes.bootstrapMetadata().featureLevel(KRaftVersion.FEATURE_NAME) > 0) {
+                StringBuilder dynamicVotersBuilder = new StringBuilder();
+                String prefix = "";
+                for (TestKitNode controllerNode : 
nodes.controllerNodes().values()) {
+                    int port = socketFactoryManager.
+                        getOrCreatePortForListener(controllerNode.id(), 
"CONTROLLER");
+                    dynamicVotersBuilder.append(prefix);
+                    prefix = ",";
+                    
dynamicVotersBuilder.append(String.format("%d@localhost:%d:%s",

Review Comment:
   Given that conflicts caused by base64 in converting UUID to String, maybe we 
can create `DynamicVoter` directly to avoid possible bugs in the future. for 
example:
   ```java
                   List<DynamicVoter> voters = new ArrayList<>();
                   for (TestKitNode controllerNode : 
nodes.controllerNodes().values()) {
                       int port = socketFactoryManager.
                           getOrCreatePortForListener(controllerNode.id(), 
"CONTROLLER");
                       voters.add(new 
DynamicVoter(controllerNode.metadataUuid(), controllerNode.id(), "localhost", 
port));
                   }
                   formatter.setInitialVoters(new DynamicVoters(voters));
   ```



-- 
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