Search in sources :

Example 6 with ZKPersistentConnection

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

the class ZooKeeperConnectionAwareStoreTest method testRereadFromBusAfterExpiration.

@Test
public void testRereadFromBusAfterExpiration() throws InterruptedException, IOException, PropertyStoreException, ExecutionException, TimeoutException {
    ZooKeeperConnectionAwareStore<String, ZooKeeperEphemeralStore<String>> store = ZkStoreTestOnlyUtil.getZKAwareStore(PORT);
    PropertyEventBusImpl<String> propertyEventBus = new PropertyEventBusImpl<>(Executors.newSingleThreadExecutor());
    store.setBusImpl(propertyEventBus);
    // the first time it is written, the this countdown will go down
    CountDownLatch initializedLatch = new CountDownLatch(1);
    // when reconnection happens, the properties will be re-registered again under the current implementation
    // and the add callback will be called
    CountDownLatch addLatch = new CountDownLatch(1);
    // we could move this three statements below registration, but then we should change the logic
    // of the Subscriber: if you register before any value is in, you'll get a null value before in onInitialize
    // and subsequently values in onAdd. Therefore moving those need a refactor of the subscriber
    ZKPersistentConnection zkPersistentConnection = ZkStoreTestOnlyUtil.getZkPersistentConnection(PORT);
    ZooKeeperEphemeralStore<String> writerStore = ZkStoreTestOnlyUtil.getZooKeeperEphemeralStore(PORT);
    writerStore.put(TEST_ZK_PROP_NAME, "randomValue");
    propertyEventBus.register(Collections.singleton(TEST_ZK_PROP_NAME), new PropertyEventSubscriber<String>() {

        @Override
        public void onInitialize(String propertyName, String propertyValue) {
            if (propertyName != null) {
                initializedLatch.countDown();
            }
        }

        @Override
        public void onAdd(String propertyName, String propertyValue) {
            if (propertyName != null) {
                addLatch.countDown();
            }
        }

        @Override
        public void onRemove(String propertyName) {
        }
    });
    initializedLatch.await(5, TimeUnit.SECONDS);
    if (initializedLatch.getCount() != 0) {
        fail("Initialized not received");
    }
    if (addLatch.getCount() == 0) {
        fail("This should have not been invoked yet");
    }
    // value of previous session id
    long oldSessionId = zkPersistentConnection.getZooKeeper().getSessionId();
    ZKTestUtil.expireSession("localhost:" + PORT, zkPersistentConnection.getZooKeeper(), 30, TimeUnit.SECONDS);
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 5, TimeUnit.SECONDS);
    // when connection gets restarted, the properties are fetched again and re-registered on the bus
    addLatch.await(5, TimeUnit.SECONDS);
    if (addLatch.getCount() != 0) {
        fail("Reading last value after expiration not happened");
    }
    // shutting down
    final FutureCallback<None> shutdownCallback = new FutureCallback<>();
    store.shutdown(shutdownCallback);
    shutdownCallback.get(5, TimeUnit.SECONDS);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 7 with ZKPersistentConnection

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

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

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

Example 10 with ZKPersistentConnection

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

the class ZookeeperConnectionManagerTest method testMarkUpDuringSessionExpiration.

@Test(invocationCount = 10, timeOut = 10000)
public void testMarkUpDuringSessionExpiration() throws Exception {
    // set up
    final double newWeight = 1.5d;
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZKPersistentConnection zkPersistentConnection = getZkPersistentConnection();
    ZooKeeperConnectionManager manager = createManager(true, zkPersistentConnection, announcer);
    // the new WEIGHT will be picked up only if the connection is re-established
    announcer.setWeight(newWeight);
    // expiring the connection
    long oldSessionId = zkPersistentConnection.getZooKeeper().getSessionId();
    ZKTestUtil.expireSession("localhost:" + PORT, zkPersistentConnection.getZooKeeper(), 10, TimeUnit.SECONDS);
    // making sure that a new connection has been established.
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 10, TimeUnit.SECONDS);
    // validation
    ZooKeeperEphemeralStore<UriProperties> store = createAndStartUriStore();
    AssertionMethods.assertWithTimeout(1000, () -> {
        UriProperties properties = store.get(_cluster);
        assertNotNull(properties);
        if (properties.getPartitionDataMap(URI.create(_uri)) == null) {
            Assert.fail("Supposed to have the uri present in ZK");
        }
        assertEquals(properties.getPartitionDataMap(URI.create(_uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), newWeight);
        assertEquals(properties.Uris().size(), 1);
    });
    shutdownManager(manager);
}
Also used : ZKPersistentConnection(com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Test(org.testng.annotations.Test)

Aggregations

UriProperties (com.linkedin.d2.balancer.properties.UriProperties)5 Test (org.testng.annotations.Test)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 None (com.linkedin.common.util.None)3 FileStore (com.linkedin.d2.discovery.stores.file.FileStore)3 LastSeenZKStore (com.linkedin.d2.discovery.stores.zk.LastSeenZKStore)3 ZKPersistentConnection (com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection)3 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)2 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2 ZooKeeperConnectionManager (com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager)2 PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 MultiCallback (com.linkedin.common.callback.MultiCallback)1 D2Client (com.linkedin.d2.balancer.D2Client)1 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)1 LastSeenBalancerWithFacilitiesFactory (com.linkedin.d2.balancer.LastSeenBalancerWithFacilitiesFactory)1 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)1 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)1 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)1