下面列出了怎么用io.grpc.internal.ExponentialBackoffPolicy的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public XdsNameResolver newNameResolver(URI targetUri, Args args) {
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 XdsNameResolver(
name,
args,
new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER,
XdsChannelFactory.getInstance(),
Bootstrapper.getInstance());
}
return null;
}
@Override
public NameResolver newNameResolver(URI targetUri, Args args) {
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 XdsNameResolver(
name,
args,
new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER,
XdsChannelFactory.getInstance(),
Bootstrapper.getInstance());
}
return null;
}
@Override
public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) {
return
new GrpclbLoadBalancer(
helper,
new CachedSubchannelPool(helper),
TimeProvider.SYSTEM_TIME_PROVIDER,
Stopwatch.createUnstarted(),
new ExponentialBackoffPolicy.Provider());
}
/** Creates an XdsClient and starts a watch. */
public void createXdsClientAndStart() {
checkState(xdsClient == null, "start() called more than once");
Bootstrapper.BootstrapInfo bootstrapInfo;
List<Bootstrapper.ServerInfo> serverList;
try {
bootstrapInfo = Bootstrapper.getInstance().readBootstrap();
serverList = bootstrapInfo.getServers();
if (serverList.isEmpty()) {
throw new ManagementServerNotFoundException("No management server provided by bootstrap");
}
} catch (IOException | ManagementServerNotFoundException e) {
logger.log(Level.FINE, "Exception reading bootstrap", e);
logger.log(Level.INFO, "Fallback to plaintext for server at port {0}", port);
return;
}
Node node = bootstrapInfo.getNode();
timeService = SharedResourceHolder.get(timeServiceResource);
XdsClientImpl xdsClientImpl =
new XdsClientImpl(
"",
serverList,
XdsClient.XdsChannelFactory.getInstance(),
node,
createSynchronizationContext(),
timeService,
new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER);
start(xdsClientImpl);
}
@Override
public OrcaReportingHelperWrapper newOrcaReportingHelperWrapper(
LoadBalancer.Helper delegate,
OrcaOobReportListener listener) {
return newOrcaReportingHelperWrapper(
delegate,
listener,
new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER);
}
@Override
public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) {
return new GrpclbLoadBalancer(
helper, new CachedSubchannelPool(), TimeProvider.SYSTEM_TIME_PROVIDER,
new ExponentialBackoffPolicy.Provider());
}
@Override
public final void resetBackoffNanos() {
exponentialBackoffPolicy = new ExponentialBackoffPolicy();
}
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
Object lbConfig = resolvedAddresses.getLoadBalancingPolicyConfig();
if (lbConfig == null) {
handleNameResolutionError(Status.UNAVAILABLE.withDescription("Missing EDS lb config"));
return;
}
EdsConfig newEdsConfig = (EdsConfig) lbConfig;
if (logger.isLoggable(XdsLogLevel.INFO)) {
logger.log(
XdsLogLevel.INFO,
"Received EDS lb config: cluster={0}, child_policy={1}, "
+ "eds_service_name={2}, report_load={3}",
newEdsConfig.clusterName,
newEdsConfig.endpointPickingPolicy.getProvider().getPolicyName(),
newEdsConfig.edsServiceName,
newEdsConfig.lrsServerName != null);
}
boolean firstUpdate = false;
if (clusterName == null) {
firstUpdate = true;
}
clusterName = newEdsConfig.clusterName;
if (xdsClientPool == null) {
Attributes attributes = resolvedAddresses.getAttributes();
xdsClientPool = attributes.get(XdsAttributes.XDS_CLIENT_POOL);
if (xdsClientPool == null) {
final BootstrapInfo bootstrapInfo;
try {
bootstrapInfo = bootstrapper.readBootstrap();
} catch (Exception e) {
helper.updateBalancingState(
TRANSIENT_FAILURE,
new ErrorPicker(
Status.UNAVAILABLE.withDescription("Failed to bootstrap").withCause(e)));
return;
}
final List<ServerInfo> serverList = bootstrapInfo.getServers();
final Node node = bootstrapInfo.getNode();
if (serverList.isEmpty()) {
helper.updateBalancingState(
TRANSIENT_FAILURE,
new ErrorPicker(
Status.UNAVAILABLE
.withDescription("No management server provided by bootstrap")));
return;
}
XdsClientFactory xdsClientFactory = new XdsClientFactory() {
@Override
XdsClient createXdsClient() {
return
new XdsClientImpl(
helper.getAuthority(),
serverList,
channelFactory,
node,
helper.getSynchronizationContext(),
helper.getScheduledExecutorService(),
new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER);
}
};
xdsClientPool = new RefCountedXdsClientObjectPool(xdsClientFactory);
} else {
logger.log(XdsLogLevel.INFO, "Use xDS client from channel");
}
xdsClient = xdsClientPool.getObject();
}
// Note: childPolicy change will be handled in LocalityStore, to be implemented.
// If edsServiceName in XdsConfig is changed, do a graceful switch.
if (firstUpdate || !Objects.equals(newEdsConfig.edsServiceName, edsServiceName)) {
LoadBalancer.Factory clusterEndpointsLoadBalancerFactory =
new ClusterEndpointsBalancerFactory(newEdsConfig.edsServiceName);
switchingLoadBalancer.switchTo(clusterEndpointsLoadBalancerFactory);
}
switchingLoadBalancer.handleResolvedAddresses(resolvedAddresses);
this.edsServiceName = newEdsConfig.edsServiceName;
}
/**
* Creates a health-checking-capable LoadBalancer. This method is used to implement
* health-checking-capable {@link Factory}s, which will typically written this way:
*
* <pre>
* public class HealthCheckingFooLbFactory extends LoadBalancer.Factory {
* // This is the original balancer implementation that doesn't have health checking
* private final LoadBalancer.Factory fooLbFactory;
*
* ...
*
* // Returns the health-checking-capable version of FooLb
* public LoadBalancer newLoadBalancer(Helper helper) {
* return HealthCheckingLoadBalancerUtil.newHealthCheckingLoadBalancer(fooLbFactory, helper);
* }
* }
* </pre>
*
* <p>As a requirement for the original LoadBalancer, it must call
* {@code Helper.createSubchannel()} from the {@link
* io.grpc.LoadBalancer.Helper#getSynchronizationContext() Synchronization Context}, or
* {@code createSubchannel()} will throw.
*
* @param factory the original factory that implements load-balancing logic without health
* checking
* @param helper the helper passed to the resulting health-checking LoadBalancer.
*/
public static LoadBalancer newHealthCheckingLoadBalancer(Factory factory, Helper helper) {
HealthCheckingLoadBalancerFactory hcFactory =
new HealthCheckingLoadBalancerFactory(
factory, new ExponentialBackoffPolicy.Provider(), TimeProvider.SYSTEM_TIME_PROVIDER);
return hcFactory.newLoadBalancer(helper);
}
/**
* Creates a health-checking-capable LoadBalancer. This method is used to implement
* health-checking-capable {@link Factory}s, which will typically written this way:
*
* <pre>
* public class HealthCheckingFooLbFactory extends LoadBalancer.Factory {
* // This is the original balancer implementation that doesn't have health checking
* private final LoadBalancer.Factory fooLbFactory;
*
* ...
*
* // Returns the health-checking-capable version of FooLb
* public LoadBalancer newLoadBalancer(Helper helper) {
* return HealthCheckingLoadBalancerUtil.newHealthCheckingLoadBalancer(fooLbFactory, helper);
* }
* }
* </pre>
*
* <p>As a requirement for the original LoadBalancer, it must call
* {@code Helper.createSubchannel()} from the {@link
* io.grpc.LoadBalancer.Helper#getSynchronizationContext() Synchronization Context}, or
* {@code createSubchannel()} will throw.
*
* @param factory the original factory that implements load-balancing logic without health
* checking
* @param helper the helper passed to the resulting health-checking LoadBalancer.
*/
public static LoadBalancer newHealthCheckingLoadBalancer(Factory factory, Helper helper) {
HealthCheckingLoadBalancerFactory hcFactory =
new HealthCheckingLoadBalancerFactory(
factory, new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER);
return hcFactory.newLoadBalancer(helper);
}