-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move name resolution retry from managed channel to name resolver (take …
…#2) (#9812) This change has these main aspects to it: 1. Removal of any name resolution responsibility from ManagedChannelImpl 2. Creation of a new RetryScheduler to own generic retry logic - Can also be used outside the name resolution context 3. Creation of a new RetryingNameScheduler that can be used to wrap any polling name resolver to add retry capability 4. A new facility in NameResolver to allow implementations to notify listeners on the success of name resolution attempts - RetryingNameScheduler relies on this
- Loading branch information
Showing
13 changed files
with
753 additions
and
360 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
core/src/main/java/io/grpc/internal/BackoffPolicyRetryScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2023 The gRPC Authors | ||
* | ||
* Licensed 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 io.grpc.internal; | ||
|
||
import io.grpc.SynchronizationContext; | ||
import io.grpc.SynchronizationContext.ScheduledHandle; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Schedules a retry operation according to a {@link BackoffPolicy}. The retry is run within a | ||
* {@link SynchronizationContext}. At most one retry is scheduled at a time. | ||
*/ | ||
final class BackoffPolicyRetryScheduler implements RetryScheduler { | ||
private final ScheduledExecutorService scheduledExecutorService; | ||
private final SynchronizationContext syncContext; | ||
private final BackoffPolicy.Provider policyProvider; | ||
|
||
private BackoffPolicy policy; | ||
private ScheduledHandle scheduledHandle; | ||
|
||
private static final Logger logger = Logger.getLogger( | ||
BackoffPolicyRetryScheduler.class.getName()); | ||
|
||
BackoffPolicyRetryScheduler(BackoffPolicy.Provider policyProvider, | ||
ScheduledExecutorService scheduledExecutorService, | ||
SynchronizationContext syncContext) { | ||
this.policyProvider = policyProvider; | ||
this.scheduledExecutorService = scheduledExecutorService; | ||
this.syncContext = syncContext; | ||
} | ||
|
||
/** | ||
* Schedules a future retry operation. Only allows one retry to be scheduled at any given time. | ||
*/ | ||
@Override | ||
public void schedule(Runnable retryOperation) { | ||
syncContext.throwIfNotInThisSynchronizationContext(); | ||
|
||
if (policy == null) { | ||
policy = policyProvider.get(); | ||
} | ||
// If a retry is already scheduled, take no further action. | ||
if (scheduledHandle != null && scheduledHandle.isPending()) { | ||
return; | ||
} | ||
long delayNanos = policy.nextBackoffNanos(); | ||
scheduledHandle = syncContext.schedule(retryOperation, delayNanos, TimeUnit.NANOSECONDS, | ||
scheduledExecutorService); | ||
logger.log(Level.FINE, "Scheduling DNS resolution backoff for {0}ns", delayNanos); | ||
} | ||
|
||
/** | ||
* Resets the {@link BackoffPolicyRetryScheduler} and cancels any pending retry task. The policy | ||
* will be cleared thus also resetting any state associated with it (e.g. a backoff multiplier). | ||
*/ | ||
@Override | ||
public void reset() { | ||
syncContext.throwIfNotInThisSynchronizationContext(); | ||
|
||
syncContext.execute(() -> { | ||
if (scheduledHandle != null && scheduledHandle.isPending()) { | ||
scheduledHandle.cancel(); | ||
} | ||
policy = null; | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,19 +47,25 @@ public final class DnsNameResolverProvider extends NameResolverProvider { | |
private static final String SCHEME = "dns"; | ||
|
||
@Override | ||
public DnsNameResolver newNameResolver(URI targetUri, NameResolver.Args args) { | ||
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lhotari
|
||
if (SCHEME.equals(targetUri.getScheme())) { | ||
String targetPath = Preconditions.checkNotNull(targetUri.getPath(), "targetPath"); | ||
Preconditions.checkArgument(targetPath.startsWith("/"), | ||
"the path component (%s) of the target (%s) must start with '/'", targetPath, targetUri); | ||
String name = targetPath.substring(1); | ||
return new DnsNameResolver( | ||
targetUri.getAuthority(), | ||
name, | ||
args, | ||
GrpcUtil.SHARED_CHANNEL_EXECUTOR, | ||
Stopwatch.createUnstarted(), | ||
InternalServiceProviders.isAndroid(getClass().getClassLoader())); | ||
return new RetryingNameResolver( | ||
new DnsNameResolver( | ||
targetUri.getAuthority(), | ||
name, | ||
args, | ||
GrpcUtil.SHARED_CHANNEL_EXECUTOR, | ||
Stopwatch.createUnstarted(), | ||
InternalServiceProviders.isAndroid(getClass().getClassLoader())), | ||
new BackoffPolicyRetryScheduler( | ||
new ExponentialBackoffPolicy.Provider(), | ||
args.getScheduledExecutorService(), | ||
args.getSynchronizationContext()), | ||
args.getSynchronizationContext()); | ||
} else { | ||
return null; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2023 The gRPC Authors | ||
* | ||
* Licensed 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 io.grpc.internal; | ||
|
||
/** | ||
* This interface is used to schedule future retry attempts for a failed operation. The retry delay | ||
* and the number of attempt is defined by implementing classes. Implementations should assure | ||
* that only one future retry operation is ever scheduled at a time. | ||
*/ | ||
public interface RetryScheduler { | ||
|
||
/** | ||
* A request to schedule a future retry (or retries) for a failed operation. Noop if an operation | ||
* has already been scheduled. | ||
*/ | ||
void schedule(Runnable retryOperation); | ||
|
||
/** | ||
* Resets the scheduler, effectively cancelling any future retry operation. | ||
*/ | ||
void reset(); | ||
} |
Oops, something went wrong.
This seems to break binary compatibility. Is this intentional?
I get
java.lang.NoSuchMethodError: 'io.grpc.internal.DnsNameResolver io.grpc.internal.DnsNameResolverProvider.newNameResolver(java.net.URI, io.grpc.NameResolver$Args)'
, examples in Apache Pulsar when trying to upgrade to grpc-java 1.56.0 . The failure appear in the Apache Bookkeeper client that uses an older version of gprc-java, version 1.47.0 .