org.apache.bookkeeper.zookeeper.ZooKeeperClient Java Examples

The following examples show how to use org.apache.bookkeeper.zookeeper.ZooKeeperClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZKTestEnv.java    From herddb with Apache License 2.0 6 votes vote down vote up
public ZKTestEnv(Path path) throws Exception {
    zkServer = new TestingServer(1282, path.toFile(), true);
    this.path = path;

    try (ZooKeeperClient zkc = ZooKeeperClient
            .newBuilder()
            .connectString(zkServer.getConnectString())
            .sessionTimeoutMs(10000)
            .build()) {

        boolean rootExists = zkc.exists(getPath(), false) != null;

        if (!rootExists) {
            zkc.create(getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    }
}
 
Example #2
Source File: ZkIsolatedBookieEnsemblePlacementPolicy.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private ZooKeeperCache getAndSetZkCache(Configuration conf) {
    ZooKeeperCache zkCache = null;
    if (conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE) != null) {
        zkCache = (ZooKeeperCache) conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE);
    } else {
        int zkTimeout;
        String zkServers;
        if (conf instanceof ClientConfiguration) {
            zkTimeout = ((ClientConfiguration) conf).getZkTimeout();
            zkServers = ((ClientConfiguration) conf).getZkServers();
            try {
                ZooKeeper zkClient = ZooKeeperClient.newBuilder().connectString(zkServers)
                        .sessionTimeoutMs(zkTimeout).build();
                zkCache = new ZooKeeperCache("bookies-isolation", zkClient,
                        (int) TimeUnit.MILLISECONDS.toSeconds(zkTimeout)) {
                };
                conf.addProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, zkCache);
            } catch (Exception e) {
                LOG.error("Error creating zookeeper client", e);
            }
        } else {
            LOG.error("No zk configurations available");
        }
    }
    return zkCache;
}
 
Example #3
Source File: BookKeeperServiceRunner.java    From pravega with Apache License 2.0 5 votes vote down vote up
private void initializeZookeeper() throws Exception {
    log.info("Formatting ZooKeeper ...");

    if (this.secureZK) {
        ZKTLSUtils.setSecureZKClientProperties(this.tlsTrustStore, JKSHelper.loadPasswordFrom(this.tLSKeyStorePasswordPath));
    } else {
        ZKTLSUtils.unsetSecureZKClientProperties();
    }

    @Cleanup
    val zkc = ZooKeeperClient.newBuilder()
                             .connectString(LOOPBACK_ADDRESS.getHostAddress() + ":" + this.zkPort)
                             .sessionTimeoutMs(10000)
                             .build();

    String znode;
    StringBuilder znodePath = new StringBuilder();
    for (String z : this.ledgersPath.split("/")) {
        znodePath.append(z);
        znode = znodePath.toString();
        if (!znode.isEmpty()) {
            zkc.create(znode, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        znodePath.append("/");
    }

    znodePath.append("available");
    zkc.create(znodePath.toString(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example #4
Source File: ZkBookieRackAffinityMapping.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private ZooKeeperDataCache<BookiesRackConfiguration> getAndSetZkCache(Configuration conf) {
    ZooKeeperCache zkCache = null;
    if (conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE) != null) {
        zkCache = (ZooKeeperCache) conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE);
    } else {
        int zkTimeout;
        String zkServers;
        if (conf instanceof ClientConfiguration) {
            zkTimeout = ((ClientConfiguration) conf).getZkTimeout();
            zkServers = ((ClientConfiguration) conf).getZkServers();
            try {
                ZooKeeper zkClient = ZooKeeperClient.newBuilder().connectString(zkServers)
                        .sessionTimeoutMs(zkTimeout).build();
                zkCache = new ZooKeeperCache("bookies-racks", zkClient,
                        (int) TimeUnit.MILLISECONDS.toSeconds(zkTimeout)) {
                };
                conf.addProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, zkCache);
            } catch (Exception e) {
                LOG.error("Error creating zookeeper client", e);
            }
        } else {
            LOG.error("No zk configurations available");
        }
    }
    ZooKeeperDataCache<BookiesRackConfiguration> zkDataCache = getZkBookieRackMappingCache(
            zkCache);
    zkDataCache.registerListener(this);
    return zkDataCache;
}
 
Example #5
Source File: ZookeeperBkClientFactoryImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) {
    CompletableFuture<ZooKeeper> future = new CompletableFuture<>();

    executor.execute(safeRun(() -> {
        try {
            ZooKeeper zk = ZooKeeperClient.newBuilder().connectString(serverList)
                    .sessionTimeoutMs(zkSessionTimeoutMillis)
                    .connectRetryPolicy(new BoundExponentialBackoffRetryPolicy(zkSessionTimeoutMillis,
                            zkSessionTimeoutMillis, 0))
                    .build();

            if (zk.getState() == States.CONNECTEDREADONLY && sessionType != SessionType.AllowReadOnly) {
                zk.close();
                future.completeExceptionally(new IllegalStateException("Cannot use a read-only session"));
            }

            log.info("ZooKeeper session established: {}", zk);
            future.complete(zk);
        } catch (IOException | KeeperException | InterruptedException exception) {
            log.error("Failed to establish ZooKeeper session: {}", exception.getMessage());
            future.completeExceptionally(exception);
        }
    }, throwable -> {
        future.completeExceptionally(throwable);
    }));

    return future;
}
 
Example #6
Source File: ZkBookieRackAffinityMappingTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    localZkS = new ZookeeperServerTest(0);
    localZkS.start();
    localZkc = ZooKeeperClient.newBuilder().connectString("127.0.0.1:" + localZkS.getZookeeperPort()).build();
    BOOKIE1 = new BookieSocketAddress("127.0.0.1:3181");
    BOOKIE2 = new BookieSocketAddress("127.0.0.2:3181");
    BOOKIE3 = new BookieSocketAddress("127.0.0.3:3181");
}
 
Example #7
Source File: ZkIsolatedBookieEnsemblePlacementPolicyTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    timer = new HashedWheelTimer();
    localZkS = new ZookeeperServerTest(0);
    localZkS.start();

    localZkc = ZooKeeperClient.newBuilder().connectString("127.0.0.1" + ":" + localZkS.getZookeeperPort()).build();
    writableBookies.add(new BookieSocketAddress(BOOKIE1));
    writableBookies.add(new BookieSocketAddress(BOOKIE2));
    writableBookies.add(new BookieSocketAddress(BOOKIE3));
    writableBookies.add(new BookieSocketAddress(BOOKIE4));
    isolationGroups.add("group1");
}
 
Example #8
Source File: ManagedLedgerFactoryImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public ManagedLedgerFactoryImpl(ClientConfiguration bkClientConfiguration, ManagedLedgerFactoryConfig config)
        throws Exception {
    this(ZooKeeperClient.newBuilder()
            .connectString(bkClientConfiguration.getZkServers())
            .sessionTimeoutMs(bkClientConfiguration.getZkTimeout())
            .build(), bkClientConfiguration, config);
}
 
Example #9
Source File: ManagedLedgerFactoryImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private ManagedLedgerFactoryImpl(ClientConfiguration clientConfiguration, String zkConnection, ManagedLedgerFactoryConfig config) throws Exception {
    this(new DefaultBkFactory(clientConfiguration),
        true,
        ZooKeeperClient.newBuilder()
            .connectString(zkConnection)
            .sessionTimeoutMs(clientConfiguration.getZkTimeout()).build(), config, NullStatsLogger.INSTANCE);
}
 
Example #10
Source File: ZooKeeperUtil.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void startServer(String path) throws Exception {
    LOG.debug("Running ZK server");
    // ServerStats.registerAsConcrete();
    ClientBase.setupTestEnv();
    ZkTmpDir = File.createTempFile("zookeeper", "test");
    ZkTmpDir.delete();
    ZkTmpDir.mkdir();

    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperServer.DEFAULT_TICK_TIME);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(zkaddr, 100);
    serverFactory.startup(zks);

    zooKeeperPort = serverFactory.getLocalPort();
    connectString = "localhost:" + zooKeeperPort;
    boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
    LOG.debug("Server up: " + b);

    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    zkc = ZooKeeperClient.newBuilder().connectString(getZooKeeperConnectString()).build();
    if (path != "") {
        zkc.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    // initialize the zk client with values
    zkc.create(path + "/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.create(path +"/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example #11
Source File: ZKTestEnv.java    From herddb with Apache License 2.0 4 votes vote down vote up
public void startBookie(boolean format) throws Exception {
        if (bookie != null) {
            throw new Exception("bookie already started");
        }
        ServerConfiguration conf = new ServerConfiguration();
        conf.setBookiePort(0);
        conf.setUseHostNameAsBookieID(true);

        Path targetDir = path.resolve("bookie_data");
        conf.setZkServers("localhost:1282");
        conf.setZkLedgersRootPath("/ledgers");
        conf.setLedgerDirNames(new String[]{targetDir.toAbsolutePath().toString()});
        conf.setJournalDirName(targetDir.toAbsolutePath().toString());
        conf.setFlushInterval(10000);
        conf.setGcWaitTime(5);
        conf.setJournalFlushWhenQueueEmpty(true);
//        conf.setJournalBufferedEntriesThreshold(1);
        conf.setAutoRecoveryDaemonEnabled(false);
        conf.setEnableLocalTransport(true);
        conf.setJournalSyncData(false);

        conf.setAllowLoopback(true);
        conf.setProperty("journalMaxGroupWaitMSec", 10); // default 200ms

        try (ZooKeeperClient zkc = ZooKeeperClient
                .newBuilder()
                .connectString("localhost:1282")
                .sessionTimeoutMs(10000)
                .build()) {

            boolean rootExists = zkc.exists(getPath(), false) != null;

            if (!rootExists) {
                zkc.create(getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }

        }

        if (format) {
            BookKeeperAdmin.initNewCluster(conf);
            BookKeeperAdmin.format(conf, false, true);
        }

        this.bookie = new BookieServer(conf);
        this.bookie.start();
    }