Search in sources :

Example 91 with ZooKeeper

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

the class CompactorTool method main.

public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander(arguments);
    jcommander.setProgramName("PulsarTopicCompactor");
    // parse args by JCommander
    jcommander.parse(args);
    if (arguments.help) {
        jcommander.usage();
        System.exit(-1);
    }
    // init broker config
    ServiceConfiguration brokerConfig;
    if (isBlank(arguments.brokerConfigFile)) {
        jcommander.usage();
        throw new IllegalArgumentException("Need to specify a configuration file for broker");
    } else {
        brokerConfig = PulsarConfigurationLoader.create(arguments.brokerConfigFile, ServiceConfiguration.class);
    }
    String pulsarServiceUrl = PulsarService.brokerUrl(brokerConfig);
    ClientConfiguration clientConfig = new ClientConfiguration();
    if (isNotBlank(brokerConfig.getBrokerClientAuthenticationPlugin())) {
        clientConfig.setAuthentication(brokerConfig.getBrokerClientAuthenticationPlugin(), brokerConfig.getBrokerClientAuthenticationParameters());
    }
    clientConfig.setUseTls(brokerConfig.isTlsEnabled());
    clientConfig.setTlsAllowInsecureConnection(brokerConfig.isTlsAllowInsecureConnection());
    clientConfig.setTlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());
    OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
    ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);
    ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(), ZooKeeperClientFactory.SessionType.ReadWrite, (int) brokerConfig.getZooKeeperSessionTimeoutMillis()).get();
    BookKeeperClientFactory bkClientFactory = new BookKeeperClientFactoryImpl();
    BookKeeper bk = bkClientFactory.create(brokerConfig, zk);
    try (PulsarClient pulsar = PulsarClient.create(pulsarServiceUrl, clientConfig)) {
        Compactor compactor = new TwoPhaseCompactor(brokerConfig, pulsar, bk, scheduler);
        long ledgerId = compactor.compact(arguments.topic).get();
        log.info("Compaction of topic {} complete. Compacted to ledger {}", arguments.topic, ledgerId);
    } finally {
        bk.close();
        bkClientFactory.close();
        zk.close();
        scheduler.shutdownNow();
        executor.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BookKeeperClientFactoryImpl(org.apache.pulsar.broker.BookKeeperClientFactoryImpl) BookKeeper(org.apache.bookkeeper.client.BookKeeper) ZooKeeperClientFactory(org.apache.pulsar.zookeeper.ZooKeeperClientFactory) BookKeeperClientFactory(org.apache.pulsar.broker.BookKeeperClientFactory) ZooKeeper(org.apache.zookeeper.ZooKeeper) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) JCommander(com.beust.jcommander.JCommander) ZookeeperBkClientFactoryImpl(org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler)

Example 92 with ZooKeeper

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

the class PulsarClusterUtils method waitBrokerState.

private static boolean waitBrokerState(DockerClient docker, String containerId, int timeout, TimeUnit timeoutUnit, boolean upOrDown) {
    long timeoutMillis = timeoutUnit.toMillis(timeout);
    long pollMillis = 1000;
    String brokerId = DockerUtils.getContainerHostname(docker, containerId) + ":" + BROKER_PORT;
    Optional<String> containerCluster = DockerUtils.getContainerCluster(docker, containerId);
    if (!containerCluster.isPresent()) {
        LOG.error("Unable to determine cluster for container {}. Missing label?", containerId);
        return false;
    }
    ZooKeeper zk = null;
    try {
        zk = zookeeperClient(docker, containerCluster.get());
        String path = "/loadbalance/brokers/" + brokerId;
        while (timeoutMillis > 0) {
            if ((zk.exists(path, false) != null) == upOrDown) {
                return true;
            }
            Thread.sleep(pollMillis);
            timeoutMillis -= pollMillis;
        }
    } catch (Exception e) {
        LOG.error("Exception checking for broker state", e);
        return false;
    } finally {
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (Exception e) {
            LOG.error("Exception closing zookeeper client", e);
            return false;
        }
    }
    LOG.warn("Broker {} didn't go {} after {} seconds", containerId, upOrDown ? "up" : "down", timeoutUnit.toSeconds(timeout));
    return false;
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) IOException(java.io.IOException)

Example 93 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project ecf by eclipse.

the class WriteRoot method connect.

private void connect() {
    synchronized (connectionLock) {
        if (this.isConnected || watchManager.isDisposed()) {
            return;
        }
        try {
            if (writeKeeper != null) {
                writeKeeper.close();
                watchManager.removeZooKeeper(writeKeeper);
                writeKeeper = null;
            }
            writeKeeper = new ZooKeeper(this.ip, 3000, this);
        } catch (Exception e) {
            Logger.log(LogService.LOG_DEBUG, e.getMessage(), e);
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) KeeperException(org.apache.zookeeper.KeeperException)

Example 94 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project ecf by eclipse.

the class WriteRoot method initWriteKeeper.

private void initWriteKeeper() {
    try {
        if (watchManager.getConfig().isQuorum() || watchManager.getConfig().isStandAlone()) {
            // we write nodes locally but we should check for client port.
            int port = watchManager.getConfig().getClientPort();
            if (port != 0)
                // $NON-NLS-1$
                ip += ":" + port;
        } else if (watchManager.getConfig().isCentralized()) {
            // we write nodes to the machine with this specified IP address.
            ip = watchManager.getConfig().getServerIps();
        }
        try {
            writeKeeper = new ZooKeeper(this.ip, 3000, this);
        } catch (Exception e) {
            // FATAL
            Logger.log(LogService.LOG_ERROR, "Fatal error while initializing a zookeeper client to write to: " + ip, e);
            // Publisher.publish()
            throw new IllegalStateException(e);
        }
        while (!this.isConnected) {
            synchronized (connectionLock) {
                if (watchManager.isDisposed()) {
                    // no need for connecting, we're disposed.
                    try {
                        writeKeeper.close();
                    } catch (Throwable t) {
                    // ignore
                    }
                    break;
                }
                try {
                    Stat s = this.writeKeeper.exists(INode.ROOT, this);
                    this.isConnected = true;
                    if (s == null) {
                        writeKeeper.create(INode.ROOT, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    }
                } catch (KeeperException e) {
                    if (e.code().equals(KeeperException.Code.CONNECTIONLOSS)) {
                        isConnected = false;
                        PrettyPrinter.attemptingConnectionTo(this.ip);
                    } else
                        Logger.log(LogService.LOG_ERROR, "Error while trying to connect to " + this.ip, // $NON-NLS-1$
                        e);
                }
            }
        }
        synchronized (this) {
            this.notifyAll();
        }
    } catch (Exception e) {
        Logger.log(LogService.LOG_DEBUG, e.getMessage(), e);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 95 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project helix by apache.

the class ZkTestHelper method expireSession.

public static void expireSession(final ZkClient zkClient) throws Exception {
    final CountDownLatch waitNewSession = new CountDownLatch(1);
    IZkStateListener listener = new IZkStateListener() {

        @Override
        public void handleStateChanged(KeeperState state) throws Exception {
            LOG.info("IZkStateListener#handleStateChanged, state: " + state);
        }

        @Override
        public void handleNewSession() throws Exception {
            // make sure zkclient is connected again
            zkClient.waitUntilConnected();
            ZkConnection connection = ((ZkConnection) zkClient.getConnection());
            ZooKeeper curZookeeper = connection.getZookeeper();
            LOG.info("handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
            waitNewSession.countDown();
        }

        @Override
        public void handleSessionEstablishmentError(Throwable var1) throws Exception {
        }
    };
    zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper curZookeeper = connection.getZookeeper();
    String oldSessionId = Long.toHexString(curZookeeper.getSessionId());
    LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper);
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            LOG.info("Watcher#process, event: " + event);
        }
    };
    final ZooKeeper dupZookeeper = new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher, curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
    // wait until connected, then close
    while (dupZookeeper.getState() != States.CONNECTED) {
        Thread.sleep(10);
    }
    Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED, "Fail to connect to zk using current session info");
    dupZookeeper.close();
    // make sure session expiry really happens
    waitNewSession.await();
    zkClient.unsubscribeStateChanges(listener);
    connection = (ZkConnection) zkClient.getConnection();
    curZookeeper = connection.getZookeeper();
    String newSessionId = Long.toHexString(curZookeeper.getSessionId());
    LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper);
    Assert.assertNotSame(newSessionId, oldSessionId, "Fail to expire current session, zk: " + curZookeeper);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) IZkStateListener(org.I0Itec.zkclient.IZkStateListener) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) ZkConnection(org.I0Itec.zkclient.ZkConnection)

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