Copilot commented on code in PR #10159: URL: https://github.com/apache/ozone/pull/10159#discussion_r3385088606
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/local/LocalOzoneCluster.java: ########## @@ -0,0 +1,333 @@ +/* + * 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 org.apache.hadoop.ozone.local; + +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_CLIENT_ADDRESS_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_CLIENT_BIND_HOST_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_HTTP_ADDRESS_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_HTTP_BIND_HOST_KEY; +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_CONTAINER_RATIS_ENABLED_KEY; +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_DATANODE_DIR_KEY; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_ADMIN_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATANODE_STORAGE_DIR; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATASTREAM_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_IPC_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_SERVER_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_METADATA_DIRS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION_TYPE; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; +import org.apache.hadoop.ozone.HddsDatanodeService; +import org.apache.hadoop.ozone.container.replication.ReplicationServer; +import org.apache.hadoop.ozone.local.LocalOzoneClusterConfig.FormatMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Starts a local in-process Ozone cluster for development and testing. + * + * <p>This implementation manages the lifecycle of datanodes (and eventually + * SCM, OM, S3G) running within a single JVM process.</p> + */ +public final class LocalOzoneCluster implements LocalOzoneRuntime { + + private static final Logger LOG = + LoggerFactory.getLogger(LocalOzoneCluster.class); + + private static final String[] NO_ARGS = new String[0]; + private static final String PORTS_STATE_FILE = "ports.properties"; + + private final LocalOzoneClusterConfig config; + private final OzoneConfiguration seedConfiguration; + private final AtomicBoolean closed = new AtomicBoolean(); + private final List<HddsDatanodeService> datanodes = new ArrayList<>(); + + private boolean previousMetricsMiniClusterMode; + private boolean metricsMiniClusterModeEnabled; + + /** + * Creates a new local Ozone cluster. + * + * @param config the cluster configuration + * @param seedConfiguration the base Ozone configuration + */ + public LocalOzoneCluster(LocalOzoneClusterConfig config, + OzoneConfiguration seedConfiguration) { + this.config = Objects.requireNonNull(config, "config"); + this.seedConfiguration = new OzoneConfiguration( + Objects.requireNonNull(seedConfiguration, "seedConfiguration")); + } + + @Override + public void start() throws Exception { + enableMiniClusterMetricsMode(); + prepareDataDirectory(); + + OzoneConfiguration baseConf = prepareBaseConfiguration(); + startDatanodes(baseConf); + + // TODO: Add SCM/OM startup and waitForClusterToBeReady() in future tasks + LOG.info("Local Ozone cluster started with {} datanode(s)", + datanodes.size()); Review Comment: The PR description and CLI option `--startup-timeout` indicate startup should wait until the cluster is actually usable, but this implementation starts only DNs and immediately reports success without any readiness check or timeout usage. This can mislead callers into using the cluster before SCM/OM are ready (once added), and the timeout config is currently unused. ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/local/LocalOzoneCluster.java: ########## @@ -0,0 +1,333 @@ +/* + * 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 org.apache.hadoop.ozone.local; + +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_CLIENT_ADDRESS_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_CLIENT_BIND_HOST_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_HTTP_ADDRESS_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_HTTP_BIND_HOST_KEY; +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_CONTAINER_RATIS_ENABLED_KEY; +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_DATANODE_DIR_KEY; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_ADMIN_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATANODE_STORAGE_DIR; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATASTREAM_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_IPC_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_SERVER_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_METADATA_DIRS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION_TYPE; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; +import org.apache.hadoop.ozone.HddsDatanodeService; +import org.apache.hadoop.ozone.container.replication.ReplicationServer; +import org.apache.hadoop.ozone.local.LocalOzoneClusterConfig.FormatMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Starts a local in-process Ozone cluster for development and testing. + * + * <p>This implementation manages the lifecycle of datanodes (and eventually + * SCM, OM, S3G) running within a single JVM process.</p> + */ +public final class LocalOzoneCluster implements LocalOzoneRuntime { + + private static final Logger LOG = + LoggerFactory.getLogger(LocalOzoneCluster.class); + + private static final String[] NO_ARGS = new String[0]; + private static final String PORTS_STATE_FILE = "ports.properties"; + + private final LocalOzoneClusterConfig config; + private final OzoneConfiguration seedConfiguration; + private final AtomicBoolean closed = new AtomicBoolean(); + private final List<HddsDatanodeService> datanodes = new ArrayList<>(); + + private boolean previousMetricsMiniClusterMode; + private boolean metricsMiniClusterModeEnabled; + + /** + * Creates a new local Ozone cluster. + * + * @param config the cluster configuration + * @param seedConfiguration the base Ozone configuration + */ + public LocalOzoneCluster(LocalOzoneClusterConfig config, + OzoneConfiguration seedConfiguration) { + this.config = Objects.requireNonNull(config, "config"); + this.seedConfiguration = new OzoneConfiguration( + Objects.requireNonNull(seedConfiguration, "seedConfiguration")); + } + + @Override + public void start() throws Exception { + enableMiniClusterMetricsMode(); + prepareDataDirectory(); + + OzoneConfiguration baseConf = prepareBaseConfiguration(); + startDatanodes(baseConf); + + // TODO: Add SCM/OM startup and waitForClusterToBeReady() in future tasks + LOG.info("Local Ozone cluster started with {} datanode(s)", + datanodes.size()); + } Review Comment: `start()` can leave partially-started datanodes running and metrics mode enabled if any step throws (eg port persistence write or a later DN start). That can leak threads/resources and make subsequent runs flaky. Wrap startup in a try/catch that attempts `close()` (and suppresses close failures) before rethrowing the original exception. ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/local/LocalOzoneCluster.java: ########## @@ -0,0 +1,333 @@ +/* + * 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 org.apache.hadoop.ozone.local; + +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_CLIENT_ADDRESS_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_CLIENT_BIND_HOST_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_HTTP_ADDRESS_KEY; +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_DATANODE_HTTP_BIND_HOST_KEY; +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_CONTAINER_RATIS_ENABLED_KEY; +import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_DATANODE_DIR_KEY; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_IPC_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_ADMIN_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATANODE_STORAGE_DIR; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATASTREAM_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_IPC_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_CONTAINER_RATIS_SERVER_PORT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_METADATA_DIRS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION_TYPE; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; +import org.apache.hadoop.ozone.HddsDatanodeService; +import org.apache.hadoop.ozone.container.replication.ReplicationServer; +import org.apache.hadoop.ozone.local.LocalOzoneClusterConfig.FormatMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Starts a local in-process Ozone cluster for development and testing. + * + * <p>This implementation manages the lifecycle of datanodes (and eventually + * SCM, OM, S3G) running within a single JVM process.</p> + */ +public final class LocalOzoneCluster implements LocalOzoneRuntime { + + private static final Logger LOG = + LoggerFactory.getLogger(LocalOzoneCluster.class); + + private static final String[] NO_ARGS = new String[0]; + private static final String PORTS_STATE_FILE = "ports.properties"; + + private final LocalOzoneClusterConfig config; + private final OzoneConfiguration seedConfiguration; + private final AtomicBoolean closed = new AtomicBoolean(); + private final List<HddsDatanodeService> datanodes = new ArrayList<>(); + + private boolean previousMetricsMiniClusterMode; + private boolean metricsMiniClusterModeEnabled; + + /** + * Creates a new local Ozone cluster. + * + * @param config the cluster configuration + * @param seedConfiguration the base Ozone configuration + */ + public LocalOzoneCluster(LocalOzoneClusterConfig config, + OzoneConfiguration seedConfiguration) { + this.config = Objects.requireNonNull(config, "config"); + this.seedConfiguration = new OzoneConfiguration( + Objects.requireNonNull(seedConfiguration, "seedConfiguration")); + } + + @Override + public void start() throws Exception { + enableMiniClusterMetricsMode(); + prepareDataDirectory(); + + OzoneConfiguration baseConf = prepareBaseConfiguration(); + startDatanodes(baseConf); + + // TODO: Add SCM/OM startup and waitForClusterToBeReady() in future tasks + LOG.info("Local Ozone cluster started with {} datanode(s)", + datanodes.size()); + } + + @Override + public String getDisplayHost() { + return "0.0.0.0".equals(config.getHost()) + ? LocalOzoneClusterConfig.DEFAULT_HOST + : config.getHost(); + } + + /** + * Returns the number of running datanodes. + */ + public int getDatanodeCount() { + return datanodes.size(); + } + + @Override + public int getScmPort() { + // TODO: Return actual SCM port when SCM is implemented + return -1; + } + + @Override + public int getOmPort() { + // TODO: Return actual OM port when OM is implemented + return -1; + } + + @Override + public int getS3gPort() { + // TODO: Return actual S3G port when S3G is implemented + return -1; + } + + @Override + public String getS3Endpoint() { + // TODO: Return actual S3 endpoint when S3G is implemented + return ""; + } Review Comment: These port/endpoint accessors return placeholder values (-1/empty string), but `LocalOzoneRuntime` documents that they must be usable after `start()` completes. Returning sentinel values can cause callers to proceed with invalid endpoints. Until SCM/OM/S3G are implemented, it is safer to fail fast with an explicit exception (or implement real allocation and return actual ports). ########## hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/local/TestPortAllocator.java: ########## @@ -0,0 +1,99 @@ +/* + * 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 org.apache.hadoop.ozone.local; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link PortAllocator}. + */ +class TestPortAllocator { + + @Test + void reserveWithPreferredPortReturnsPreferred() throws IOException { + PortAllocator allocator = new PortAllocator(); + int port = allocator.reserve(9878); + assertEquals(9878, port); + } + + @Test + void reserveWithZeroAllocatesEphemeralPort() throws IOException { + PortAllocator allocator = new PortAllocator(); + int port = allocator.reserve(0); + assertTrue(port > 0, "Should allocate a valid port"); + assertTrue(port <= 65535, "Port should be in valid range"); + } + + @Test + void reserveAllocatesUniqueEphemeralPorts() throws IOException { + PortAllocator allocator = new PortAllocator(); + Set<Integer> ports = new HashSet<>(); + + for (int i = 0; i < 10; i++) { + int port = allocator.reserve(0); + assertTrue(ports.add(port), + "Port " + port + " was allocated more than once"); + } + } + + @Test + void reserveRejectsDuplicatePreferredPort() throws IOException { + PortAllocator allocator = new PortAllocator(); + allocator.reserve(9878); + + IOException exception = assertThrows(IOException.class, + () -> allocator.reserve(9878)); + assertTrue(exception.getMessage().contains("9878")); + assertTrue(exception.getMessage().contains("more than once")); + } + + @Test + void multipleAllocatorsAreIndependent() throws IOException { + PortAllocator allocator1 = new PortAllocator(); + PortAllocator allocator2 = new PortAllocator(); + + // Both should be able to reserve the same preferred port + // (they track independently, actual port conflict would happen at bind time) + int port1 = allocator1.reserve(9878); + int port2 = allocator2.reserve(9878); + + assertEquals(port1, port2); + } + + @Test + void ephemeralPortsAreDifferentEachTime() throws IOException { + PortAllocator allocator1 = new PortAllocator(); + PortAllocator allocator2 = new PortAllocator(); + + int port1 = allocator1.reserve(0); + int port2 = allocator2.reserve(0); + + // While not guaranteed, ephemeral ports should generally be different + // This test may occasionally fail but validates the allocation mechanism + assertNotEquals(port1, port2, + "Ephemeral ports from different allocators should typically differ"); + } Review Comment: `ephemeralPortsAreDifferentEachTime` is intentionally non-deterministic (the comment notes it may fail). That makes CI flaky because the OS can legitimately reuse the same ephemeral port after the socket closes. Prefer a deterministic assertion (eg that both ports are valid and that allocation succeeds independently) or drop this test. ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/local/PortAllocator.java: ########## @@ -0,0 +1,72 @@ +/* + * 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 org.apache.hadoop.ozone.local; + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.HashSet; +import java.util.Set; + +/** + * Allocates unique ports for local Ozone services. + * + * <p>This allocator ensures that each service gets a unique port, + * either by using a preferred port or by finding a free ephemeral port. + * It tracks all reserved ports to prevent conflicts when multiple + * services are started in the same JVM.</p> + */ +final class PortAllocator { + + private final Set<Integer> reserved = new HashSet<>(); + + /** + * Reserves a port for use by a local service. + * + * @param preferredPort the preferred port to use, or 0 to auto-allocate + * @return the reserved port number + * @throws IOException if the preferred port is already reserved or + * if no free port can be found + */ + int reserve(int preferredPort) throws IOException { + if (preferredPort > 0) { + if (!reserved.add(preferredPort)) { + throw new IOException("Port " + preferredPort + + " is configured more than once."); + } + return preferredPort; + } + + while (true) { + int candidate = findFreePort(); + if (reserved.add(candidate)) { + return candidate; + } + } + } + + /** + * Finds a free ephemeral port by opening and immediately closing + * a server socket. + */ + private static int findFreePort() throws IOException { + try (ServerSocket socket = new ServerSocket(0)) { + socket.setReuseAddress(false); + return socket.getLocalPort(); + } Review Comment: `setReuseAddress(false)` is called after the `ServerSocket(0)` constructor has already bound the socket. If the intent is to ensure SO_REUSEADDR is disabled for the bind, this is likely a no-op; set the option before binding (or drop the call if unnecessary). ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/local/PersistedPorts.java: ########## @@ -0,0 +1,92 @@ +/* + * 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 org.apache.hadoop.ozone.local; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +/** + * Persists allocated ports to enable stable endpoints across restarts. + * + * <p>When a local Ozone cluster starts, it allocates ephemeral ports for + * its services. This class saves those ports to a properties file so that + * subsequent restarts can reuse the same ports, providing stable endpoints + * for clients.</p> + */ +final class PersistedPorts { + + private final Path path; + private final Properties properties = new Properties(); + + private PersistedPorts(Path path) { + this.path = path; + } + + /** + * Loads persisted ports from the specified file. + * + * @param path the path to the ports properties file + * @return a PersistedPorts instance, empty if file doesn't exist + * @throws IOException if reading the file fails + */ + static PersistedPorts load(Path path) throws IOException { + PersistedPorts persistedPorts = new PersistedPorts(path); + if (Files.exists(path)) { + try (InputStream input = Files.newInputStream(path)) { + persistedPorts.properties.load(input); + } + } + return persistedPorts; + } + + /** + * Gets a previously persisted port value. + * + * @param key the port identifier (e.g., "dn.0.container.ipc") + * @return the port number, or 0 if not persisted + */ + int get(String key) { + String value = properties.getProperty(key); + return value == null ? 0 : Integer.parseInt(value); + } Review Comment: `get()` can throw an unchecked `NumberFormatException` if the persisted properties file is corrupted or manually edited (eg non-numeric value). That would fail cluster startup unexpectedly. Consider treating invalid values as "not persisted" (return 0) so the runtime can re-allocate a fresh port. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
