下面列出了怎么用io.grpc.SynchronizationContext的API类实例代码及写法,或者点击链接到github查看源代码。
InternalSubchannel(List<EquivalentAddressGroup> addressGroups, String authority, String userAgent,
BackoffPolicy.Provider backoffPolicyProvider,
ClientTransportFactory transportFactory, ScheduledExecutorService scheduledExecutor,
Supplier<Stopwatch> stopwatchSupplier, SynchronizationContext syncContext, Callback callback,
InternalChannelz channelz, CallTracer callsTracer, ChannelTracer channelTracer,
InternalLogId logId, TimeProvider timeProvider) {
Preconditions.checkNotNull(addressGroups, "addressGroups");
Preconditions.checkArgument(!addressGroups.isEmpty(), "addressGroups is empty");
checkListHasNoNulls(addressGroups, "addressGroups contains null entry");
this.addressIndex = new Index(
Collections.unmodifiableList(new ArrayList<>(addressGroups)));
this.authority = authority;
this.userAgent = userAgent;
this.backoffPolicyProvider = backoffPolicyProvider;
this.transportFactory = transportFactory;
this.scheduledExecutor = scheduledExecutor;
this.connectingTimer = stopwatchSupplier.get();
this.syncContext = syncContext;
this.callback = callback;
this.channelz = channelz;
this.callsTracer = callsTracer;
this.channelTracer = Preconditions.checkNotNull(channelTracer, "channelTracer");
this.logId = Preconditions.checkNotNull(logId, "logId");
this.channelLogger = new ChannelLoggerImpl(channelTracer, timeProvider);
}
XdsClientImpl(
String targetName,
List<ServerInfo> servers, // list of management servers
XdsChannelFactory channelFactory,
Node node,
SynchronizationContext syncContext,
ScheduledExecutorService timeService,
BackoffPolicy.Provider backoffPolicyProvider,
Supplier<Stopwatch> stopwatchSupplier) {
this.targetName = checkNotNull(targetName, "targetName");
this.channel =
checkNotNull(channelFactory, "channelFactory")
.createChannel(checkNotNull(servers, "servers"));
this.node = checkNotNull(node, "node");
this.syncContext = checkNotNull(syncContext, "syncContext");
this.timeService = checkNotNull(timeService, "timeService");
this.backoffPolicyProvider = checkNotNull(backoffPolicyProvider, "backoffPolicyProvider");
this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatch");
adsStreamRetryStopwatch = stopwatchSupplier.get();
logId = InternalLogId.allocate("xds-client", targetName);
logger = XdsLogger.withLogId(logId);
logger.log(XdsLogLevel.INFO, "Created");
}
HealthCheckState(
HelperImpl helperImpl,
LoadBalancer delegate, SynchronizationContext syncContext,
ScheduledExecutorService timerService) {
this.helperImpl = checkNotNull(helperImpl, "helperImpl");
this.delegate = checkNotNull(delegate, "delegate");
this.syncContext = checkNotNull(syncContext, "syncContext");
this.timerService = checkNotNull(timerService, "timerService");
}
OobChannel(
String authority, ObjectPool<? extends Executor> executorPool,
ScheduledExecutorService deadlineCancellationExecutor, SynchronizationContext syncContext,
CallTracer callsTracer, ChannelTracer channelTracer, InternalChannelz channelz,
TimeProvider timeProvider) {
this.authority = checkNotNull(authority, "authority");
this.executorPool = checkNotNull(executorPool, "executorPool");
this.executor = checkNotNull(executorPool.getObject(), "executor");
this.deadlineCancellationExecutor = checkNotNull(
deadlineCancellationExecutor, "deadlineCancellationExecutor");
this.delayedTransport = new DelayedClientTransport(executor, syncContext);
this.channelz = Preconditions.checkNotNull(channelz);
this.delayedTransport.start(new ManagedClientTransport.Listener() {
@Override
public void transportShutdown(Status s) {
// Don't care
}
@Override
public void transportTerminated() {
subchannelImpl.shutdown();
}
@Override
public void transportReady() {
// Don't care
}
@Override
public void transportInUse(boolean inUse) {
// Don't care
}
});
this.channelCallsTracer = callsTracer;
this.channelTracer = checkNotNull(channelTracer, "channelTracer");
this.timeProvider = checkNotNull(timeProvider, "timeProvider");
}
LoadReportClient(
InternalLogId logId,
String targetName,
ManagedChannel channel,
Node node,
SynchronizationContext syncContext,
ScheduledExecutorService scheduledExecutorService,
BackoffPolicy.Provider backoffPolicyProvider,
Supplier<Stopwatch> stopwatchSupplier) {
this.channel = checkNotNull(channel, "channel");
this.syncContext = checkNotNull(syncContext, "syncContext");
this.timerService = checkNotNull(scheduledExecutorService, "timeService");
this.backoffPolicyProvider = checkNotNull(backoffPolicyProvider, "backoffPolicyProvider");
this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier");
this.retryStopwatch = stopwatchSupplier.get();
checkNotNull(targetName, "targetName");
checkNotNull(node, "node");
Struct metadata =
node.getMetadata()
.toBuilder()
.putFields(
TARGET_NAME_METADATA_KEY,
Value.newBuilder().setStringValue(targetName).build())
.build();
this.node = node.toBuilder().setMetadata(metadata).build();
String logPrefix = checkNotNull(logId, "logId").toString().concat("-lrs-client");
logger = XdsLogger.withPrefix(logPrefix);
}
OrcaReportingState(
OrcaReportingHelper orcaHelper,
SynchronizationContext syncContext,
ScheduledExecutorService timeService) {
this.orcaHelper = checkNotNull(orcaHelper, "orcaHelper");
this.syncContext = checkNotNull(syncContext, "syncContext");
this.timeService = checkNotNull(timeService, "timeService");
}
HealthCheckState(
HelperImpl helperImpl,
Subchannel subchannel, SynchronizationContext syncContext,
ScheduledExecutorService timerService) {
this.helperImpl = checkNotNull(helperImpl, "helperImpl");
this.subchannel = checkNotNull(subchannel, "subchannel");
this.subchannelLogger = checkNotNull(subchannel.getChannelLogger(), "subchannelLogger");
this.syncContext = checkNotNull(syncContext, "syncContext");
this.timerService = checkNotNull(timerService, "timerService");
}
InternalSubchannel(List<EquivalentAddressGroup> addressGroups, String authority, String userAgent,
BackoffPolicy.Provider backoffPolicyProvider,
ClientTransportFactory transportFactory, ScheduledExecutorService scheduledExecutor,
Supplier<Stopwatch> stopwatchSupplier, SynchronizationContext syncContext, Callback callback,
InternalChannelz channelz, CallTracer callsTracer, ChannelTracer channelTracer,
InternalLogId logId, ChannelLogger channelLogger) {
Preconditions.checkNotNull(addressGroups, "addressGroups");
Preconditions.checkArgument(!addressGroups.isEmpty(), "addressGroups is empty");
checkListHasNoNulls(addressGroups, "addressGroups contains null entry");
List<EquivalentAddressGroup> unmodifiableAddressGroups =
Collections.unmodifiableList(new ArrayList<>(addressGroups));
this.addressGroups = unmodifiableAddressGroups;
this.addressIndex = new Index(unmodifiableAddressGroups);
this.authority = authority;
this.userAgent = userAgent;
this.backoffPolicyProvider = backoffPolicyProvider;
this.transportFactory = transportFactory;
this.scheduledExecutor = scheduledExecutor;
this.connectingTimer = stopwatchSupplier.get();
this.syncContext = syncContext;
this.callback = callback;
this.channelz = channelz;
this.callsTracer = callsTracer;
this.channelTracer = Preconditions.checkNotNull(channelTracer, "channelTracer");
this.logId = Preconditions.checkNotNull(logId, "logId");
this.channelLogger = Preconditions.checkNotNull(channelLogger, "channelLogger");
}
OobChannel(
String authority, ObjectPool<? extends Executor> executorPool,
ScheduledExecutorService deadlineCancellationExecutor, SynchronizationContext syncContext,
CallTracer callsTracer, ChannelTracer channelTracer, InternalChannelz channelz,
TimeProvider timeProvider) {
this.authority = checkNotNull(authority, "authority");
this.logId = InternalLogId.allocate(getClass(), authority);
this.executorPool = checkNotNull(executorPool, "executorPool");
this.executor = checkNotNull(executorPool.getObject(), "executor");
this.deadlineCancellationExecutor = checkNotNull(
deadlineCancellationExecutor, "deadlineCancellationExecutor");
this.delayedTransport = new DelayedClientTransport(executor, syncContext);
this.channelz = Preconditions.checkNotNull(channelz);
this.delayedTransport.start(new ManagedClientTransport.Listener() {
@Override
public void transportShutdown(Status s) {
// Don't care
}
@Override
public void transportTerminated() {
subchannelImpl.shutdown();
}
@Override
public void transportReady() {
// Don't care
}
@Override
public void transportInUse(boolean inUse) {
// Don't care
}
});
this.channelCallsTracer = callsTracer;
this.channelTracer = checkNotNull(channelTracer, "channelTracer");
this.timeProvider = checkNotNull(timeProvider, "timeProvider");
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return delegate().getSynchronizationContext();
}
@Test
public void channelTracing_lbPolicyChanged() {
final FakeClock clock = new FakeClock();
List<EquivalentAddressGroup> servers =
Collections.singletonList(
new EquivalentAddressGroup(new SocketAddress(){}, Attributes.EMPTY));
Helper helper = new TestHelper() {
@Override
public Subchannel createSubchannel(List<EquivalentAddressGroup> addrs, Attributes attrs) {
return new TestSubchannel(addrs, attrs);
}
@Override
public ManagedChannel createOobChannel(EquivalentAddressGroup eag, String authority) {
return mock(ManagedChannel.class, RETURNS_DEEP_STUBS);
}
@Override
public String getAuthority() {
return "fake_authority";
}
@Override
public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
// noop
}
@Override
public SynchronizationContext getSynchronizationContext() {
return new SynchronizationContext(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
throw new AssertionError(e);
}
});
}
@Override
public ScheduledExecutorService getScheduledExecutorService() {
return clock.getScheduledExecutorService();
}
};
LoadBalancer lb = new AutoConfiguredLoadBalancerFactory().newLoadBalancer(helper);
lb.handleResolvedAddressGroups(servers, Attributes.EMPTY);
verifyNoMoreInteractions(channelLogger);
Map<String, Object> serviceConfig = new HashMap<String, Object>();
serviceConfig.put("loadBalancingPolicy", "round_robin");
lb.handleResolvedAddressGroups(servers,
Attributes.newBuilder()
.set(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG, serviceConfig).build());
verify(channelLogger).log(
eq(ChannelLogLevel.INFO),
eq("Load balancer changed from {0} to {1}"),
eq("PickFirstLoadBalancer"), eq("RoundRobinLoadBalancer"));
verifyNoMoreInteractions(channelLogger);
serviceConfig.put("loadBalancingPolicy", "round_robin");
lb.handleResolvedAddressGroups(servers,
Attributes.newBuilder()
.set(GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG, serviceConfig).build());
verifyNoMoreInteractions(channelLogger);
servers = Collections.singletonList(new EquivalentAddressGroup(
new SocketAddress(){},
Attributes.newBuilder().set(GrpcAttributes.ATTR_LB_ADDR_AUTHORITY, "ok").build()));
lb.handleResolvedAddressGroups(servers, Attributes.EMPTY);
verify(channelLogger).log(
eq(ChannelLogLevel.INFO),
eq("Load balancer changed from {0} to {1}"),
eq("RoundRobinLoadBalancer"), eq("GrpclbLoadBalancer"));
verifyNoMoreInteractions(channelLogger);
}
IdleSubchannelEntry(Subchannel subchannel, SynchronizationContext syncContext) {
this.subchannel = checkNotNull(subchannel, "subchannel");
this.syncContext = checkNotNull(syncContext, "syncContext");
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return syncContext;
}
@Override
public SynchronizationContext getSynchronizationContext() {
return delegate().getSynchronizationContext();
}
/**
* Creates a new delayed transport.
*
* @param defaultAppExecutor pending streams will create real streams and run bufferred operations
* in an application executor, which will be this executor, unless there is on provided in
* {@link CallOptions}.
* @param syncContext all listener callbacks of the delayed transport will be run from this
* SynchronizationContext.
*/
DelayedClientTransport(Executor defaultAppExecutor, SynchronizationContext syncContext) {
this.defaultAppExecutor = defaultAppExecutor;
this.syncContext = syncContext;
}
/**
* Creates a new delayed transport.
*
* @param defaultAppExecutor pending streams will create real streams and run bufferred operations
* in an application executor, which will be this executor, unless there is on provided in
* {@link CallOptions}.
* @param syncContext all listener callbacks of the delayed transport will be run from this
* SynchronizationContext.
*/
DelayedClientTransport(Executor defaultAppExecutor, SynchronizationContext syncContext) {
this.defaultAppExecutor = defaultAppExecutor;
this.syncContext = syncContext;
}