From 9118645a16e80b75d2f30d7319888b7a52a87b3b Mon Sep 17 00:00:00 2001 From: Jiwei Guo Date: Tue, 22 Nov 2022 17:27:46 +0800 Subject: [PATCH] [fix][broker] DnsResolverUtil.TTL should be greater than zero (#18565) (cherry picked from commit 67f94613f3ccceb89d756b097d756b1439a9b36f) --- .../common/util/netty/DnsResolverUtil.java | 8 +--- .../common/util/netty/DnsResolverTest.java | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) 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 8b06dbf36eca3..5f1fe5a1ea6e1 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 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); + } + } +}