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

core: document that NameResolver is not thread-safe. #2918

Closed
Closed
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
6 changes: 5 additions & 1 deletion core/src/main/java/io/grpc/NameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
*
* <p>The addresses and attributes of a target may be changed over time, thus the caller registers a
* {@link Listener} to receive continuous updates.
*
* <p>Thread-safety: {@link #getServiceAuthority} may be called concurrently. Other methods on
* {@link NameResolver} don't need to be thread-safe. {@link Factory} and {@link Listener} are
* thread-safe.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
@ThreadSafe
public abstract class NameResolver {
/**
* Returns the authority used to authenticate connections to servers. It <strong>must</strong> be
Expand Down Expand Up @@ -83,6 +86,7 @@ public abstract class NameResolver {
*/
public void refresh() {}

@ThreadSafe
public abstract static class Factory {
/**
* The port number used in case the target or the underlying naming system doesn't provide a
Expand Down
60 changes: 22 additions & 38 deletions core/src/main/java/io/grpc/internal/DnsNameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

/**
* A DNS-based {@link NameResolver}.
Expand All @@ -65,17 +64,11 @@ class DnsNameResolver extends NameResolver {
private final int port;
private final Resource<ScheduledExecutorService> timerServiceResource;
private final Resource<ExecutorService> executorResource;
@GuardedBy("this")
private boolean shutdown;
@GuardedBy("this")
private ScheduledExecutorService timerService;
@GuardedBy("this")
private ExecutorService executor;
@GuardedBy("this")
private ScheduledFuture<?> resolutionTask;
@GuardedBy("this")
private boolean resolving;
@GuardedBy("this")
private Listener listener;

DnsNameResolver(@Nullable String nsAuthority, String name, Attributes params,
Expand Down Expand Up @@ -111,7 +104,7 @@ public final String getServiceAuthority() {
}

@Override
public final synchronized void start(Listener listener) {
public final void start(Listener listener) {
Preconditions.checkState(this.listener == null, "already started");
timerService = SharedResourceHolder.get(timerServiceResource);
executor = SharedResourceHolder.get(executorResource);
Expand All @@ -120,7 +113,7 @@ public final synchronized void start(Listener listener) {
}

@Override
public final synchronized void refresh() {
public final void refresh() {
Preconditions.checkState(listener != null, "not started");
resolve();
}
Expand All @@ -130,18 +123,16 @@ public final synchronized void refresh() {
public void run() {
InetAddress[] inetAddrs;
Listener savedListener;
synchronized (DnsNameResolver.this) {
// If this task is started by refresh(), there might already be a scheduled task.
if (resolutionTask != null) {
resolutionTask.cancel(false);
resolutionTask = null;
}
if (shutdown) {
return;
}
savedListener = listener;
resolving = true;
// If this task is started by refresh(), there might already be a scheduled task.
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if resolutionRunnable runs on a thread other than the original one it ran on. I don't know if ChannelExecutor is guaranteed to be serial.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, you are right. ChannelExecutor needs to be exposed to NameResolver so that tasks ran in an executor can be serialized with it. I will put this PR on hold until I have time to improve it.

if (resolutionTask != null) {
resolutionTask.cancel(false);
resolutionTask = null;
}
if (shutdown) {
return;
}
savedListener = listener;
resolving = true;
try {
if (System.getenv("GRPC_PROXY_EXP") != null) {
EquivalentAddressGroup server =
Expand All @@ -153,16 +144,14 @@ public void run() {
try {
inetAddrs = getAllByName(host);
} catch (UnknownHostException e) {
synchronized (DnsNameResolver.this) {
if (shutdown) {
return;
}
// Because timerService is the single-threaded GrpcUtil.TIMER_SERVICE in production,
// we need to delegate the blocking work to the executor
resolutionTask =
timerService.schedule(new LogExceptionRunnable(resolutionRunnableOnExecutor),
1, TimeUnit.MINUTES);
if (shutdown) {
return;
}
// Because timerService is the single-threaded GrpcUtil.TIMER_SERVICE in production,
// we need to delegate the blocking work to the executor
resolutionTask =
timerService.schedule(new LogExceptionRunnable(resolutionRunnableOnExecutor),
1, TimeUnit.MINUTES);
savedListener.onError(Status.UNAVAILABLE.withCause(e));
return;
}
Expand All @@ -174,20 +163,16 @@ public void run() {
}
savedListener.onAddresses(servers, Attributes.EMPTY);
} finally {
synchronized (DnsNameResolver.this) {
resolving = false;
}
resolving = false;
}
}
};

private final Runnable resolutionRunnableOnExecutor = new Runnable() {
@Override
public void run() {
synchronized (DnsNameResolver.this) {
if (!shutdown) {
executor.execute(resolutionRunnable);
}
if (!shutdown) {
executor.execute(resolutionRunnable);
}
}
};
Expand All @@ -198,7 +183,6 @@ InetAddress[] getAllByName(String host) throws UnknownHostException {
return InetAddress.getAllByName(host);
}

@GuardedBy("this")
private void resolve() {
if (resolving || shutdown) {
return;
Expand All @@ -207,7 +191,7 @@ private void resolve() {
}

@Override
public final synchronized void shutdown() {
public final void shutdown() {
if (shutdown) {
return;
}
Expand Down