Search in sources :

Example 81 with ZooKeeper

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

the class LoadSimulationController method handleCopy.

// Handle the command line arguments associated with the copy command.
private void handleCopy(final ShellArguments arguments) throws Exception {
    final List<String> commandArguments = arguments.commandArguments;
    // Copy accepts 3 application arguments: Tenant name, source ZooKeeper and target ZooKeeper connect strings.
    if (checkAppArgs(commandArguments.size() - 1, 3)) {
        final String tenantName = commandArguments.get(1);
        final String sourceZKConnectString = commandArguments.get(2);
        final String targetZKConnectString = commandArguments.get(3);
        final ZooKeeper sourceZKClient = new ZooKeeper(sourceZKConnectString, 5000, null);
        final ZooKeeper targetZKClient = new ZooKeeper(targetZKConnectString, 5000, null);
        // Make a map for each thread to speed up the ZooKeeper writing process.
        final Map<String, ResourceQuota>[] threadLocalMaps = new Map[clients.length];
        for (int i = 0; i < clients.length; ++i) {
            threadLocalMaps[i] = new HashMap<>();
        }
        getResourceQuotas(QUOTA_ROOT, sourceZKClient, threadLocalMaps);
        final List<Future> futures = new ArrayList<>(clients.length);
        int i = 0;
        log.info("Copying...");
        for (final Map<String, ResourceQuota> bundleToQuota : threadLocalMaps) {
            final int j = i;
            futures.add(threadPool.submit(() -> {
                for (final Map.Entry<String, ResourceQuota> entry : bundleToQuota.entrySet()) {
                    final String bundle = entry.getKey();
                    final ResourceQuota quota = entry.getValue();
                    // Simulation will send messages in and out at about the same rate, so just make the rate the
                    // average of in and out.
                    final int tenantStart = QUOTA_ROOT.length() + 1;
                    final int clusterStart = bundle.indexOf('/', tenantStart) + 1;
                    final String sourceTenant = bundle.substring(tenantStart, clusterStart - 1);
                    final int namespaceStart = bundle.indexOf('/', clusterStart) + 1;
                    final String sourceCluster = bundle.substring(clusterStart, namespaceStart - 1);
                    final String namespace = bundle.substring(namespaceStart, bundle.lastIndexOf('/'));
                    final String keyRangeString = bundle.substring(bundle.lastIndexOf('/') + 1);
                    // To prevent duplicate node issues for same namespace names in different clusters/tenants.
                    final String manglePrefix = String.format("%s-%s-%s", sourceCluster, sourceTenant, keyRangeString);
                    final String mangledNamespace = String.format("%s-%s", manglePrefix, namespace);
                    final BundleData bundleData = initializeBundleData(quota, arguments);
                    final String oldAPITargetPath = String.format("/loadbalance/resource-quota/namespace/%s/%s/%s/0x00000000_0xffffffff", tenantName, cluster, mangledNamespace);
                    final String newAPITargetPath = String.format("/loadbalance/bundle-data/%s/%s/%s/0x00000000_0xffffffff", tenantName, cluster, mangledNamespace);
                    try {
                        ZkUtils.createFullPathOptimistic(targetZKClient, oldAPITargetPath, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(quota), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    } catch (KeeperException.NodeExistsException e) {
                    // Ignore already created nodes.
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    // Put the bundle data in the new ZooKeeper.
                    try {
                        ZkUtils.createFullPathOptimistic(targetZKClient, newAPITargetPath, bundleData.getJsonBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    } catch (KeeperException.NodeExistsException e) {
                    // Ignore already created nodes.
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        trade(arguments, makeTopic(tenantName, mangledNamespace, "t"), j);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }));
            ++i;
        }
        for (final Future future : futures) {
            future.get();
        }
        sourceZKClient.close();
        targetZKClient.close();
    }
}
Also used : ArrayList(java.util.ArrayList) BundleData(org.apache.pulsar.broker.BundleData) ParameterException(com.beust.jcommander.ParameterException) KeeperException(org.apache.zookeeper.KeeperException) ZooKeeper(org.apache.zookeeper.ZooKeeper) ResourceQuota(org.apache.pulsar.common.policies.data.ResourceQuota) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) Map(java.util.Map)

Example 82 with ZooKeeper

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

the class GlobalZooKeeperCache method start.

public void start() throws IOException {
    CompletableFuture<ZooKeeper> zkFuture = zlClientFactory.create(globalZkConnect, SessionType.AllowReadOnly, zkSessionTimeoutMillis);
    // Initial session creation with global ZK must work
    try {
        ZooKeeper newSession = zkFuture.get(10, TimeUnit.SECONDS);
        // Register self as a watcher to receive notification when session expires and trigger a new session to be
        // created
        newSession.register(this);
        zkSession.set(newSession);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.error("Failed to establish global zookeeper session: {}", e.getMessage(), e);
        throw new IOException(e);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 83 with ZooKeeper

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

the class LocalBookkeeperEnsemble method initializeZookeper.

private void initializeZookeper() throws IOException {
    LOG.info("Instantiate ZK Client");
    // initialize the zk client with values
    try {
        ZKConnectionWatcher zkConnectionWatcher = new ZKConnectionWatcher();
        zkc = new ZooKeeper(HOSTPORT, zkSessionTimeOut, zkConnectionWatcher);
        zkConnectionWatcher.waitForConnection();
        if (zkc.exists("/ledgers", false) == null) {
            zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        if (zkc.exists("/ledgers/available", false) == null) {
            zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        if (zkc.exists("/ledgers/available/readonly", false) == null) {
            zkc.create("/ledgers/available/readonly", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    // No need to create an entry for each requested bookie anymore as the
    // BookieServers will register themselves with ZooKeeper on startup.
    } catch (KeeperException e) {
        LOG.error("Exception while creating znodes", e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Interrupted while creating znodes", e);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) KeeperException(org.apache.zookeeper.KeeperException)

Example 84 with ZooKeeper

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

the class LocalZooKeeperConnectionService method start.

public void start(ShutdownService shutdownService) throws IOException {
    // Connect to local ZK
    CompletableFuture<ZooKeeper> zkFuture = zkClientFactory.create(zkConnect, SessionType.ReadWrite, (int) zkSessionTimeoutMillis);
    try {
        localZooKeeper = zkFuture.get(zkSessionTimeoutMillis, TimeUnit.MILLISECONDS);
        localZooKeeperSessionWatcher = new ZooKeeperSessionWatcher(localZooKeeper, zkSessionTimeoutMillis, shutdownService);
        localZooKeeperSessionWatcher.start();
        localZooKeeper.register(localZooKeeperSessionWatcher);
    } catch (Exception e) {
        throw new IOException("Failed to establish session with local ZK", e);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 85 with ZooKeeper

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

the class ZookeeperClientFactoryImplTest method testZKCreationRW.

@Test
void testZKCreationRW() throws Exception {
    ZooKeeperClientFactory zkf = new ZookeeperClientFactoryImpl();
    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);
    assertTrue(localZkc.getState().isConnected());
    assertNotEquals(localZkc.getState(), States.CONNECTEDREADONLY);
    localZkc.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZookeeperClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl) ZooKeeperClientFactory(org.apache.pulsar.zookeeper.ZooKeeperClientFactory) Test(org.testng.annotations.Test)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)605 Test (org.junit.jupiter.api.Test)190 KeeperException (org.apache.zookeeper.KeeperException)153 Test (org.junit.Test)100 Stat (org.apache.zookeeper.data.Stat)86 IOException (java.io.IOException)83 CountDownLatch (java.util.concurrent.CountDownLatch)80 WatchedEvent (org.apache.zookeeper.WatchedEvent)70 Watcher (org.apache.zookeeper.Watcher)59 ArrayList (java.util.ArrayList)47 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)41 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)38 Timeout (org.junit.jupiter.api.Timeout)32 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)31 File (java.io.File)28 HashMap (java.util.HashMap)26 CompletableFuture (java.util.concurrent.CompletableFuture)22 Test (org.testng.annotations.Test)21 AsyncCallback (org.apache.zookeeper.AsyncCallback)19 ACL (org.apache.zookeeper.data.ACL)19