Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jitter for the subsequent DNS queries #2170

Merged
merged 2 commits into from
Apr 6, 2022
Merged
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
1 change: 1 addition & 0 deletions servicetalk-dns-discovery-netty/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation project(":servicetalk-concurrent-api-internal")
implementation project(":servicetalk-transport-netty")
implementation project(":servicetalk-transport-netty-internal")
implementation project(":servicetalk-utils-internal")
implementation "com.google.code.findbugs:jsr305:$jsr305Version"
implementation "io.netty:netty-resolver-dns"
implementation "org.slf4j:slf4j-api:$slf4jVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021-2022 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,6 +64,7 @@
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.IntFunction;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -117,9 +118,11 @@ final class DefaultDnsClient implements DnsClient {
private final int srvConcurrency;
private final boolean srvFilterDuplicateEvents;
private final boolean inactiveEventsOnError;
private final long ttlJitterNanos;
private boolean closed;

DefaultDnsClient(final IoExecutor ioExecutor, final int minTTL, int srvConcurrency, boolean inactiveEventsOnError,
DefaultDnsClient(final IoExecutor ioExecutor, final int minTTL, final long ttlJitterNanos,
final int srvConcurrency, final boolean inactiveEventsOnError,
final boolean completeOncePreferredResolved, final boolean srvFilterDuplicateEvents,
Duration srvHostNameRepeatInitialDelay, Duration srvHostNameRepeatJitter,
@Nullable Integer maxUdpPayloadSize, @Nullable final Integer ndots,
Expand All @@ -141,6 +144,7 @@ final class DefaultDnsClient implements DnsClient {
srvHostNameRepeatInitialDelay, srvHostNameRepeatJitter, nettyIoExecutor);
this.ttlCache = new MinTtlCache(new DefaultDnsCache(minTTL, Integer.MAX_VALUE, minTTL), minTTL,
nettyIoExecutor);
this.ttlJitterNanos = ttlJitterNanos;
this.observer = observer;
this.missingRecordStatus = missingRecordStatus;
asyncCloseable = toAsyncCloseable(graceful -> {
Expand Down Expand Up @@ -629,12 +633,15 @@ private void cancelAndTerminate0(Throwable cause) {
private void scheduleQuery0(final long nanos) {
assertInEventloop();

LOGGER.trace("DnsClient {}, scheduling DNS query for {} after {} nanos.",
DefaultDnsClient.this, AbstractDnsPublisher.this, nanos);
final long delay = ThreadLocalRandom.current()
.nextLong(nanos, addWithOverflowProtection(nanos, ttlJitterNanos));
LOGGER.debug("DnsClient {}, scheduling DNS query for {} after {}ms, original TTL: {}ms.",
DefaultDnsClient.this, AbstractDnsPublisher.this, NANOSECONDS.toMillis(delay),
NANOSECONDS.toMillis(nanos));

// This value is coming from DNS TTL for which the unit is seconds and the minimum value we accept
// in the builder is 1 second.
cancellableForQuery = nettyIoExecutor.schedule(this::doQuery0, nanos, NANOSECONDS);
cancellableForQuery = nettyIoExecutor.schedule(this::doQuery0, delay, NANOSECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been nice to support retry strategies here too, but can't think of an easy way to do so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SD retries are handled at client level, bcz it needs visibility into SD errors, see SingleAddressHttpClientBuilder#retryServiceDiscoveryErrors(...).
Don't think we need retry schedule, nettyIoExecutor won't reject unless it's closed.

}

private void handleResolveDone0(final Future<DnsAnswer<T>> addressFuture,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021-2022 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import static io.servicetalk.dns.discovery.netty.DnsClients.asHostAndPortDiscoverer;
import static io.servicetalk.dns.discovery.netty.DnsClients.asSrvDiscoverer;
import static io.servicetalk.transport.netty.internal.GlobalExecutionContext.globalExecutionContext;
import static io.servicetalk.utils.internal.DurationUtils.ensurePositive;
import static java.time.Duration.ofSeconds;
import static java.util.Objects.requireNonNull;

Expand All @@ -52,6 +53,7 @@ public final class DefaultDnsServiceDiscovererBuilder {
@Nullable
private Duration queryTimeout;
private int minTTLSeconds = 10;
private Duration ttlJitter = ofSeconds(4);
private int srvConcurrency = 2048;
private boolean inactiveEventsOnError;
private boolean completeOncePreferredResolved = true;
Expand All @@ -78,6 +80,21 @@ public DefaultDnsServiceDiscovererBuilder minTTL(final int minTTLSeconds) {
return this;
}

/**
* The jitter to apply to schedule the next query after TTL.
* <p>
* The jitter value will be added on top of the TTL value returned from the DNS server to help spread out
* subsequent DNS queries.
*
* @param ttlJitter The jitter to apply to schedule the next query after TTL.
* @return {@code this}.
*/
public DefaultDnsServiceDiscovererBuilder ttlJitter(final Duration ttlJitter) {
ensurePositive(ttlJitter, "jitter");
this.ttlJitter = ttlJitter;
return this;
}

/**
* Set the {@link DnsServerAddressStreamProvider} which determines which DNS server should be used per query.
*
Expand Down Expand Up @@ -288,7 +305,8 @@ private static DnsClientFilterFactory appendFilter(@Nullable final DnsClientFilt
*/
DnsClient build() {
final DnsClient rawClient = new DefaultDnsClient(
ioExecutor == null ? globalExecutionContext().ioExecutor() : ioExecutor, minTTLSeconds, srvConcurrency,
ioExecutor == null ? globalExecutionContext().ioExecutor() : ioExecutor, minTTLSeconds,
ttlJitter.toNanos(), srvConcurrency,
inactiveEventsOnError, completeOncePreferredResolved, srvFilterDuplicateEvents,
srvHostNameRepeatInitialDelay, srvHostNameRepeatJitter, maxUdpPayloadSize, ndots, optResourceEnabled,
queryTimeout, dnsResolverAddressTypes, dnsServerAddressStreamProvider, observer, missingRecordStatus);
Expand Down