Search in sources :

Example 1 with PropertyEventSubscriber

use of com.linkedin.d2.discovery.event.PropertyEventSubscriber 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 2 with PropertyEventSubscriber

use of com.linkedin.d2.discovery.event.PropertyEventSubscriber 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 3 with PropertyEventSubscriber

use of com.linkedin.d2.discovery.event.PropertyEventSubscriber in project rest.li by linkedin.

the class ZooKeeperChildrenDataPublisherTest method testChildCreation.

@Test
public void testChildCreation() 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.ensurePersistentNodeExists("/bucket/child-4", callback);
    callback.get();
    if (!addLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to get publish initialized property value");
    }
    _testData.put("bucket/child-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 4 with PropertyEventSubscriber

use of com.linkedin.d2.discovery.event.PropertyEventSubscriber in project rest.li by linkedin.

the class ZooKeeperChildrenDataPublisherTest method testPublishInitialize.

@Test
public void testPublishInitialize() throws InterruptedException, IOException, PropertyStoreException, 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 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) {
            _outputData = propertyValue;
            initLatch.countDown();
        }

        @Override
        public void onAdd(String propertyName, Map<String, String> propertyValue) {
        }

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

        @Override
        public void onError(Throwable e) {
            Assert.fail("publisher start onError called", 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");
    }
    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) Test(org.testng.annotations.Test)

Example 5 with PropertyEventSubscriber

use of com.linkedin.d2.discovery.event.PropertyEventSubscriber in project rest.li by linkedin.

the class ZooKeeperEphemeralStoreStrawMan method main.

public static void main(String[] args) throws IOException, InterruptedException, PropertyStoreException {
    ZKConnection zkClient = new ZKConnection("localhost:2181", 30000);
    PropertyStringMerger merger = new PropertyStringMerger();
    Set<String> listenTos = new HashSet<String>();
    ZooKeeperEphemeralStore<String> zk = new ZooKeeperEphemeralStore<String>(zkClient, new PropertyStringSerializer(), merger, "/test/lb/test-property-ephemeral", false, true);
    listenTos.add("foo12");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    PropertyEventBus<String> bus = new PropertyEventBusImpl<String>(executorService, zk);
    bus.register(listenTos, new PropertyEventSubscriber<String>() {

        @Override
        public void onAdd(String propertyName, String propertyValue) {
            System.err.println("onAdd: " + propertyName + "\t" + propertyValue);
        }

        @Override
        public void onInitialize(String propertyName, String propertyValue) {
            System.err.println("onInitialize: " + propertyName + "\t" + propertyValue);
        }

        @Override
        public void onRemove(String propertyName) {
            System.err.println("onRemove: " + propertyName);
        }
    });
    zk.put("foo12", "TEST1");
    zk.put("foo12", "TEST2");
    zk.put("foo12", "TEST3");
    zk.put("foo12", "TEST4");
    zk.put("foo12", "TEST5");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    zk.remove("foo12");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    zkClient.getZooKeeper().close();
    executorService.shutdown();
}
Also used : PropertyStringMerger(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStoreTest.PropertyStringMerger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PropertyStringSerializer(com.linkedin.d2.discovery.stores.PropertyStringSerializer) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) HashSet(java.util.HashSet)

Aggregations

PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)6 None (com.linkedin.common.util.None)4 PropertyEventSubscriber (com.linkedin.d2.discovery.event.PropertyEventSubscriber)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.testng.annotations.Test)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 PropertyStringSerializer (com.linkedin.d2.discovery.stores.PropertyStringSerializer)2 HashSet (java.util.HashSet)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 PropertyStringMerger (com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStoreTest.PropertyStringMerger)1