Search in sources :

Example 1 with RequestTimeoutClient

use of com.linkedin.d2.balancer.clients.RequestTimeoutClient 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;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) ArrayList(java.util.ArrayList) FSBasedDownstreamServicesFetcher(com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher) NoOpJmxManager(com.linkedin.d2.jmx.NoOpJmxManager) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) RequestTimeoutClient(com.linkedin.d2.balancer.clients.RequestTimeoutClient) DynamicClient(com.linkedin.d2.balancer.clients.DynamicClient) BackupRequestsClient(com.linkedin.d2.balancer.clients.BackupRequestsClient) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) RetryClient(com.linkedin.d2.balancer.clients.RetryClient)

Example 2 with RequestTimeoutClient

use of com.linkedin.d2.balancer.clients.RequestTimeoutClient in project rest.li by linkedin.

the class RequestTimeoutClientTest method testRequestTimeoutAllowed.

/**
 * Check that the timeout are the expected ones
 */
@Test(groups = { "small" }, dataProvider = "allCombinations3x")
@SuppressWarnings("deprecation")
public void testRequestTimeoutAllowed(boolean isHigherThanDefault, boolean ignoreTimeoutIfHigher, int expectedTimeout) throws Exception {
    LoadBalancerSimulator.ClockedExecutor clockedExecutor = new LoadBalancerSimulator.ClockedExecutor();
    LoadBalancerMock balancer = new LoadBalancerMock(false, true, clockedExecutor);
    DirectoryProvider dirProvider = new DirectoryProviderMock();
    KeyMapperProvider keyMapperProvider = new KeyMapperProviderMock();
    ClientFactoryProvider clientFactoryProvider = Mockito.mock(ClientFactoryProvider.class);
    Facilities facilities = new DelegatingFacilities(dirProvider, keyMapperProvider, clientFactoryProvider);
    D2Client client = new DynamicClient(balancer, facilities, true);
    URI uri = URI.create("d2://test");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    client = new RequestTimeoutClient(client, balancer, clockedExecutor);
    RequestContext requestContext = new RequestContext();
    int requestTimeout = isHigherThanDefault ? DEFAULT_REQUEST_TIMEOUT + 100 : DEFAULT_REQUEST_TIMEOUT - 100;
    DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    requestContext.putLocalAttr(R2Constants.REQUEST_TIMEOUT, requestTimeout);
    if (ignoreTimeoutIfHigher) {
        requestContext.putLocalAttr(R2Constants.REQUEST_TIMEOUT_IGNORE_IF_HIGHER_THAN_DEFAULT, ignoreTimeoutIfHigher);
    }
    client.restRequest(restRequest, requestContext, restCallback);
    clockedExecutor.run(expectedTimeout - 10).get();
    Assert.assertFalse(checkTimeoutFired(restCallback));
    checkRequestTimeoutOrViewSet(requestContext);
    clockedExecutor.run(expectedTimeout + 10).get();
    Assert.assertTrue(checkTimeoutFired(restCallback));
    checkRequestTimeoutOrViewSet(requestContext);
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) RestResponse(com.linkedin.r2.message.rest.RestResponse) KeyMapperProvider(com.linkedin.d2.balancer.util.KeyMapperProvider) ClientFactoryProvider(com.linkedin.d2.balancer.util.ClientFactoryProvider) URI(java.net.URI) Facilities(com.linkedin.d2.balancer.Facilities) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) LoadBalancerMock(com.linkedin.d2.balancer.clients.stub.LoadBalancerMock) DirectoryProviderMock(com.linkedin.d2.balancer.clients.stub.DirectoryProviderMock) RestRequest(com.linkedin.r2.message.rest.RestRequest) DirectoryProvider(com.linkedin.d2.balancer.util.DirectoryProvider) LoadBalancerSimulator(com.linkedin.d2.balancer.simple.LoadBalancerSimulator) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) KeyMapperProviderMock(com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) Test(org.testng.annotations.Test)

Aggregations

D2Client (com.linkedin.d2.balancer.D2Client)1 Facilities (com.linkedin.d2.balancer.Facilities)1 BackupRequestsClient (com.linkedin.d2.balancer.clients.BackupRequestsClient)1 DynamicClient (com.linkedin.d2.balancer.clients.DynamicClient)1 RequestTimeoutClient (com.linkedin.d2.balancer.clients.RequestTimeoutClient)1 RetryClient (com.linkedin.d2.balancer.clients.RetryClient)1 DirectoryProviderMock (com.linkedin.d2.balancer.clients.stub.DirectoryProviderMock)1 KeyMapperProviderMock (com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock)1 LoadBalancerMock (com.linkedin.d2.balancer.clients.stub.LoadBalancerMock)1 LoadBalancerSimulator (com.linkedin.d2.balancer.simple.LoadBalancerSimulator)1 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)1 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)1 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)1 RelativeLoadBalancerStrategy (com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy)1 RelativeLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory)1 ClientFactoryProvider (com.linkedin.d2.balancer.util.ClientFactoryProvider)1 DelegatingFacilities (com.linkedin.d2.balancer.util.DelegatingFacilities)1 DirectoryProvider (com.linkedin.d2.balancer.util.DirectoryProvider)1 KeyMapperProvider (com.linkedin.d2.balancer.util.KeyMapperProvider)1 FSBasedDownstreamServicesFetcher (com.linkedin.d2.balancer.util.downstreams.FSBasedDownstreamServicesFetcher)1