bitflicker64 commented on code in PR #3291:
URL: https://github.com/apache/hugegraph/pull/3291#discussion_r4219046287


##########
hugegraph-store/hg-store-test/src/main/java/org/apache/hugegraph/store/core/store/util/IpUtilTest.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.hugegraph.store.core.store.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hugegraph.store.util.IpUtil;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class IpUtilTest {
+
+    @Test
+    public void testNearestAddressDoesNotPreferLoopback() {
+        String selected = IpUtil.getNearestAddress("127.0.0.2:8510");
+        Assert.assertNotEquals("127.0.0.1:8510", selected);
+        Assert.assertFalse(selected.startsWith("127."));

Review Comment:
   Minor: This assertion depends on the test host having a non-loopback IPv4. 
With only `lo` (for example `docker run --network none`, the case in #3255), 
`getIpAddress()` now returns an empty list, `getNearestAddress` throws "no 
available ipv4" internally and returns the input `127.0.0.2:8510`, so 
`assertFalse(selected.startsWith("127."))` fails. Please add 
`Assume.assumeTrue(...)` for a non-loopback IPv4 on the host, or test the 
selection logic with an injected address list.



##########
hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/util/IpUtil.java:
##########
@@ -47,7 +47,9 @@ private static List<String> getIpAddress() throws 
SocketException {
             Enumeration addresses = network.getInetAddresses();
             while (addresses.hasMoreElements()) {
                 InetAddress address = (InetAddress) addresses.nextElement();
-                if (address != null && (address instanceof Inet4Address)) {
+                if (address instanceof Inet4Address &&
+                    !address.isLoopbackAddress() &&

Review Comment:
   Important: Skipping loopback here changes the result for the shipped default 
config, not only for hostnames. `hg-store-dist/.../conf/application.yml` sets 
`raft.address: 127.0.0.1:8510` and `grpc.host: 127.0.0.1`. Before this PR, 
`127.0.0.1` was in the list and had distance 0, so 
`getNearestAddress("127.0.0.1:8510")` returned it unchanged. I compiled the 
base and head `IpUtil` and ran both on a host with lo0, en0 and a utun 
interface: base returned `127.0.0.1:8510`, head returned `100.82.33.17:8510` (a 
VPN address). `HeartbeatService.registerStore()` sends that value to PD as the 
raft address, but `PartitionEngine` (line 250) still builds its own `PeerId` 
from `options.getRaftAddress()` = `127.0.0.1:8510`. Peer lists come from the 
registered address (`PartitionManager.getPartitionPeers` / `shards2Peers`, used 
at `HgStoreEngine` line 406), and `HgStoreEngine` lines 383-388 skip creating a 
partition engine when the conf does not contain `options.getRaftAddress()`. So 
a default singl
 e-node Store on any machine with a network interface registers an address that 
does not match its raft identity, and the choice depends on interface order 
(Docker bridge, VPN). Please keep the configured address when it is a literal 
that is bound locally, including loopback. For example, return `raftAddress` 
unchanged when `tmp[0]` is in the local address list (collected with loopback), 
and drop loopback only from the fallback candidates used when the configured 
IPv4 is not local. Please also add a test that 
`getNearestAddress("127.0.0.1:8510")` returns `127.0.0.1:8510`.



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

Reply via email to