From 31eaac20cab2947fa38f37c2e4d13a326e043e41 Mon Sep 17 00:00:00 2001 From: Jiwe Guo Date: Tue, 22 Nov 2022 14:13:45 +0800 Subject: [PATCH 1/3] Fix DNS maxTtl issue. --- .../common/util/netty/DnsResolverUtil.java | 5 ++- .../common/util/netty/DnsResolverTest.java | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java 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 7170067af4818..208e7b387437c 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 @@ -44,13 +44,16 @@ public class DnsResolverUtil { Class inetAddressCachePolicyClass = Class.forName("sun.net.InetAddressCachePolicy"); Method getTTLMethod = inetAddressCachePolicyClass.getMethod("get"); ttl = (Integer) getTTLMethod.invoke(null); + if (ttl <= 0) { + ttl = DEFAULT_TTL; + } Method getNegativeTTLMethod = inetAddressCachePolicyClass.getMethod("getNegative"); negativeTtl = (Integer) getNegativeTTLMethod.invoke(null); } catch (NoSuchMethodException | ClassNotFoundException | InvocationTargetException | IllegalAccessException e) { log.warn("Cannot get DNS TTL settings from sun.net.InetAddressCachePolicy class", e); } - TTL = useDefaultTTLWhenSetToForever(ttl, DEFAULT_TTL); + TTL = ttl; NEGATIVE_TTL = useDefaultTTLWhenSetToForever(negativeTtl, DEFAULT_NEGATIVE_TTL); } 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 0000000000000..0ccb960e79887 --- /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); + } + } +} From 1fea9c48dc29e84e187ec9155c39bf8e0caa172f Mon Sep 17 00:00:00 2001 From: Jiwe Guo Date: Tue, 22 Nov 2022 15:43:36 +0800 Subject: [PATCH 2/3] apply comment. --- .../pulsar/common/util/netty/DnsResolverUtil.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 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 208e7b387437c..266f3697c99d1 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 @@ -44,21 +44,14 @@ public class DnsResolverUtil { Class inetAddressCachePolicyClass = Class.forName("sun.net.InetAddressCachePolicy"); Method getTTLMethod = inetAddressCachePolicyClass.getMethod("get"); ttl = (Integer) getTTLMethod.invoke(null); - if (ttl <= 0) { - ttl = DEFAULT_TTL; - } Method getNegativeTTLMethod = inetAddressCachePolicyClass.getMethod("getNegative"); negativeTtl = (Integer) getNegativeTTLMethod.invoke(null); } catch (NoSuchMethodException | ClassNotFoundException | InvocationTargetException | IllegalAccessException e) { log.warn("Cannot get DNS TTL settings from sun.net.InetAddressCachePolicy class", e); } - TTL = 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_TTL : negativeTtl; } private DnsResolverUtil() { From 5e5a8b44d05956b0487803f1e1433c2edb003f6a Mon Sep 17 00:00:00 2001 From: Jiwe Guo Date: Tue, 22 Nov 2022 16:07:37 +0800 Subject: [PATCH 3/3] apply comment. --- .../org/apache/pulsar/common/util/netty/DnsResolverUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 266f3697c99d1..f49a6453c72b3 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 @@ -51,7 +51,7 @@ public class DnsResolverUtil { log.warn("Cannot get DNS TTL settings from sun.net.InetAddressCachePolicy class", e); } TTL = ttl <= 0 ? DEFAULT_TTL : ttl; - NEGATIVE_TTL = negativeTtl < 0 ? DEFAULT_TTL : negativeTtl; + NEGATIVE_TTL = negativeTtl < 0 ? DEFAULT_NEGATIVE_TTL : negativeTtl; } private DnsResolverUtil() {