Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,10 @@ public ZooKeeper(ZooKeeperOptions options) throws IOException {
}

ZKClientConfig clientConfig = options.getClientConfig();
this.clientConfig = clientConfig != null ? clientConfig : new ZKClientConfig();
if (clientConfig == null) {
clientConfig = new ZKClientConfig();
}
this.clientConfig = clientConfig;
ConnectStringParser connectStringParser = new ConnectStringParser(connectString);
HostProvider hostProvider;
if (options.getHostProvider() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,18 @@ public DnsSrvHostProvider(final String dnsSrvName, final long randomnessSeed, fi
this.dnsSrvName = dnsSrvName;
this.dnsResolver = dnsResolver;
try {
final ZKClientConfig config = clientConfig == null ? new ZKClientConfig() : clientConfig;
final List<InetSocketAddress> serverAddresses = lookupDnsSrvRecords();
if (serverAddresses.isEmpty()) {
LOG.error("No SRV records found for DNS name: {}", dnsSrvName);
throw new IllegalArgumentException("No SRV records found for DNS name: " + dnsSrvName);
}

this.connectionManager = new HostConnectionManager(serverAddresses, randomnessSeed, clientConfig);
this.connectionManager = new HostConnectionManager(serverAddresses, randomnessSeed, config);
this.previousServerSet = new HashSet<>(serverAddresses);


final long refreshIntervalInSeconds = getRefreshInterval();
final long refreshIntervalInSeconds = getRefreshInterval(config);
if (refreshIntervalInSeconds > 0) {
dnsRefreshTimer = new Timer("DnsSrvRefresh-" + dnsSrvName, true);
dnsRefreshTimer.scheduleAtFixedRate(new TimerTask() {
Expand Down Expand Up @@ -199,9 +200,9 @@ public void close() {
}
}

private long getRefreshInterval() {
private long getRefreshInterval(final ZKClientConfig clientConfig) {
final long defaultInterval = ZKClientConfig.DNS_SRV_REFRESH_INTERVAL_SECONDS_DEFAULT;
final long interval = Long.getLong(ZKClientConfig.DNS_SRV_REFRESH_INTERVAL_SECONDS, defaultInterval);
final long interval = clientConfig.getLong(ZKClientConfig.DNS_SRV_REFRESH_INTERVAL_SECONDS, defaultInterval);
if (interval < 0) {
LOG.error("Invalid DNS SRV refresh interval {} seconds", interval);
throw new IllegalArgumentException("Invalid DNS SRV refresh interval: " + interval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -194,6 +195,34 @@ public void testRefreshIntervalNegative() throws Exception {
assertEquals("Invalid DNS SRV refresh interval: -1", exception.getMessage());
}

@Test
public void testRefreshIntervalFromClientConfig() throws Exception {
final ZKClientConfig clientConfig = new ZKClientConfig();
clientConfig.setProperty(ZKClientConfig.DNS_SRV_REFRESH_INTERVAL_SECONDS, "1");

final SRVRecord[] srvRecords = createMockSrvRecords();
when(mockDnsSrvResolver.lookupSrvRecords(TEST_DNS_NAME)).thenReturn(srvRecords);

try (final DnsSrvHostProvider hostProvider = new DnsSrvHostProvider(TEST_DNS_NAME, TEST_SEED, mockDnsSrvResolver, clientConfig)) {
// initial lookup plus at least one periodic refresh
verify(mockDnsSrvResolver, timeout(5000).atLeast(2)).lookupSrvRecords(TEST_DNS_NAME);
}
}

@Test
public void testRefreshIntervalNegativeInClientConfig() throws Exception {
final ZKClientConfig clientConfig = new ZKClientConfig();
clientConfig.setProperty(ZKClientConfig.DNS_SRV_REFRESH_INTERVAL_SECONDS, "-1");

final SRVRecord[] srvRecords = createMockSrvRecords();
when(mockDnsSrvResolver.lookupSrvRecords(TEST_DNS_NAME)).thenReturn(srvRecords);

final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> new DnsSrvHostProvider(TEST_DNS_NAME, TEST_SEED, mockDnsSrvResolver, clientConfig));

assertEquals("Invalid DNS SRV refresh interval: -1", exception.getMessage());
}

private SRVRecord[] createMockSrvRecords() {
return new SRVRecord[]{
createMockSrvRecord("server1.example.com.", 2181),
Expand Down
Loading