use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.
the class SharedZkConnectionProviderTest method shutdownConnectionManagers.
private void shutdownConnectionManagers(List<ZooKeeperConnectionManager> managers) throws Exception {
FutureCallback<None> shutdownCallback = new FutureCallback<>();
Callback<None> shutdownMulitCallback = Callbacks.countDown(shutdownCallback, managers.size());
for (ZooKeeperConnectionManager manager : managers) {
_threadPoolExecutor.submit(() -> manager.shutdown(shutdownMulitCallback));
}
shutdownCallback.get(BLOCKING_CALL_TIMEOUT, TimeUnit.MILLISECONDS);
}
use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.
the class RestLiExampleD2Server method main.
public static void main(String[] args) throws Exception {
// write D2-related configuration to ZooKeeper
// all the configuration here are permanent, no need to re-write as long as ZooKeeper still has the data
// therefore in real case, do this "discovery" step separately
final HttpServer server = RestLiExampleBasicServer.createServer();
final ZooKeeperConnectionManager zkConn = createZkConn();
startServer(server, zkConn);
System.out.println("Example server with D2 is running with URL " + RestLiExampleBasicServer.getServerUrl() + ". Press any key to stop server.");
System.in.read();
stopServer(server, zkConn);
}
use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.
the class SharedZkConnectionProviderTest method testAnnouncerNoStartup.
/**
* Hosts should only be announced when ZookeeperConnectionManager is started.
*/
@Test(groups = "needZk")
public void testAnnouncerNoStartup() throws Exception {
List<URI> hosts = prepareHostNames(5, "testAnnouncerNoStartup");
List<ZooKeeperConnectionManager> connectionManagers = prepareConnectionManagers(hosts);
List<ZooKeeperConnectionManager> managersToStart = connectionManagers.subList(0, 3);
assertEquals(_provider.getZkConnectionCount(), 1);
startConnectionManagers(connectionManagers.subList(0, 3));
// verify that only three managers are started.
UriProperties properties = _verificationStore.get(CLUSTER_NAME);
assertNotNull(properties);
assertEquals(properties.Uris().size(), 3);
shutdownConnectionManagers(connectionManagers);
}
use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.
the class SharedZkConnectionProviderTest method testMarkUpAndMarkDownSharingConnection.
/**
* Test both markUp and markDown when using only one connection.
*/
@Test(groups = "needZk")
public void testMarkUpAndMarkDownSharingConnection() throws Exception {
List<URI> hostNames = prepareHostNames(5, "testMarkUpAndMarkDownSharingConnection");
List<ZooKeeperConnectionManager> connectionManagers = prepareConnectionManagers(hostNames);
// announce all five hosts
startConnectionManagers(connectionManagers);
UriProperties properties = _verificationStore.get(CLUSTER_NAME);
assertNotNull(properties);
assertEquals(properties.Uris().size(), 5);
FutureCallback<None> markdownCallback = new FutureCallback<>();
Callback<None> markdownMultiCallback = Callbacks.countDown(markdownCallback, 2);
// markdown three hosts
for (ZooKeeperConnectionManager manager : connectionManagers.subList(0, 2)) {
_threadPoolExecutor.submit(() -> {
try {
// Using random sleep to introduce delay to simulate uncertainty during real environment.
Thread.sleep(Math.abs(new Random().nextInt()) % 100);
manager.getAnnouncers()[0].markDown(markdownMultiCallback);
} catch (Exception e) {
markdownMultiCallback.onError(new RuntimeException("MarkDown failed for host: " + manager.getAnnouncers()[0].getUri() + "due to: " + e.getMessage(), e));
}
});
}
markdownCallback.get(BLOCKING_CALL_TIMEOUT, TimeUnit.MILLISECONDS);
UriProperties newProperties = _verificationStore.get(CLUSTER_NAME);
assertNotNull(newProperties);
assertEquals(newProperties.Uris().size(), 3);
shutdownConnectionManagers(connectionManagers);
}
use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.
the class SharedZkConnectionProviderTest method prepareConnectionManagers.
/**
* For each given uri, generate a zookeeperConnectionManager for announcement
*/
private List<ZooKeeperConnectionManager> prepareConnectionManagers(List<URI> hostNames) throws Exception {
List<ZooKeeperConnectionManager> connectionManagers = new ArrayList<>();
for (URI uri : hostNames) {
ZooKeeperServer server = new ZooKeeperServer();
ZooKeeperAnnouncer announcer = new ZooKeeperAnnouncer(server, true);
announcer.setCluster(CLUSTER_NAME);
announcer.setUri(uri.toString());
Map<Integer, PartitionData> partitionWeight = new HashMap<>();
partitionWeight.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(0.5d));
announcer.setPartitionData(partitionWeight);
ZooKeeperConnectionManager.ZKStoreFactory<UriProperties, ZooKeeperEphemeralStore<UriProperties>> factory = new ZooKeeperUriStoreFactory();
ZKConnectionBuilder connectionBuilder = new ZKConnectionBuilder("localhost:" + ZK_PORT);
connectionBuilder.setTimeout(ZK_TIMEOUT);
ZKPersistentConnection connection = _provider.getZKPersistentConnection(connectionBuilder);
ZooKeeperConnectionManager connectionManager = new ZooKeeperConnectionManager(connection, ZKBASE_PATH, factory, announcer);
connectionManagers.add(connectionManager);
}
return connectionManagers;
}
Aggregations