Search in sources :

Example 1 with LastSeenZKStore

use of com.linkedin.d2.discovery.stores.zk.LastSeenZKStore 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 2 with LastSeenZKStore

use of com.linkedin.d2.discovery.stores.zk.LastSeenZKStore in project rest.li by linkedin.

the class LastSeenZKStoreTest method testLastSeenLifeCycle.

/**
 * The test aims at
 * 1) write data in the FS store
 * 2) Shutdown the ZKServer and check if a new LastSeenZKStore will read data from disk
 * 3) Restart ZKServer and see if this LastSeenZKStore which could never access to disk will retrieve latest
 *    information from there
 */
@Test(timeOut = 10000, enabled = false)
public void testLastSeenLifeCycle() throws InterruptedException, ExecutionException, TimeoutException, IOException, PropertyStoreException {
    createZKServer();
    // Fill the store with data
    File dataPath = ZKTestUtil.createTempDir("randomFileDataPath");
    LastSeenZKStore<String> store = ZkStoreTestOnlyUtil.getLastSeenZKStore(dataPath.getPath(), PORT);
    ZooKeeperEphemeralStore<String> storeWriter = ZkStoreTestOnlyUtil.getZooKeeperEphemeralStore(PORT);
    storeWriter.put(TEST_ZK_PROP_NAME, "randomData");
    PropertyEventBusImpl<String> propertyEventBus = new PropertyEventBusImpl<>(Executors.newSingleThreadExecutor());
    propertyEventBus.setPublisher(store);
    CountDownLatch initializedLatch = new CountDownLatch(1);
    propertyEventBus.register(Collections.singleton(TEST_ZK_PROP_NAME), new LatchSubscriber(initializedLatch, null));
    initializedLatch.await(5, TimeUnit.SECONDS);
    if (initializedLatch.getCount() != 0) {
        fail("Initialized not received");
    }
    // stopping ZK without removing data. This make ZK unreachable
    _zkServer.shutdown(false);
    // create new last seen, without ZK Connection, and see if it fetches from the server
    store = ZkStoreTestOnlyUtil.getLastSeenZKStore(dataPath.getPath(), PORT);
    propertyEventBus = new PropertyEventBusImpl<>(Executors.newSingleThreadExecutor());
    propertyEventBus.setPublisher(store);
    CountDownLatch initializedLatch2 = new CountDownLatch(1);
    CountDownLatch addLatch2 = new CountDownLatch(1);
    propertyEventBus.register(Collections.singleton(TEST_ZK_PROP_NAME), new LatchSubscriber(initializedLatch2, addLatch2));
    initializedLatch2.await(5, TimeUnit.SECONDS);
    if (initializedLatch2.getCount() != 0) {
        fail("Initialized not received");
    }
    if (addLatch2.getCount() != 1) {
        fail("The add latch should have not been invoked yet");
    }
    // restart ZK and see if it reads the most updated value, the most updated value in this case is identical
    _zkServer.restart();
    addLatch2.await(50, TimeUnit.SECONDS);
    if (addLatch2.getCount() != 0) {
        fail("When ZK restarted we didn't read the most updated value from ZK");
    }
    // shutting everything down
    final FutureCallback<None> shutdownCallback = new FutureCallback<>();
    store.shutdown(shutdownCallback);
    shutdownCallback.get(5, TimeUnit.SECONDS);
    final FutureCallback<None> shutdownCallback2 = new FutureCallback<>();
    storeWriter.shutdown(shutdownCallback2);
    shutdownCallback2.get(5, TimeUnit.SECONDS);
    _zkServer.shutdown();
}
Also used : PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with LastSeenZKStore

use of com.linkedin.d2.discovery.stores.zk.LastSeenZKStore 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)

Example 4 with LastSeenZKStore

use of com.linkedin.d2.discovery.stores.zk.LastSeenZKStore in project rest.li by linkedin.

the class LastSeenBalancerWithFacilitiesFactory method getUriPropertiesLastSeenZKStore.

private LastSeenZKStore<UriProperties> getUriPropertiesLastSeenZKStore(D2ClientConfig config, ZKPersistentConnection zkPersistentConnection, D2ClientJmxManager d2ClientJmxManager, ScheduledExecutorService executorService, int zookeeperReadWindowMs) {
    ZooKeeperEphemeralStoreBuilder<UriProperties> zkUrisStoreBuilder = new ZooKeeperEphemeralStoreBuilder<UriProperties>().setSerializer(new UriPropertiesJsonSerializer()).setPath(ZKFSUtil.uriPath(config.basePath)).setMerger(new UriPropertiesMerger()).setUseNewWatcher(config.useNewEphemeralStoreWatcher).setExecutorService(executorService).setZookeeperReadWindowMs(zookeeperReadWindowMs).addOnBuildListener(d2ClientJmxManager::setZkUriRegistry);
    FileStore<UriProperties> fileStore = new FileStore<>(config.fsBasePath + File.separator + ZKFSUtil.URI_PATH, new UriPropertiesJsonSerializer());
    d2ClientJmxManager.setFsUriStore(fileStore);
    if (config.enableSaveUriDataOnDisk) {
        zkUrisStoreBuilder.setBackupStoreFilePath(config.fsBasePath);
    }
    return new LastSeenZKStore<>(fileStore, zkUrisStoreBuilder, zkPersistentConnection, config._executorService, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
}
Also used : UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) FileStore(com.linkedin.d2.discovery.stores.file.FileStore) LastSeenZKStore(com.linkedin.d2.discovery.stores.zk.LastSeenZKStore) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger)

Example 5 with LastSeenZKStore

use of com.linkedin.d2.discovery.stores.zk.LastSeenZKStore in project rest.li by linkedin.

the class LastSeenBalancerWithFacilitiesFactory method getServicePropertiesLastSeenZKStore.

private LastSeenZKStore<ServiceProperties> getServicePropertiesLastSeenZKStore(D2ClientConfig config, ZKPersistentConnection zkPersistentConnection, D2ClientJmxManager d2ClientJmxManager, ScheduledExecutorService executorService, int zookeeperReadWindowMs) {
    ZooKeeperPermanentStoreBuilder<ServiceProperties> zkServiceStoreBuilder = new ZooKeeperPermanentStoreBuilder<ServiceProperties>().setSerializer(new ServicePropertiesJsonSerializer(config.clientServicesConfig)).setPath(ZKFSUtil.servicePath(config.basePath, config.d2ServicePath)).setExecutorService(executorService).setZookeeperReadWindowMs(zookeeperReadWindowMs).addOnBuildListener(d2ClientJmxManager::setZkServiceRegistry);
    FileStore<ServiceProperties> fileStore = new FileStore<>(FileSystemDirectory.getServiceDirectory(config.fsBasePath, config.d2ServicePath), new ServicePropertiesJsonSerializer());
    d2ClientJmxManager.setFsServiceStore(fileStore);
    return new LastSeenZKStore<>(fileStore, zkServiceStoreBuilder, zkPersistentConnection, config._executorService, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
}
Also used : FileStore(com.linkedin.d2.discovery.stores.file.FileStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) LastSeenZKStore(com.linkedin.d2.discovery.stores.zk.LastSeenZKStore) ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)

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 PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 None (com.linkedin.common.util.None)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 WarmUpLoadBalancer (com.linkedin.d2.balancer.util.WarmUpLoadBalancer)1 LastSeenLoadBalancerWithFacilities (com.linkedin.d2.balancer.zkfs.LastSeenLoadBalancerWithFacilities)1 ZKConnectionBuilder (com.linkedin.d2.discovery.stores.zk.ZKConnectionBuilder)1 ZKPersistentConnection (com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection)1 D2ClientJmxManager (com.linkedin.d2.jmx.D2ClientJmxManager)1 File (java.io.File)1