Search in sources :

Example 11 with ZooKeeperEphemeralStore

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();
    }
}
Also used : ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) KeyMapper(com.linkedin.d2.balancer.KeyMapper) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) URI(java.net.URI) UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) FutureCallback(com.linkedin.common.callback.FutureCallback) HashSet(java.util.HashSet) ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ZooKeeperEphemeralStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Collection(java.util.Collection) None(com.linkedin.common.util.None) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 12 with ZooKeeperEphemeralStore

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;
}
Also used : ZooKeeperEphemeralStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore) None(com.linkedin.common.util.None) PropertyStringSerializer(com.linkedin.d2.discovery.stores.PropertyStringSerializer) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException)

Example 13 with ZooKeeperEphemeralStore

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");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 14 with ZooKeeperEphemeralStore

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;
}
Also used : UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) ZooKeeperEphemeralStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 15 with ZooKeeperEphemeralStore

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);
}
Also used : HashMap(java.util.HashMap) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

UriProperties (com.linkedin.d2.balancer.properties.UriProperties)17 None (com.linkedin.common.util.None)14 FutureCallback (com.linkedin.common.callback.FutureCallback)13 HashMap (java.util.HashMap)13 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)11 UriPropertiesMerger (com.linkedin.d2.balancer.properties.UriPropertiesMerger)11 ZooKeeperEphemeralStore (com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore)10 Test (org.testng.annotations.Test)10 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)9 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)9 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)9 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)7 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)7 ZooKeeperPermanentStore (com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore)6 ZKConnection (com.linkedin.d2.discovery.stores.zk.ZKConnection)5 PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)4 HashSet (java.util.HashSet)4 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)3 SimpleLoadBalancerState (com.linkedin.d2.balancer.simple.SimpleLoadBalancerState)3 PropertyStringSerializer (com.linkedin.d2.discovery.stores.PropertyStringSerializer)3