Search in sources :

Example 1 with D2ClientConfig

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

the class ZKFSLoadBalancerWithFacilitiesFactory method createLoadBalancerFactory.

private ZKFSLoadBalancer.TogglingLoadBalancerFactory createLoadBalancerFactory(D2ClientConfig config) {
    final ZKFSTogglingLoadBalancerFactoryImpl.ComponentFactory loadBalancerComponentFactory;
    if (config.componentFactory == null) {
        loadBalancerComponentFactory = new ZKFSComponentFactory();
    } else {
        loadBalancerComponentFactory = config.componentFactory;
    }
    D2ClientJmxManager d2ClientJmxManager = new D2ClientJmxManager(config.d2JmxManagerPrefix, config.jmxManager);
    return new ZKFSTogglingLoadBalancerFactoryImpl(loadBalancerComponentFactory, config.lbWaitTimeout, config.lbWaitUnit, config.basePath, config.fsBasePath, config.clientFactories, config.loadBalancerStrategyFactories, config.d2ServicePath, config.sslContext, config.sslParameters, config.isSSLEnabled, config.clientServicesConfig, config.useNewEphemeralStoreWatcher, config.partitionAccessorRegistry, config.enableSaveUriDataOnDisk, config.sslSessionValidatorFactory, d2ClientJmxManager, config.zookeeperReadWindowMs, config.deterministicSubsettingMetadataProvider);
}
Also used : ZKFSTogglingLoadBalancerFactoryImpl(com.linkedin.d2.balancer.zkfs.ZKFSTogglingLoadBalancerFactoryImpl) D2ClientJmxManager(com.linkedin.d2.jmx.D2ClientJmxManager) ZKFSComponentFactory(com.linkedin.d2.balancer.zkfs.ZKFSComponentFactory)

Example 2 with D2ClientConfig

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

the class ZKFSLoadBalancerWithFacilitiesFactory method create.

@Override
public LoadBalancerWithFacilities create(D2ClientConfig config) {
    LOG.info("Creating D2 LoadBalancer based on ZKFSLoadBalancerWithFacilitiesFactory");
    ZKFSLoadBalancer zkfsLoadBalancer = new ZKFSLoadBalancer(config.zkHosts, (int) config.zkSessionTimeoutInMs, (int) config.zkStartupTimeoutInMs, createLoadBalancerFactory(config), config.flagFile, config.basePath, config.shutdownAsynchronously, config.isSymlinkAware, config._executorService, config.zooKeeperDecorator);
    LoadBalancerWithFacilities balancer = zkfsLoadBalancer;
    if (config.warmUp) {
        balancer = new WarmUpLoadBalancer(balancer, zkfsLoadBalancer, config.startUpExecutorService, config.fsBasePath, config.d2ServicePath, config.downstreamServicesFetcher, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
    }
    return balancer;
}
Also used : WarmUpLoadBalancer(com.linkedin.d2.balancer.util.WarmUpLoadBalancer) ZKFSLoadBalancer(com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer)

Example 3 with D2ClientConfig

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

the class LastSeenBalancerWithFacilitiesFactory method create.

@Override
public LoadBalancerWithFacilities create(D2ClientConfig config) {
    LOG.info("Creating D2 LoadBalancer based on LastSeenLoadBalancerWithFacilities");
    D2ClientJmxManager d2ClientJmxManager = new D2ClientJmxManager(config.d2JmxManagerPrefix, config.jmxManager);
    // init connection
    ZKConnectionBuilder zkConnectionBuilder = new ZKConnectionBuilder(config.zkHosts);
    zkConnectionBuilder.setShutdownAsynchronously(config.shutdownAsynchronously).setIsSymlinkAware(config.isSymlinkAware).setTimeout((int) config.zkSessionTimeoutInMs);
    ZKPersistentConnection zkPersistentConnection;
    if (config.zkConnectionToUseForLB != null) {
        LOG.info("LastSeenLoadBalancer using shared connection to zookeeper");
        zkPersistentConnection = config.zkConnectionToUseForLB;
    } else {
        LOG.info("LastSeenLoadBalancer using its own connection to zookeeper");
        zkPersistentConnection = new ZKPersistentConnection(zkConnectionBuilder);
    }
    // init all the stores
    LastSeenZKStore<ClusterProperties> lsClusterStore = getClusterPropertiesLastSeenZKStore(config, zkPersistentConnection, d2ClientJmxManager, config._executorService, config.zookeeperReadWindowMs);
    PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<>(config._executorService);
    clusterBus.setPublisher(lsClusterStore);
    LastSeenZKStore<ServiceProperties> lsServiceStore = getServicePropertiesLastSeenZKStore(config, zkPersistentConnection, d2ClientJmxManager, config._executorService, config.zookeeperReadWindowMs);
    PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<>(config._executorService);
    serviceBus.setPublisher(lsServiceStore);
    LastSeenZKStore<UriProperties> lsUrisStore = getUriPropertiesLastSeenZKStore(config, zkPersistentConnection, d2ClientJmxManager, config._executorService, config.zookeeperReadWindowMs);
    PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<>(config._executorService);
    uriBus.setPublisher(lsUrisStore);
    // create the simple load balancer
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(config._executorService, uriBus, clusterBus, serviceBus, config.clientFactories, config.loadBalancerStrategyFactories, config.sslContext, config.sslParameters, config.isSSLEnabled, config.partitionAccessorRegistry, config.sslSessionValidatorFactory, config.deterministicSubsettingMetadataProvider);
    d2ClientJmxManager.setSimpleLoadBalancerState(state);
    SimpleLoadBalancer simpleLoadBalancer = new SimpleLoadBalancer(state, config.lbWaitTimeout, config.lbWaitUnit, config._executorService);
    d2ClientJmxManager.setSimpleLoadBalancer(simpleLoadBalancer);
    // add facilities
    LastSeenLoadBalancerWithFacilities lastSeenLoadBalancer = new LastSeenLoadBalancerWithFacilities(simpleLoadBalancer, config.basePath, config.d2ServicePath, zkPersistentConnection, lsClusterStore, lsServiceStore, lsUrisStore);
    LoadBalancerWithFacilities balancer = lastSeenLoadBalancer;
    if (config.warmUp) {
        balancer = new WarmUpLoadBalancer(balancer, lastSeenLoadBalancer, config.startUpExecutorService, config.fsBasePath, config.d2ServicePath, config.downstreamServicesFetcher, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
    }
    return balancer;
}
Also used : ZKPersistentConnection(com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection) WarmUpLoadBalancer(com.linkedin.d2.balancer.util.WarmUpLoadBalancer) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) LastSeenLoadBalancerWithFacilities(com.linkedin.d2.balancer.zkfs.LastSeenLoadBalancerWithFacilities) D2ClientJmxManager(com.linkedin.d2.jmx.D2ClientJmxManager) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ZKConnectionBuilder(com.linkedin.d2.discovery.stores.zk.ZKConnectionBuilder) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) LastSeenLoadBalancerWithFacilities(com.linkedin.d2.balancer.zkfs.LastSeenLoadBalancerWithFacilities)

Example 4 with D2ClientConfig

use of com.linkedin.d2.balancer.D2ClientConfig 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 5 with D2ClientConfig

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

the class LastSeenBalancerWithFacilitiesFactory method getClusterPropertiesLastSeenZKStore.

private LastSeenZKStore<ClusterProperties> getClusterPropertiesLastSeenZKStore(D2ClientConfig config, ZKPersistentConnection zkPersistentConnection, D2ClientJmxManager d2ClientJmxManager, ScheduledExecutorService executorService, int zookeeperReadWindowMs) {
    ZooKeeperPermanentStoreBuilder<ClusterProperties> zkClusterStoreBuilder = new ZooKeeperPermanentStoreBuilder<ClusterProperties>().setSerializer(new ClusterPropertiesJsonSerializer()).setPath(ZKFSUtil.clusterPath(config.basePath)).setExecutorService(executorService).setZookeeperReadWindowMs(zookeeperReadWindowMs).addOnBuildListener(d2ClientJmxManager::setZkClusterRegistry);
    FileStore<ClusterProperties> fileStore = new FileStore<>(FileSystemDirectory.getClusterDirectory(config.fsBasePath), new ClusterPropertiesJsonSerializer());
    d2ClientJmxManager.setFsClusterStore(fileStore);
    return new LastSeenZKStore<>(fileStore, zkClusterStoreBuilder, zkPersistentConnection, config._executorService, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
}
Also used : FileStore(com.linkedin.d2.discovery.stores.file.FileStore) LastSeenZKStore(com.linkedin.d2.discovery.stores.zk.LastSeenZKStore) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Aggregations

FileStore (com.linkedin.d2.discovery.stores.file.FileStore)3 LastSeenZKStore (com.linkedin.d2.discovery.stores.zk.LastSeenZKStore)3 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)2 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)2 WarmUpLoadBalancer (com.linkedin.d2.balancer.util.WarmUpLoadBalancer)2 D2ClientJmxManager (com.linkedin.d2.jmx.D2ClientJmxManager)2 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 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)1 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)1 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)1 UriPropertiesMerger (com.linkedin.d2.balancer.properties.UriPropertiesMerger)1 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)1 SimpleLoadBalancerState (com.linkedin.d2.balancer.simple.SimpleLoadBalancerState)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