Search in sources :

Example 6 with ZooKeeperClientFactory

use of org.apache.pulsar.zookeeper.ZooKeeperClientFactory in project incubator-pulsar by apache.

the class DiscoveryServiceServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    log.info("Initializing DiscoveryServiceServlet resource");
    String zookeeperServers = config.getInitParameter("zookeeperServers");
    String zookeeperSessionTimeoutMsStr = config.getInitParameter("zookeeperSessionTimeoutMs");
    int zookeeperSessionTimeoutMs = zookeeperSessionTimeoutMsStr != null ? Integer.valueOf(zookeeperSessionTimeoutMsStr) : 30_000;
    String zookeeperClientFactoryClassName = config.getInitParameter("zookeeperClientFactoryClass");
    if (zookeeperClientFactoryClassName == null) {
        zookeeperClientFactoryClassName = ZookeeperClientFactoryImpl.class.getName();
    }
    log.info("zookeeperServers={} zookeeperClientFactoryClass={}", zookeeperServers, zookeeperClientFactoryClassName);
    try {
        ZooKeeperClientFactory zkClientFactory = (ZooKeeperClientFactory) Class.forName(zookeeperClientFactoryClassName).newInstance();
        zkCache = new ZookeeperCacheLoader(zkClientFactory, zookeeperServers, zookeeperSessionTimeoutMs);
    } catch (Throwable t) {
        throw new ServletException(t);
    }
}
Also used : ServletException(javax.servlet.ServletException) ZookeeperClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl) ZooKeeperClientFactory(org.apache.pulsar.zookeeper.ZooKeeperClientFactory)

Example 7 with ZooKeeperClientFactory

use of org.apache.pulsar.zookeeper.ZooKeeperClientFactory in project incubator-pulsar by apache.

the class PulsarClusterMetadataSetup method main.

public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander();
    try {
        jcommander.addObject(arguments);
        jcommander.parse(args);
        if (arguments.help) {
            jcommander.usage();
            return;
        }
    } catch (Exception e) {
        jcommander.usage();
        throw e;
    }
    log.info("Setting up cluster {} with zk={} global-zk={}", arguments.cluster, arguments.zookeeper, arguments.globalZookeeper);
    // Format BookKeeper metadata
    ServerConfiguration bkConf = new ServerConfiguration();
    bkConf.setLedgerManagerFactoryClass(HierarchicalLedgerManagerFactory.class);
    bkConf.setZkServers(arguments.zookeeper);
    if (!BookKeeperAdmin.format(bkConf, false, /* interactive */
    false)) {
        throw new IOException("Failed to initialize BookKeeper metadata");
    }
    ZooKeeperClientFactory zkfactory = new ZookeeperClientFactoryImpl();
    ZooKeeper localZk = zkfactory.create(arguments.zookeeper, SessionType.ReadWrite, 30000).get();
    ZooKeeper globalZk = zkfactory.create(arguments.globalZookeeper, SessionType.ReadWrite, 30000).get();
    localZk.create("/managed-ledgers", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    localZk.create("/namespace", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    try {
        ZkUtils.createFullPathOptimistic(globalZk, POLICIES_ROOT, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (NodeExistsException e) {
    // Ignore
    }
    try {
        ZkUtils.createFullPathOptimistic(globalZk, "/admin/clusters", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (NodeExistsException e) {
    // Ignore
    }
    ClusterData clusterData = new ClusterData(arguments.clusterWebServiceUrl, arguments.clusterWebServiceUrlTls, arguments.clusterBrokerServiceUrl, arguments.clusterBrokerServiceUrlTls);
    byte[] clusterDataJson = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(clusterData);
    globalZk.create("/admin/clusters/" + arguments.cluster, clusterDataJson, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Create marker for "global" cluster
    ClusterData globalClusterData = new ClusterData(null, null);
    byte[] globalClusterDataJson = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(globalClusterData);
    try {
        globalZk.create("/admin/clusters/global", globalClusterDataJson, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (NodeExistsException e) {
    // Ignore
    }
    log.info("Cluster metadata for '{}' setup correctly", arguments.cluster);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ZookeeperClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl) JCommander(com.beust.jcommander.JCommander) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) IOException(java.io.IOException) IOException(java.io.IOException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ZooKeeperClientFactory(org.apache.pulsar.zookeeper.ZooKeeperClientFactory)

Example 8 with ZooKeeperClientFactory

use of org.apache.pulsar.zookeeper.ZooKeeperClientFactory in project incubator-pulsar by apache.

the class ZookeeperClientFactoryImplTest method testZKCreationFailure.

@Test
void testZKCreationFailure() throws Exception {
    ZooKeeperClientFactory zkf = new ZookeeperClientFactoryImpl();
    CompletableFuture<ZooKeeper> zkFuture = zkf.create("invalid", SessionType.ReadWrite, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
    assertTrue(zkFuture.isCompletedExceptionally());
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZookeeperClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl) ZooKeeperClientFactory(org.apache.pulsar.zookeeper.ZooKeeperClientFactory) Test(org.testng.annotations.Test)

Example 9 with ZooKeeperClientFactory

use of org.apache.pulsar.zookeeper.ZooKeeperClientFactory 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

ZooKeeperClientFactory (org.apache.pulsar.zookeeper.ZooKeeperClientFactory)9 ZooKeeper (org.apache.zookeeper.ZooKeeper)8 Test (org.testng.annotations.Test)6 ZookeeperClientFactoryImpl (org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl)5 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)4 ZookeeperBkClientFactoryImpl (org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl)4 JCommander (com.beust.jcommander.JCommander)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 PulsarClient (org.apache.pulsar.client.api.PulsarClient)2 Stat (org.apache.zookeeper.data.Stat)2 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletException (javax.servlet.ServletException)1 BookKeeper (org.apache.bookkeeper.client.BookKeeper)1 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)1 BookKeeperClientFactory (org.apache.pulsar.broker.BookKeeperClientFactory)1 BookKeeperClientFactoryImpl (org.apache.pulsar.broker.BookKeeperClientFactoryImpl)1