hangc0276 commented on code in PR #4814:
URL: https://github.com/apache/bookkeeper/pull/4814#discussion_r4761265709


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/net/DNS.java:
##########
@@ -70,19 +70,16 @@ public static String reverseDns(InetAddress hostIp, String 
ns)
             //Not proper address. May be IPv6
             throw new NamingException("IPV6");
         }
+
+        // Loopback addresses are local-only; avoid querying external DNS for 
PTR records.
+        if (hostIp.isLoopbackAddress()) {

Review Comment:
   The change itself is correct, but I could not find the bookie startup path 
described in the motivation reaching it.
   
   In this repo `BookieImpl.getBookieAddress` calls `DNS.getDefaultIP(iface)`, 
and only with `useHostNameAsBookieID` it calls the JDK's 
`InetAddress.getCanonicalHostName()`. Neither goes through `DNS.reverseDns`. 
`reverseDns` is only called by `DNS.getHosts` / `getDefaultHost(iface)` for a 
non-`default` interface, and nothing in main code calls those for a loopback 
address (`ZkLedgerUnderreplicationManager` uses `getDefaultHost("default")`, 
which returns the cached name directly).
   
   I checked it with a test: `BookieImpl.getBookieAddress(conf)` with 
`listeningInterface` set to the loopback interface, `DNS` mocked to count 
`getPtrAttributes` calls, and this short-circuit removed. It makes no PTR query 
(`useHostNameAsBookieID=false`). That test stopped at its first case; the 
`useHostNameAsBookieID=true` case goes through the JDK resolver, which I 
couldn't intercept, so that part is from reading the code. I also couldn't 
reproduce your router timeout.
   
   So the Pulsar `AuditorLedgerCheckerTest` stall may come from somewhere else. 
If you have a `jstack` of the blocked thread, can you check whether it ends in 
`DNS.reverseDns` / JNDI, or in `InetAddress.getCanonicalHostName()`? In the 
second case this PR won't help and the fix belongs in that call.



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/net/DNS.java:
##########
@@ -70,19 +70,16 @@ public static String reverseDns(InetAddress hostIp, String 
ns)
             //Not proper address. May be IPv6
             throw new NamingException("IPV6");
         }
+
+        // Loopback addresses are local-only; avoid querying external DNS for 
PTR records.
+        if (hostIp.isLoopbackAddress()) {
+            return cachedHostname;

Review Comment:
   Small contract change worth documenting. For loopback, `reverseDns` now 
returns the machine's canonical hostname (and ignores the `ns` argument), 
instead of what the nameserver says (for example `localhost.`) or throwing 
`NamingException` when there is no PTR record. The only in-repo consumer, 
`getHosts`, swallows that exception and falls back to the same cached hostname, 
so nothing visible changes there. The Javadoc above `reverseDns` still has the 
dangling line "Loopback addresses"; finishing it as "Loopback addresses are 
answered locally with the cached local hostname; `ns` is not used" would 
document this.



##########
bookkeeper-server/src/test/java/org/apache/bookkeeper/net/DNSTest.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.bookkeeper.net;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+
+import java.net.InetAddress;
+import javax.naming.NamingException;
+import org.junit.Test;
+import org.mockito.MockedStatic;
+
+public class DNSTest {
+
+    @Test
+    public void testReverseDnsForLoopbackAddressDoesNotQueryPtr() throws 
Exception {
+        String expectedHostname = DNS.getDefaultHost("default");

Review Comment:
   Good regression test: I removed the loopback check and it fails ("Naming 
simulated DNS timeout"), and it passes with the PR. Two suggestions, both 
optional:
   - Add a control that a non-loopback address still calls `getPtrAttributes` 
with the reversed name and the given nameserver. I checked `192.0.2.1` with ns 
`10.0.0.53` and it queries `1.2.0.192.in-addr.arpa` as before.
   - Cover more than `127.0.0.1`. I checked `127.0.0.2`, `127.0.1.1` and 
`127.255.255.254` (all skip the query) and that IPv6 `::1` still throws 
`NamingException("IPV6")` because the IPv6 check runs first. I can share these 
tests.



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