use of com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore in project rest.li by linkedin.
the class ZKFSTest method testKeyMapper.
@Test
public void testKeyMapper() throws Exception {
final String TEST_SERVICE_NAME = "test-service";
final String TEST_CLUSTER_NAME = "test-cluster";
final URI TEST_SERVER_URI1 = URI.create("http://test-host-1/");
final URI TEST_SERVER_URI2 = URI.create("http://test-host-2/");
final int NUM_ITERATIONS = 5;
startServer();
try {
ZKFSLoadBalancer balancer = getBalancer();
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(30, TimeUnit.SECONDS);
ZKConnection conn = balancer.zkConnection();
ZooKeeperPermanentStore<ServiceProperties> serviceStore = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, TEST_CLUSTER_NAME, "/test", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, Arrays.asList("http"), null);
serviceStore.put(TEST_SERVICE_NAME, props);
ClusterProperties clusterProperties = new ClusterProperties(TEST_CLUSTER_NAME);
ZooKeeperPermanentStore<ClusterProperties> clusterStore = new ZooKeeperPermanentStore<ClusterProperties>(conn, new ClusterPropertiesJsonSerializer(), ZKFSUtil.clusterPath(BASE_PATH));
clusterStore.put(TEST_CLUSTER_NAME, clusterProperties);
ZooKeeperEphemeralStore<UriProperties> uriStore = new ZooKeeperEphemeralStore<UriProperties>(conn, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), ZKFSUtil.uriPath(BASE_PATH), false, true);
Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1.0d));
uriData.put(TEST_SERVER_URI1, partitionData);
uriData.put(TEST_SERVER_URI2, partitionData);
UriProperties uriProps = new UriProperties(TEST_CLUSTER_NAME, uriData);
callback = new FutureCallback<None>();
uriStore.start(callback);
callback.get(30, TimeUnit.SECONDS);
uriStore.put(TEST_CLUSTER_NAME, uriProps);
Set<Integer> keys = new HashSet<Integer>();
for (int ii = 0; ii < 100; ++ii) {
keys.add(ii);
}
for (int ii = 0; ii < NUM_ITERATIONS; ++ii) {
KeyMapper mapper = balancer.getKeyMapper();
MapKeyResult<URI, Integer> batches = mapper.mapKeysV2(URI.create("d2://" + TEST_SERVICE_NAME), keys);
Assert.assertEquals(batches.getMapResult().size(), 2);
for (Map.Entry<URI, Collection<Integer>> oneBatch : batches.getMapResult().entrySet()) {
Assert.assertTrue(oneBatch.getKey().toString().startsWith("http://test-host-"));
Assert.assertTrue(keys.containsAll(oneBatch.getValue()));
}
}
} finally {
stopServer();
}
}
use of com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore in project rest.li by linkedin.
the class ZooKeeperEphemeralStorePublisherTest method getStore.
@Override
protected ZooKeeperStore<String> getStore() {
ZooKeeperEphemeralStore<String> store = new ZooKeeperEphemeralStore<String>(getConnection(), new PropertyStringSerializer(), _merger, "/testing/testPath", false, true);
try {
FutureCallback<None> callback = new FutureCallback<None>();
store.start(callback);
callback.get(30, TimeUnit.SECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
}
return store;
}
use of com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore in project rest.li by linkedin.
the class ZooKeeperEphemeralStoreTest method testPutGetRemovePartial.
@Test(groups = { "small", "back-end" })
public void testPutGetRemovePartial() throws InterruptedException, IOException, PropertyStoreException, ExecutionException {
ZooKeeperEphemeralStore<String> store = getStore();
store.put("service-1", "1");
store.put("service-1", "2");
store.put("service-2", "3");
assertTrue(store.get("service-1").equals("1,2") || store.get("service-1").equals("2,1"));
assertEquals(store.get("service-2"), "3");
assertNull(store.get("service-3"));
store.removePartial("service-1", "2");
assertEquals(store.get("service-1"), "1");
store.remove("service-2");
assertNull(store.get("service-2"));
final CountDownLatch latch = new CountDownLatch(1);
store.shutdown(new PropertyEventShutdownCallback() {
@Override
public void done() {
latch.countDown();
}
});
if (!latch.await(5, TimeUnit.SECONDS)) {
fail("unable to shut down");
}
}
use of com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore in project rest.li by linkedin.
the class ZookeeperConnectionManagerTest method createAndStartUriStore.
private ZooKeeperEphemeralStore<UriProperties> createAndStartUriStore() throws IOException, ExecutionException, InterruptedException {
ZKConnection zkClient = new ZKConnection("localhost:" + PORT, 5000);
zkClient.start();
ZooKeeperEphemeralStore<UriProperties> store = new ZooKeeperEphemeralStore<UriProperties>(zkClient, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), "/d2/uris");
FutureCallback<None> callback = new FutureCallback<None>();
store.start(callback);
callback.get();
return store;
}
use of com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore 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);
}
Aggregations