Skip to content

Commit

Permalink
[fix][broker] DnsResolverUtil.TTL should be greater than zero (#18565)
Browse files Browse the repository at this point in the history
(cherry picked from commit 67f9461)
  • Loading branch information
Technoboy- authored and codelipenghui committed Nov 23, 2022
1 parent 9343a35 commit 9118645
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 9118645

Please sign in to comment.