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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations