use of com.linkedin.d2.balancer.servers.ZooKeeperServer in project rest.li by linkedin.
the class ZKFSTest method testZKDown.
@Test
public void testZKDown() throws Exception {
final String TEST_SERVICE_NAME = "testingService";
final String TEST_CLUSTER_NAME = "someCluster";
startServer();
try {
ZKFSLoadBalancer balancer = getBalancer();
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(30, TimeUnit.SECONDS);
ZKConnection conn = new ZKConnection("localhost:" + PORT, 30000);
conn.start();
ZooKeeperPermanentStore<ServiceProperties> store = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
callback = new FutureCallback<None>();
store.start(callback);
callback.get(30, TimeUnit.SECONDS);
ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, TEST_CLUSTER_NAME, "/somePath", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, Arrays.asList("http"), null);
store.put(TEST_SERVICE_NAME, props);
ZooKeeperPermanentStore<ClusterProperties> clusterStore = new ZooKeeperPermanentStore<ClusterProperties>(conn, new ClusterPropertiesJsonSerializer(), ZKFSUtil.clusterPath(BASE_PATH));
callback = new FutureCallback<None>();
clusterStore.start(callback);
callback.get(30, TimeUnit.SECONDS);
ClusterProperties clusterProps = new ClusterProperties("someCluster");
clusterStore.put(TEST_CLUSTER_NAME, clusterProps);
ZKConnection serverConn = new ZKConnection("localhost:" + PORT, 30000);
serverConn.start();
ZooKeeperEphemeralStore<UriProperties> uriStore = new ZooKeeperEphemeralStore<UriProperties>(serverConn, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), ZKFSUtil.uriPath(BASE_PATH));
callback = new FutureCallback<None>();
uriStore.start(callback);
callback.get(30, TimeUnit.SECONDS);
ZooKeeperServer server = new ZooKeeperServer(uriStore);
callback = new FutureCallback<None>();
Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>();
partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1.0));
server.markUp(TEST_CLUSTER_NAME, URI.create("http://test.uri"), partitionDataMap, callback);
callback.get(30, TimeUnit.SECONDS);
URIRequest request = new URIRequest("d2://" + TEST_SERVICE_NAME + "/foo");
TransportClient client = balancer.getClient(request, new RequestContext());
// Stop the server to cause a disconnect event
stopServer();
// Sleep to ensure the disconnect has propagated; ideally the Toggle should expose
// some interface to allow detection that the toggle occurred
Thread.sleep(1000);
// Now see if it still works
client = balancer.getClient(request, new RequestContext());
} finally {
stopServer();
}
}
use of com.linkedin.d2.balancer.servers.ZooKeeperServer in project rest.li by linkedin.
the class ZookeeperConnectionManagerTest method testDelayMarkUp.
@Test
public void testDelayMarkUp() throws IOException, ExecutionException, InterruptedException, PropertyStoreException {
final String uri = "http://cluster-1/test";
final String cluster = "cluster-1";
final double weight = 0.5d;
ZooKeeperAnnouncer announcer = new ZooKeeperAnnouncer(new ZooKeeperServer(), false);
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);
assertNull(properties);
FutureCallback<None> markUpCallback = new FutureCallback<None>();
announcer.markUp(markUpCallback);
markUpCallback.get();
UriProperties propertiesAfterMarkUp = store.get(cluster);
assertNotNull(propertiesAfterMarkUp);
assertEquals(propertiesAfterMarkUp.getPartitionDataMap(URI.create(uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), weight);
assertEquals(propertiesAfterMarkUp.Uris().size(), 1);
}
use of com.linkedin.d2.balancer.servers.ZooKeeperServer in project rest.li by linkedin.
the class ZookeeperConnectionManagerTest method testMarkUp.
@Test
public void testMarkUp() throws IOException, ExecutionException, InterruptedException, PropertyStoreException {
final String uri = "http://cluster-1/test";
final String cluster = "cluster-1";
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);
}
use of com.linkedin.d2.balancer.servers.ZooKeeperServer 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.ZooKeeperServer 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;
}
Aggregations