Search in sources :

Example 6 with ZooKeeperConnectionManager

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);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 7 with ZooKeeperConnectionManager

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);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) HttpServer(com.linkedin.r2.transport.http.server.HttpServer)

Example 8 with ZooKeeperConnectionManager

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);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 9 with ZooKeeperConnectionManager

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);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) Random(java.util.Random) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) URI(java.net.URI) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 10 with ZooKeeperConnectionManager

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;
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZooKeeperUriStoreFactory(com.linkedin.d2.balancer.servers.ZooKeeperUriStoreFactory) ZooKeeperAnnouncer(com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ZooKeeperServer(com.linkedin.d2.balancer.servers.ZooKeeperServer)

Aggregations

Test (org.testng.annotations.Test)16 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)15 FutureCallback (com.linkedin.common.callback.FutureCallback)12 None (com.linkedin.common.util.None)12 ZooKeeperConnectionManager (com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager)11 URI (java.net.URI)6 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 TimeoutException (java.util.concurrent.TimeoutException)3 MultiCallback (com.linkedin.common.callback.MultiCallback)2 D2Client (com.linkedin.d2.balancer.D2Client)2 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)2 ZooKeeperAnnouncer (com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer)2 ZKPersistentConnection (com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection)2 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)2 List (java.util.List)2 Random (java.util.Random)2