Skip to content

Commit 6e269f8

Browse files
committed
Review feedback
- Do not change the public API - Use a hacky internal callback - More unit test coverage
1 parent 55962ba commit 6e269f8

13 files changed

+379
-163
lines changed

api/src/main/java/io/grpc/NameResolver.java

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@
6060
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
6161
public abstract class NameResolver {
6262

63-
// Outside listeners that get notified of the result of each name resolution.
64-
private ArrayList<ResolutionResultListener> resolutionResultListeners = new ArrayList<>();
65-
6663
/**
6764
* Returns the authority used to authenticate connections to servers. It <strong>must</strong> be
6865
* from a trusted source, because if the authority is tampered with, RPCs may be sent to the
@@ -95,9 +92,8 @@ public void onError(Status error) {
9592
}
9693

9794
@Override
98-
public boolean onResult(ResolutionResult resolutionResult) {
95+
public void onResult(ResolutionResult resolutionResult) {
9996
listener.onAddresses(resolutionResult.getAddresses(), resolutionResult.getAttributes());
100-
return true;
10197
}
10298
});
10399
}
@@ -136,43 +132,6 @@ public void start(Listener2 listener) {
136132
*/
137133
public void refresh() {}
138134

139-
/**
140-
* Adds a new {@link ResolutionResultListener} that will get notified of the outcome of each
141-
* resolution.
142-
*
143-
* @since 1.53.0
144-
*/
145-
public final void addResolutionResultListener(ResolutionResultListener listener) {
146-
checkArgument(listener != null, "listener");
147-
resolutionResultListeners.add(listener);
148-
}
149-
150-
/**
151-
* Removes an existing {@link ResolutionResultListener}.
152-
*
153-
* @return {@code true} if the listener was removed, otherwise {@code false}
154-
* @since 1.53.0
155-
*/
156-
public final boolean removeResolutionResultListener(ResolutionResultListener listener) {
157-
checkArgument(listener != null);
158-
return resolutionResultListeners.remove(listener);
159-
}
160-
161-
/**
162-
* Intended for extending classes to call when they know the result of a name resolution.
163-
*
164-
* <p>Note that while these listeners can be added to any {@link NameResolver}, only concrete
165-
* implementations that call this method will actually support this facility.
166-
*
167-
* @param successful {@code true} if resolution was successful and the addresses were accepted.
168-
* @since 1.53.0
169-
*/
170-
protected final void fireResolutionResultEvent(boolean successful) {
171-
for (ResolutionResultListener listener : resolutionResultListeners) {
172-
listener.resolutionAttempted(successful);
173-
}
174-
}
175-
176135
/**
177136
* Factory that creates {@link NameResolver} instances.
178137
*
@@ -267,12 +226,9 @@ public final void onAddresses(
267226
* {@link ResolutionResult#getAddresses()} is empty, {@link #onError(Status)} will be called.
268227
*
269228
* @param resolutionResult the resolved server addresses, attributes, and Service Config.
270-
* @return {@code true} if the listener accepts the resolved addresses, otherwise {@code false}.
271-
* If the addresses are not accepted the {@link NameResolver} will refresh and retry
272-
* later if it uses polling.
273229
* @since 1.21.0
274230
*/
275-
public abstract boolean onResult(ResolutionResult resolutionResult);
231+
public abstract void onResult(ResolutionResult resolutionResult);
276232

277233
/**
278234
* Handles a name resolving error from the resolver. The listener is responsible for eventually
@@ -294,24 +250,6 @@ public final void onAddresses(
294250
@Documented
295251
public @interface ResolutionResultAttr {}
296252

297-
298-
/**
299-
* A callback interface called at the end of every resolve operation to indicate if the operation
300-
* was successful. Success means that there were no problems with either the name resolution part
301-
* nor with {@link Listener} accepting the resolution results.
302-
*/
303-
public interface ResolutionResultListener {
304-
305-
/**
306-
* Called after an attempt at name resolution.
307-
*
308-
* <p>Note! Implementations of this should return quickly and not throw exceptions.
309-
*
310-
* @param successful {@code true} if resolution was successful and the addresses were accepted.
311-
*/
312-
void resolutionAttempted(boolean successful);
313-
}
314-
315253
/**
316254
* Information that a {@link Factory} uses to create a {@link NameResolver}.
317255
*
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2023 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.internal;
18+
19+
import io.grpc.SynchronizationContext;
20+
import io.grpc.SynchronizationContext.ScheduledHandle;
21+
import java.util.concurrent.ScheduledExecutorService;
22+
import java.util.concurrent.TimeUnit;
23+
import java.util.logging.Logger;
24+
25+
/**
26+
* Schedules a retry operation according to a {@link BackoffPolicy}. The retry is run within a
27+
* {@link SynchronizationContext}. At most one retry is scheduled at a time.
28+
*/
29+
final class BackoffPolicyRetryScheduler implements RetryScheduler {
30+
private final ScheduledExecutorService scheduledExecutorService;
31+
private final SynchronizationContext syncContext;
32+
private final BackoffPolicy.Provider policyProvider;
33+
34+
private BackoffPolicy policy;
35+
private ScheduledHandle scheduledHandle;
36+
37+
private static final Logger logger = Logger.getLogger(
38+
BackoffPolicyRetryScheduler.class.getName());
39+
40+
BackoffPolicyRetryScheduler(BackoffPolicy.Provider policyProvider,
41+
ScheduledExecutorService scheduledExecutorService,
42+
SynchronizationContext syncContext) {
43+
this.policyProvider = policyProvider;
44+
this.scheduledExecutorService = scheduledExecutorService;
45+
this.syncContext = syncContext;
46+
}
47+
48+
/**
49+
* Schedules a future retry operation. Only allows one retry to be scheduled at any given time.
50+
*
51+
* @return The delay in nanos before the operation fires or -1 if it was not scheduled.
52+
*/
53+
@Override
54+
public long schedule(Runnable retryOperation) {
55+
if (policy == null) {
56+
policy = policyProvider.get();
57+
}
58+
// If a retry is already scheduled, take no further action.
59+
if (scheduledHandle != null && scheduledHandle.isPending()) {
60+
return -1;
61+
}
62+
long delayNanos = policy.nextBackoffNanos();
63+
scheduledHandle = syncContext.schedule(retryOperation, delayNanos, TimeUnit.NANOSECONDS,
64+
scheduledExecutorService);
65+
logger.fine("Scheduling DNS resolution backoff for " + delayNanos + "ns");
66+
67+
return delayNanos;
68+
}
69+
70+
/**
71+
* Resets the {@link BackoffPolicyRetryScheduler} and cancels any pending retry task. The policy
72+
* will be cleared thus also resetting any state associated with it (e.g. a backoff multiplier).
73+
*/
74+
@Override
75+
public void reset() {
76+
if (scheduledHandle != null && scheduledHandle.isPending()) {
77+
scheduledHandle.cancel();
78+
}
79+
policy = null;
80+
}
81+
82+
}

core/src/main/java/io/grpc/internal/DnsNameResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ public void run() {
318318
result = doResolve(false);
319319
if (result.error != null) {
320320
savedListener.onError(result.error);
321-
fireResolutionResultEvent(false);
322321
return;
323322
}
324323
if (result.addresses != null) {
@@ -331,7 +330,7 @@ public void run() {
331330
resolutionResultBuilder.setAttributes(result.attributes);
332331
}
333332
}
334-
fireResolutionResultEvent(savedListener.onResult(resolutionResultBuilder.build()));
333+
savedListener.onResult(resolutionResultBuilder.build());
335334
} catch (IOException e) {
336335
savedListener.onError(
337336
Status.UNAVAILABLE.withDescription("Unable to resolve host " + host).withCause(e));

core/src/main/java/io/grpc/internal/DnsNameResolverProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
6161
GrpcUtil.SHARED_CHANNEL_EXECUTOR,
6262
Stopwatch.createUnstarted(),
6363
InternalServiceProviders.isAndroid(getClass().getClassLoader())),
64-
new ExponentialBackoffPolicy.Provider(),
65-
args.getScheduledExecutorService(),
66-
args.getSynchronizationContext());
64+
new BackoffPolicyRetryScheduler(
65+
new ExponentialBackoffPolicy.Provider(),
66+
args.getScheduledExecutorService(),
67+
args.getSynchronizationContext()));
6768
} else {
6869
return null;
6970
}

core/src/main/java/io/grpc/internal/ManagedChannelImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import io.grpc.internal.ManagedChannelServiceConfig.ServiceConfigConvertedSelector;
8686
import io.grpc.internal.RetriableStream.ChannelBufferMeter;
8787
import io.grpc.internal.RetriableStream.Throttle;
88+
import io.grpc.internal.RetryingNameResolver.ResolutionResultListener;
8889
import java.net.URI;
8990
import java.net.URISyntaxException;
9091
import java.util.ArrayList;
@@ -1697,7 +1698,7 @@ public ChannelCredentials withoutBearerTokens() {
16971698
}
16981699
}
16991700

1700-
private final class NameResolverListener extends NameResolver.Listener2 {
1701+
final class NameResolverListener extends NameResolver.Listener2 {
17011702
final LbHelperImpl helper;
17021703
final NameResolver resolver;
17031704

@@ -1707,7 +1708,7 @@ private final class NameResolverListener extends NameResolver.Listener2 {
17071708
}
17081709

17091710
@Override
1710-
public boolean onResult(final ResolutionResult resolutionResult) {
1711+
public void onResult(final ResolutionResult resolutionResult) {
17111712
final class NamesResolved implements Runnable {
17121713

17131714
@SuppressWarnings("ReferenceEquality")
@@ -1848,7 +1849,15 @@ public void run() {
18481849
if (lastAddressesAccepted == null) {
18491850
lastAddressesAccepted = false;
18501851
}
1851-
return lastAddressesAccepted;
1852+
1853+
// If a listener is provided, let it know if the addresses were accepted.
1854+
// TODO(tmwilson): Once we are ready to change the onResult() API and return a boolean
1855+
// this hacky callback in an attribute approach can be removed.
1856+
ResolutionResultListener resolutionResultListener = resolutionResult.getAttributes()
1857+
.get(RetryingNameResolver.RESOLUTION_RESULT_LISTENER_KEY);
1858+
if (resolutionResultListener != null) {
1859+
resolutionResultListener.resolutionAttempted(lastAddressesAccepted);
1860+
}
18521861
}
18531862

18541863
@Override
Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 The gRPC Authors
2+
* Copyright 2023 The gRPC Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,65 +16,22 @@
1616

1717
package io.grpc.internal;
1818

19-
import io.grpc.SynchronizationContext;
20-
import io.grpc.SynchronizationContext.ScheduledHandle;
21-
import java.util.concurrent.ScheduledExecutorService;
22-
import java.util.concurrent.TimeUnit;
23-
import java.util.logging.Logger;
24-
2519
/**
26-
* Schedules a retry operation according to a {@link BackoffPolicy}. The retry is run within a
27-
* {@link SynchronizationContext}. At most one retry is scheduled at a time.
20+
* This interface is used to schedule future retry attempts for a failed operation. The retry delay
21+
* and the number of attempt is defined by implementing classes. Implementations should assure
22+
* that only one future retry operation is ever scheduled at a time.
2823
*/
29-
final class RetryScheduler {
30-
private final Runnable retryOperation;
31-
private final ScheduledExecutorService scheduledExecutorService;
32-
private final SynchronizationContext syncContext;
33-
private final BackoffPolicy.Provider policyProvider;
34-
35-
private BackoffPolicy policy;
36-
private ScheduledHandle scheduledHandle;
37-
38-
private static final Logger logger = Logger.getLogger(RetryScheduler.class.getName());
39-
40-
RetryScheduler(Runnable retryOperation, ScheduledExecutorService scheduledExecutorService,
41-
SynchronizationContext syncContext, BackoffPolicy.Provider policyProvider) {
42-
this.retryOperation = retryOperation;
43-
this.scheduledExecutorService = scheduledExecutorService;
44-
this.syncContext = syncContext;
45-
this.policyProvider = policyProvider;
46-
}
24+
public interface RetryScheduler {
4725

4826
/**
49-
* Schedules a future retry operation. Only allows one retry to be scheduled at any given time.
27+
* A request to schedule a future retry (or retries) for a failed operation.
5028
*
5129
* @return The delay in nanos before the operation fires or -1 if it was not scheduled.
5230
*/
53-
long schedule() {
54-
if (policy == null) {
55-
policy = policyProvider.get();
56-
}
57-
// If a retry is already scheduled, take no further action.
58-
if (scheduledHandle != null && scheduledHandle.isPending()) {
59-
return -1;
60-
}
61-
long delayNanos = policy.nextBackoffNanos();
62-
scheduledHandle = syncContext.schedule(retryOperation, delayNanos, TimeUnit.NANOSECONDS,
63-
scheduledExecutorService);
64-
logger.fine("Scheduling DNS resolution backoff for " + delayNanos + "ns");
65-
66-
return delayNanos;
67-
}
31+
long schedule(Runnable retryOperation);
6832

6933
/**
70-
* Resets the {@link RetryScheduler} and cancels any pending retry task. The policy will be
71-
* cleared thus also resetting any state associated with it (e.g. a backoff multiplier).
34+
* Resets the scheduler, effectively cancelling any future retry operation.
7235
*/
73-
void reset() {
74-
if (scheduledHandle != null && scheduledHandle.isPending()) {
75-
scheduledHandle.cancel();
76-
}
77-
policy = null;
78-
}
79-
36+
void reset();
8037
}

0 commit comments

Comments
 (0)