use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.
the class RetryClientTest method testStreamRetry.
@Test
public void testStreamRetry() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, true);
URI uri = URI.create("d2://retryService?arg1arg2");
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
DegraderTrackerClientTest.TestCallback<StreamResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
client.streamRequest(streamRequest, restCallback);
assertNull(restCallback.e);
assertNotNull(restCallback.t);
}
use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.
the class RetryClientTest method testRetry.
@Test
public void testRetry() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"));
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, 3);
URI uri = URI.create("d2://retryService?arg1arg2");
RestRequest restRequest = new RestRequestBuilder(uri).build();
TrackerClientTest.TestCallback<RestResponse> restCallback = new TrackerClientTest.TestCallback<RestResponse>();
client.restRequest(restRequest, restCallback);
assertNull(restCallback.e);
assertNotNull(restCallback.t);
}
use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.
the class D2ClientBuilder method build.
/**
* @return {@link D2Client} that is not started yet. Call start(Callback) to start it.
*/
public D2Client build() {
final Map<String, TransportClientFactory> transportClientFactories = (_config.clientFactories == null) ? // if user didn't provide transportClientFactories we'll use default ones
createDefaultTransportClientFactories() : _config.clientFactories;
List<ScheduledExecutorService> executorsToShutDown = new ArrayList<>();
if (_config.startUpExecutorService == null) {
// creating an executor that when there are no tasks to execute doesn't create any thread.
_config.startUpExecutorService = Executors.newScheduledThreadPool(0, new NamedThreadFactory("D2 StartupOnlyExecutor"));
executorsToShutDown.add(_config.startUpExecutorService);
}
if (_config._executorService == null) {
LOG.warn("No executor service passed as argument. Pass it for " + "enhanced monitoring and to have better control over the executor.");
_config._executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("D2 PropertyEventExecutor"));
executorsToShutDown.add(_config._executorService);
}
if (_config.downstreamServicesFetcher == null) {
_config.downstreamServicesFetcher = new FSBasedDownstreamServicesFetcher(_config.fsBasePath, _config.d2ServicePath);
}
if (_config.jmxManager == null) {
_config.jmxManager = new NoOpJmxManager();
}
if (_config.d2ServicePath == null || // checking empty for backward compatibility with ZKFS behavior
_config.d2ServicePath.isEmpty()) {
_config.d2ServicePath = ZKFSUtil.SERVICE_PATH;
}
final Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = createDefaultLoadBalancerStrategyFactories();
final D2ClientConfig cfg = new D2ClientConfig(_config.zkHosts, _config.zkSessionTimeoutInMs, _config.zkStartupTimeoutInMs, _config.lbWaitTimeout, _config.lbWaitUnit, _config.flagFile, _config.basePath, _config.fsBasePath, _config.componentFactory, transportClientFactories, _config.lbWithFacilitiesFactory, _config.sslContext, _config.sslParameters, _config.isSSLEnabled, _config.shutdownAsynchronously, _config.isSymlinkAware, _config.clientServicesConfig, _config.d2ServicePath, _config.useNewEphemeralStoreWatcher, _config.healthCheckOperations, _config._executorService, _config.retry, _config.restRetryEnabled, _config.streamRetryEnabled, _config.retryLimit, _config.retryUpdateIntervalMs, _config.retryAggregatedIntervalNum, _config.warmUp, _config.warmUpTimeoutSeconds, _config.warmUpConcurrentRequests, _config.downstreamServicesFetcher, _config.backupRequestsEnabled, _config.backupRequestsStrategyStatsConsumer, _config.backupRequestsLatencyNotificationInterval, _config.backupRequestsLatencyNotificationIntervalUnit, _config.enableBackupRequestsClientAsync, _config._backupRequestsExecutorService, _config.eventEmitter, _config.partitionAccessorRegistry, _config.zooKeeperDecorator, _config.enableSaveUriDataOnDisk, loadBalancerStrategyFactories, _config.requestTimeoutHandlerEnabled, _config.sslSessionValidatorFactory, _config.zkConnectionToUseForLB, _config.startUpExecutorService, _config.jmxManager, _config.d2JmxManagerPrefix, _config.zookeeperReadWindowMs, _config.enableRelativeLoadBalancer, _config.deterministicSubsettingMetadataProvider);
final LoadBalancerWithFacilitiesFactory loadBalancerFactory = (_config.lbWithFacilitiesFactory == null) ? new ZKFSLoadBalancerWithFacilitiesFactory() : _config.lbWithFacilitiesFactory;
LoadBalancerWithFacilities loadBalancer = loadBalancerFactory.create(cfg);
D2Client d2Client = new DynamicClient(loadBalancer, loadBalancer, _restOverStream);
if (_config.requestTimeoutHandlerEnabled) {
d2Client = new RequestTimeoutClient(d2Client, loadBalancer, _config._executorService);
}
if (_config.backupRequestsEnabled) {
ScheduledExecutorService executor = _config._backupRequestsExecutorService;
if (executor == null) {
LOG.warn("Backup Requests Executor not configured, creating one with core pool size equal to: " + Runtime.getRuntime().availableProcessors());
executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new NamedThreadFactory("Backup Requests Executor"));
executorsToShutDown.add(executor);
}
d2Client = new BackupRequestsClient(d2Client, loadBalancer, executor, _config.backupRequestsStrategyStatsConsumer, _config.backupRequestsLatencyNotificationInterval, _config.backupRequestsLatencyNotificationIntervalUnit, _config.enableBackupRequestsClientAsync);
}
if (_config.retry) {
d2Client = new RetryClient(d2Client, loadBalancer, _config.retryLimit, _config.retryUpdateIntervalMs, _config.retryAggregatedIntervalNum, SystemClock.instance(), true, true);
} else if (_config.restRetryEnabled || _config.streamRetryEnabled) {
d2Client = new RetryClient(d2Client, loadBalancer, _config.retryLimit, _config.retryUpdateIntervalMs, _config.retryAggregatedIntervalNum, SystemClock.instance(), _config.restRetryEnabled, _config.streamRetryEnabled);
}
// is being shut down.
if (_config.clientFactories != transportClientFactories) {
d2Client = new TransportClientFactoryAwareD2Client(d2Client, transportClientFactories.values());
}
if (executorsToShutDown.size() > 0) {
d2Client = new ExecutorShutdownAwareD2Client(d2Client, executorsToShutDown);
}
return d2Client;
}
use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.
the class RetryClientTest method testIgnoreStreamRetry.
@Test
public void testIgnoreStreamRetry() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
URI uri = URI.create("d2://retryService?arg1arg2");
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
DegraderTrackerClientTest.TestCallback<StreamResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
client.streamRequest(streamRequest, restCallback);
assertNull(restCallback.t);
assertNotNull(restCallback.e);
assertTrue(restCallback.e.getMessage().contains("Data not available"));
}
use of com.linkedin.d2.balancer.clients.RetryClient in project rest.li by linkedin.
the class RetryClientTest method testRestRetryOverLimit.
@Test
public void testRestRetryOverLimit() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/retry2"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, balancer, 1, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
RestRequest restRequest = new RestRequestBuilder(uri).build();
DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
client.restRequest(restRequest, restCallback);
assertNull(restCallback.t);
assertNotNull(restCallback.e);
assertTrue(restCallback.e.getMessage().contains("Data not available"));
}
Aggregations