Search in sources :

Example 1 with EventListner

use of org.apache.pulsar.broker.zookeeper.aspectj.ClientCnxnAspect.EventListner in project incubator-pulsar by apache.

the class ZooKeeperClientAspectJTest method testZkClientAspectJTrigger.

/**
 * Verifies that aspect-advice calculates the latency of of zk-operation
 *
 * @throws Exception
 */
@Test(enabled = false, timeOut = 7000)
void testZkClientAspectJTrigger() throws Exception {
    OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
    ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
    CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
    localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    try {
        assertTrue(localZkc.getState().isConnected());
        assertNotEquals(localZkc.getState(), States.CONNECTEDREADONLY);
        final AtomicInteger writeCount = new AtomicInteger(0);
        final AtomicInteger readCount = new AtomicInteger(0);
        EventListner listener = new EventListner() {

            @Override
            public void recordLatency(EventType eventType, long latencyMiliSecond) {
                if (eventType.equals(EventType.write)) {
                    writeCount.incrementAndGet();
                } else if (eventType.equals(EventType.read)) {
                    readCount.incrementAndGet();
                }
            }
        };
        ClientCnxnAspect.addListener(listener);
        CountDownLatch createLatch = new CountDownLatch(1);
        CountDownLatch deleteLatch = new CountDownLatch(1);
        CountDownLatch readLatch = new CountDownLatch(1);
        CountDownLatch existLatch = new CountDownLatch(1);
        localZkc.create("/createTest", "data".getBytes(), Acl, CreateMode.EPHEMERAL, (rc, path, ctx, name) -> {
            createLatch.countDown();
        }, "create");
        localZkc.delete("/deleteTest", -1, (rc, path, ctx) -> {
            deleteLatch.countDown();
        }, "delete");
        localZkc.exists("/createTest", null, (int rc, String path, Object ctx, Stat stat) -> {
            existLatch.countDown();
        }, null);
        localZkc.getData("/createTest", null, (int rc, String path, Object ctx, byte[] data, Stat stat) -> {
            readLatch.countDown();
        }, null);
        createLatch.await();
        deleteLatch.await();
        existLatch.await();
        readLatch.await();
        Thread.sleep(500);
        Assert.assertEquals(writeCount.get(), 2);
        Assert.assertEquals(readCount.get(), 2);
        ClientCnxnAspect.removeListener(listener);
    } finally {
        if (localZkc != null) {
            localZkc.close();
        }
        executor.shutdown();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) EventListner(org.apache.pulsar.broker.zookeeper.aspectj.ClientCnxnAspect.EventListner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventType(org.apache.pulsar.broker.zookeeper.aspectj.ClientCnxnAspect.EventType) ZookeeperBkClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl) CountDownLatch(java.util.concurrent.CountDownLatch) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) ZooKeeperClientFactory(org.apache.pulsar.zookeeper.ZooKeeperClientFactory) Test(org.testng.annotations.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)1 EventListner (org.apache.pulsar.broker.zookeeper.aspectj.ClientCnxnAspect.EventListner)1 EventType (org.apache.pulsar.broker.zookeeper.aspectj.ClientCnxnAspect.EventType)1 ZooKeeperClientFactory (org.apache.pulsar.zookeeper.ZooKeeperClientFactory)1 ZookeeperBkClientFactoryImpl (org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 Stat (org.apache.zookeeper.data.Stat)1 Test (org.testng.annotations.Test)1