Search in sources :

Example 11 with ZKConnection

use of com.linkedin.d2.discovery.stores.zk.ZKConnection 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 ZKConnection

use of com.linkedin.d2.discovery.stores.zk.ZKConnection in project rest.li by linkedin.

the class ZooKeeperChildrenDataPublisherTest method testChildDataChanged.

@Test
public void testChildDataChanged() throws IOException, InterruptedException, ExecutionException {
    ZKConnection client = new ZKConnection("localhost:" + _port, 5000);
    client.start();
    final ZooKeeperChildrenDataPublisher<Map<String, String>, String> publisher = new ZooKeeperChildrenDataPublisher<Map<String, String>, String>(client, new PropertyStringSerializer(), "/");
    final CountDownLatch initLatch = new CountDownLatch(1);
    final CountDownLatch addLatch = new CountDownLatch(1);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final PropertyEventSubscriber<Map<String, String>> subscriber = new PropertyEventSubscriber<Map<String, String>>() {

        @Override
        public void onInitialize(String propertyName, Map<String, String> propertyValue) {
            initLatch.countDown();
        }

        @Override
        public void onAdd(String propertyName, Map<String, String> propertyValue) {
            _outputData = propertyValue;
            addLatch.countDown();
        }

        @Override
        public void onRemove(String propertyName) {
        }
    };
    publisher.start(new Callback<None>() {

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onSuccess(None result) {
            _eventBus = new PropertyEventBusImpl<Map<String, String>>(_executor, publisher);
            _eventBus.register(Collections.singleton("bucket"), subscriber);
            startLatch.countDown();
        }
    });
    if (!startLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to start ZookeeperChildrenDataPublisher");
    }
    if (!initLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to publish initial property value");
    }
    FutureCallback<None> callback = new FutureCallback<None>();
    _zkClient.setDataUnsafe("/bucket/child-1", "4".getBytes(), callback);
    callback.get();
    if (!addLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to get publish initialized property value");
    }
    _testData.put("bucket/child-1", "4");
    assertEquals(_outputData, _testData);
    _eventBus.unregister(Collections.singleton("bucket"), subscriber);
    client.shutdown();
}
Also used : PropertyEventSubscriber(com.linkedin.d2.discovery.event.PropertyEventSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) HashMap(java.util.HashMap) Map(java.util.Map) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 13 with ZKConnection

use of com.linkedin.d2.discovery.stores.zk.ZKConnection in project rest.li by linkedin.

the class ZooKeeperChildrenDataPublisherTest method testChildDeletion.

@Test
public void testChildDeletion() throws IOException, InterruptedException, ExecutionException {
    ZKConnection client = new ZKConnection("localhost:" + _port, 5000);
    client.start();
    final ZooKeeperChildrenDataPublisher<Map<String, String>, String> publisher = new ZooKeeperChildrenDataPublisher<Map<String, String>, String>(client, new PropertyStringSerializer(), "/");
    final CountDownLatch initLatch = new CountDownLatch(1);
    final CountDownLatch addLatch = new CountDownLatch(1);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final PropertyEventSubscriber<Map<String, String>> subscriber = new PropertyEventSubscriber<Map<String, String>>() {

        @Override
        public void onInitialize(String propertyName, Map<String, String> propertyValue) {
            initLatch.countDown();
        }

        @Override
        public void onAdd(String propertyName, Map<String, String> propertyValue) {
            _outputData = propertyValue;
            addLatch.countDown();
        }

        @Override
        public void onRemove(String propertyName) {
        }
    };
    publisher.start(new Callback<None>() {

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onSuccess(None result) {
            _eventBus = new PropertyEventBusImpl<Map<String, String>>(_executor, publisher);
            _eventBus.register(Collections.singleton("bucket"), subscriber);
            startLatch.countDown();
        }
    });
    if (!startLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to start ZookeeperChildrenDataPublisher");
    }
    if (!initLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to publish initial property value");
    }
    FutureCallback<None> callback = new FutureCallback<None>();
    _zkClient.removeNodeUnsafe("/bucket/child-1", callback);
    callback.get();
    if (!addLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to get publish initialized property value");
    }
    _testData.remove("bucket/child-1");
    assertEquals(_outputData, _testData);
    _eventBus.unregister(Collections.singleton("bucket"), subscriber);
    client.shutdown();
}
Also used : PropertyEventSubscriber(com.linkedin.d2.discovery.event.PropertyEventSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) HashMap(java.util.HashMap) Map(java.util.Map) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 14 with ZKConnection

use of com.linkedin.d2.discovery.stores.zk.ZKConnection in project rest.li by linkedin.

the class ZooKeeperPermanentStoreTest method getStore.

@Override
public PropertyStore<String> getStore() throws PropertyStoreException {
    try {
        ZKConnection client = new ZKConnection("localhost:" + PORT, 30000);
        client.start();
        ZooKeeperPermanentStore<String> store = new ZooKeeperPermanentStore<String>(client, new PropertyStringSerializer(), "/test-path");
        FutureCallback<None> callback = new FutureCallback<None>();
        store.start(callback);
        callback.get();
        return store;
    } catch (Exception e) {
        throw new PropertyStoreException(e);
    }
}
Also used : PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException) None(com.linkedin.common.util.None) PropertyStringSerializer(com.linkedin.d2.discovery.stores.PropertyStringSerializer) FutureCallback(com.linkedin.common.callback.FutureCallback) IOException(java.io.IOException) PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException)

Example 15 with ZKConnection

use of com.linkedin.d2.discovery.stores.zk.ZKConnection in project rest.li by linkedin.

the class ZKFSTest method testServiceDirectory.

@Test
public void testServiceDirectory() throws Exception {
    final String TEST_SERVICE_NAME = "testingService";
    startServer();
    try {
        ZKFSLoadBalancer balancer = getBalancer();
        FutureCallback<None> callback = new FutureCallback<None>();
        balancer.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        Directory dir = balancer.getDirectory();
        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, "someCluster", "/somePath", Arrays.asList("someStrategy"));
        store.put(TEST_SERVICE_NAME, props);
        FutureCallback<List<String>> serviceCallback = new FutureCallback<List<String>>();
        dir.getServiceNames(serviceCallback);
        Assert.assertEquals(serviceCallback.get(30, TimeUnit.SECONDS), Collections.singletonList(TEST_SERVICE_NAME));
    } finally {
        stopServer();
    }
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) List(java.util.List) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Directory(com.linkedin.d2.balancer.Directory) Test(org.testng.annotations.Test)

Aggregations

None (com.linkedin.common.util.None)15 FutureCallback (com.linkedin.common.callback.FutureCallback)12 ZKConnection (com.linkedin.d2.discovery.stores.zk.ZKConnection)11 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)10 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)10 HashMap (java.util.HashMap)10 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)9 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)9 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)9 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)9 UriPropertiesMerger (com.linkedin.d2.balancer.properties.UriPropertiesMerger)9 PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)9 ZooKeeperPermanentStore (com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore)9 Test (org.testng.annotations.Test)9 ZooKeeperEphemeralStore (com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore)7 HashSet (java.util.HashSet)5 Map (java.util.Map)5 PropertyEventSubscriber (com.linkedin.d2.discovery.event.PropertyEventSubscriber)4 PropertyStringSerializer (com.linkedin.d2.discovery.stores.PropertyStringSerializer)4 URI (java.net.URI)4