Search in sources :

Example 6 with ZooKeeperAnnouncer

use of com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer in project rest.li by linkedin.

the class ZookeeperConnectionManagerTest method testMarkDownAndUpDuringDisconnection.

@Test
public void testMarkDownAndUpDuringDisconnection() throws IOException, ExecutionException, InterruptedException, PropertyStoreException, TimeoutException {
    final String uri = "http://cluster-5/test";
    final String cluster = "cluster-5";
    final double weight = 0.5d;
    ZooKeeperAnnouncer announcer = new ZooKeeperAnnouncer(new ZooKeeperServer());
    announcer.setCluster(cluster);
    announcer.setUri(uri);
    Map<Integer, PartitionData> partitionWeight = new HashMap<Integer, PartitionData>();
    partitionWeight.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(weight));
    announcer.setPartitionData(partitionWeight);
    ZooKeeperConnectionManager manager = createManager(announcer);
    FutureCallback<None> managerStartCallback = new FutureCallback<None>();
    manager.start(managerStartCallback);
    managerStartCallback.get();
    ZooKeeperEphemeralStore<UriProperties> store = createAndStartUriStore();
    UriProperties properties = store.get(cluster);
    assertNotNull(properties);
    assertEquals(properties.getPartitionDataMap(URI.create(uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), weight);
    assertEquals(properties.Uris().size(), 1);
    _zkServer.shutdown(false);
    FutureCallback<None> markDownCallback = new FutureCallback<None>();
    announcer.markDown(markDownCallback);
    FutureCallback<None> markUpCallback = new FutureCallback<None>();
    announcer.markUp(markUpCallback);
    // ugly, but we need to wait for a while just so that Disconnect event is propagated
    // to the caller before we restart zk sever.
    Thread.sleep(1000);
    _zkServer.restart();
    markUpCallback.get(10, TimeUnit.SECONDS);
    try {
        markDownCallback.get();
        Assert.fail("mark down should have thrown CancellationException.");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof CancellationException);
    }
    properties = store.get(cluster);
    assertNotNull(properties);
    assertEquals(properties.getPartitionDataMap(URI.create(uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), weight);
    assertEquals(properties.Uris().size(), 1);
}
Also used : HashMap(java.util.HashMap) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) CancellationException(java.util.concurrent.CancellationException) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 7 with ZooKeeperAnnouncer

use of com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer in project rest.li by linkedin.

the class ExampleD2Server method createZkManager.

private static ZooKeeperConnectionManager createZkManager(JSONObject json, ZooKeeperAnnouncer[] zkAnnouncers) {
    ZKUriStoreFactory factory = new ZKUriStoreFactory();
    String zkConnectString = (String) json.get("zkConnectString");
    int zkRetryLimit = ((Long) json.get("zkRetryLimit")).intValue();
    int zkSessionTimeout = ((Long) json.get("zkSessionTimeout")).intValue();
    String zkBasePath = (String) json.get("zkBasePath");
    return new ZooKeeperConnectionManager(zkConnectString, zkSessionTimeout, zkBasePath, factory, zkRetryLimit, zkAnnouncers);
}
Also used : ZKUriStoreFactory(com.linkedin.d2.balancer.servers.ZKUriStoreFactory) ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager)

Example 8 with ZooKeeperAnnouncer

use of com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer in project rest.li by linkedin.

the class ExampleD2Server method createZkAnnouncers.

private static ZooKeeperAnnouncer[] createZkAnnouncers(List<Map<String, Object>> d2ServersConfigs) {
    ZooKeeperAnnouncer[] zkAnnouncers = new ZooKeeperAnnouncer[d2ServersConfigs.size()];
    for (int i = 0; i < d2ServersConfigs.size(); i++) {
        Map<String, Object> d2ServerConfig = d2ServersConfigs.get(i);
        ZooKeeperServer server = new ZooKeeperServer();
        ZooKeeperAnnouncer announcer = new ZooKeeperAnnouncer(server);
        Map<String, Object> partitionDataMap = (Map<String, Object>) d2ServerConfig.get("partitionData");
        announcer.setCluster((String) d2ServerConfig.get("d2Cluster"));
        announcer.setUri((String) d2ServerConfig.get("serverUri"));
        announcer.setWeightOrPartitionData(PartitionDataFactory.createPartitionDataMap(partitionDataMap));
        zkAnnouncers[i] = announcer;
    }
    return zkAnnouncers;
}
Also used : ZooKeeperAnnouncer(com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) ZooKeeperServer(com.linkedin.d2.balancer.servers.ZooKeeperServer)

Example 9 with ZooKeeperAnnouncer

use of com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer in project rest.li by linkedin.

the class ExampleD2Server method main.

public static void main(String[] args) throws IOException, ParseException {
    //get server configuration
    JSONObject json = parseConfig();
    System.out.println("Finished parsing the server config");
    List<Map<String, Object>> d2ServersConfigs = (List<Map<String, Object>>) json.get("d2Servers");
    List<Map<String, Object>> echoServerConfigs = (List<Map<String, Object>>) json.get("echoServers");
    Long timeout = (Long) json.get("announcerStartTimeout");
    Long shutdownTimeout = (Long) json.get("announcerShutdownTimeout");
    //create and start echo servers
    List<EchoServer> echoServers = createAndStartEchoServers(echoServerConfigs);
    System.out.println("Finished creating echo servers");
    //create d2 announcers (for announcing the existence of these servers to the world)
    ZooKeeperAnnouncer[] zkAnnouncers = createZkAnnouncers(d2ServersConfigs);
    //manager will keep track of the lifecycle of d2 announcers i.e. start publishing
    //once connected to zookeeper, reconnect if zookeeper is disconnected, etc
    ZooKeeperConnectionManager manager = createZkManager(json, zkAnnouncers);
    System.out.println("Starting zookeeper announcers");
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    startAnnouncers(manager, executorService, timeout);
    //waiting for user to turn off this process
    System.in.read();
    System.out.println("shutting down...");
    shutdown(echoServers, manager, executorService, shutdownTimeout);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) ZooKeeperAnnouncer(com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer) JSONObject(org.json.simple.JSONObject) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.simple.JSONObject) Map(java.util.Map)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)6 None (com.linkedin.common.util.None)6 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)6 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)6 HashMap (java.util.HashMap)6 Test (org.testng.annotations.Test)6 ZooKeeperAnnouncer (com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer)2 ZooKeeperConnectionManager (com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager)2 Map (java.util.Map)2 JSONObject (org.json.simple.JSONObject)2 ZKUriStoreFactory (com.linkedin.d2.balancer.servers.ZKUriStoreFactory)1 ZooKeeperServer (com.linkedin.d2.balancer.servers.ZooKeeperServer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1