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