This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch branch-2.11
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-2.11 by this push:
     new 4fbf457eb68 [fix][broker] DnsResolverUtil.TTL should be greater than 
zero (#18565)
4fbf457eb68 is described below

commit 4fbf457eb6832fb030195df34d9e310b74ad172e
Author: Jiwei Guo <[email protected]>
AuthorDate: Tue Nov 22 17:27:46 2022 +0800

    [fix][broker] DnsResolverUtil.TTL should be greater than zero (#18565)
    
    (cherry picked from commit 67f94613f3ccceb89d756b097d756b1439a9b36f)
---
 .../pulsar/common/util/netty/DnsResolverUtil.java  |  8 ++---
 .../pulsar/common/util/netty/DnsResolverTest.java  | 41 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 6 deletions(-)

diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java
 
b/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java
index 8b06dbf36ec..5f1fe5a1ea6 100644
--- 
a/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java
@@ -50,12 +50,8 @@ public class DnsResolverUtil {
                  | IllegalAccessException e) {
             log.warn("Cannot get DNS TTL settings from 
sun.net.InetAddressCachePolicy class", e);
         }
-        TTL = useDefaultTTLWhenSetToForever(ttl, DEFAULT_TTL);
-        NEGATIVE_TTL = useDefaultTTLWhenSetToForever(negativeTtl, 
DEFAULT_NEGATIVE_TTL);
-    }
-
-    private static int useDefaultTTLWhenSetToForever(int ttl, int defaultTtl) {
-        return ttl < 0 ? defaultTtl : ttl;
+        TTL = ttl <= 0 ? DEFAULT_TTL : ttl;
+        NEGATIVE_TTL = negativeTtl < 0 ? DEFAULT_NEGATIVE_TTL : negativeTtl;
     }
 
     private DnsResolverUtil() {
diff --git 
a/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java
 
b/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java
new file mode 100644
index 00000000000..0ccb960e798
--- /dev/null
+++ 
b/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.pulsar.common.util.netty;
+
+import io.netty.channel.EventLoop;
+import io.netty.resolver.dns.DnsNameResolverBuilder;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class DnsResolverTest {
+
+    @Test
+    public void testMaxTtl() {
+        EventLoop eventLoop = Mockito.mock(EventLoop.class);
+        DnsNameResolverBuilder dnsNameResolverBuilder = new 
DnsNameResolverBuilder(eventLoop);
+        DnsResolverUtil.applyJdkDnsCacheSettings(dnsNameResolverBuilder);
+        // If the maxTtl is <=0, it will throw IllegalArgumentException.
+        try {
+            dnsNameResolverBuilder.build();
+        } catch (Exception ex) {
+            Assert.assertFalse(ex instanceof IllegalArgumentException);
+        }
+    }
+}

Reply via email to