Search in sources :

Example 16 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project drill by axbaretto.

the class MiniZooKeeperCluster method startup.

/**
 * @param baseDir
 * @param numZooKeeperServers
 * @return ClientPort server bound to.
 * @throws IOException
 * @throws InterruptedException
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }
    setupTestEnv();
    shutdown();
    int tentativePort = selectClientPort();
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
        recreateDir(dir);
        int tickTimeToUse;
        if (this.tickTime > 0) {
            tickTimeToUse = this.tickTime;
        } else {
            tickTimeToUse = TICK_TIME;
        }
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
        NIOServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = new NIOServerCnxnFactory();
                standaloneServerFactory.configure(new InetSocketAddress(tentativePort), 1000);
            } catch (BindException e) {
                LOG.debug("Failed binding ZK Server to client port: " + tentativePort);
                // This port is already in use, try to use another.
                tentativePort++;
                continue;
            }
            try {
                standaloneServerFactory.startup(server);
            } catch (IOException e) {
                LOG.error("Zookeeper startup error", e);
                tentativePort++;
                continue;
            }
            if (!waitForServerUp(server, CONNECTION_TIMEOUT)) {
                server.shutdown();
                server = new ZooKeeperServer(dir, dir, tickTimeToUse);
                tentativePort++;
                continue;
            }
            break;
        }
        // We have selected this port as a client port.
        clientPortList.add(tentativePort);
        standaloneServerFactoryList.add(standaloneServerFactory);
        zooKeeperServers.add(server);
    }
    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    clientPort = clientPortList.get(activeZKServerIndex);
    LOG.info("Started MiniZK Cluster and connect 1 ZK server " + "on client port: " + clientPort);
    return clientPort;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 17 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project atlas by apache.

the class EmbeddedKafkaServer method startZk.

private String startZk() throws IOException, InterruptedException, URISyntaxException {
    String zkValue = properties.getProperty("zookeeper.connect");
    LOG.info("Starting zookeeper at {}", zkValue);
    URL zkAddress = getURL(zkValue);
    File snapshotDir = constructDir("zk/txn");
    File logDir = constructDir("zk/snap");
    factory = NIOServerCnxnFactory.createFactory(new InetSocketAddress(zkAddress.getHost(), zkAddress.getPort()), 1024);
    factory.startup(new ZooKeeperServer(snapshotDir, logDir, 500));
    String ret = factory.getLocalAddress().getAddress().toString();
    LOG.info("Embedded zookeeper for Kafka started at {}", ret);
    return ret;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) URL(java.net.URL) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 18 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project drill by apache.

the class MiniZooKeeperCluster method startup.

/**
 * @param baseDir
 * @param numZooKeeperServers
 * @return ClientPort server bound to.
 * @throws IOException
 * @throws InterruptedException
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }
    setupTestEnv();
    shutdown();
    int tentativePort = selectClientPort();
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
        recreateDir(dir);
        int tickTimeToUse;
        if (this.tickTime > 0) {
            tickTimeToUse = this.tickTime;
        } else {
            tickTimeToUse = TICK_TIME;
        }
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
        NIOServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = new NIOServerCnxnFactory();
                standaloneServerFactory.configure(new InetSocketAddress(tentativePort), 1000);
            } catch (BindException e) {
                logger.debug("Failed binding ZK Server to client port: {}", tentativePort);
                // This port is already in use, try to use another.
                tentativePort++;
                continue;
            }
            try {
                standaloneServerFactory.startup(server);
            } catch (IOException e) {
                logger.error("Zookeeper startup error", e);
                tentativePort++;
                continue;
            }
            if (!waitForServerUp(server, CONNECTION_TIMEOUT)) {
                server.shutdown();
                server = new ZooKeeperServer(dir, dir, tickTimeToUse);
                tentativePort++;
                continue;
            }
            break;
        }
        // We have selected this port as a client port.
        clientPortList.add(tentativePort);
        standaloneServerFactoryList.add(standaloneServerFactory);
        zooKeeperServers.add(server);
    }
    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    clientPort = clientPortList.get(activeZKServerIndex);
    logger.info("Started MiniZK Cluster and connect 1 ZK server " + "on client port: {}", clientPort);
    return clientPort;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 19 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project samza by apache.

the class TestZkStreamProcessorBase method expireSession.

// session expiration simulation
// have to use the reflection to get the session id
protected void expireSession(ZkClient zkClient) {
    ZkConnection zkConnection = null;
    try {
        Field privateField = ZkClient.class.getDeclaredField("_connection");
        privateField.setAccessible(true);
        zkConnection = (ZkConnection) privateField.get(zkClient);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assert.fail(e.toString());
    }
    ZooKeeper zookeeper = zkConnection.getZookeeper();
    long sessionId = zookeeper.getSessionId();
    LOG.info("Closing/expiring session:" + sessionId);
    ZooKeeperServer zkServer = zookeeper().zookeeper();
    zkServer.closeSession(sessionId);
}
Also used : Field(java.lang.reflect.Field) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZkConnection(org.I0Itec.zkclient.ZkConnection) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 20 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project pinpoint by naver.

the class ZookeeperUnitServer method startup.

public void startup() {
    final File snapshotDir;
    final File logDir;
    try {
        snapshotDir = Files.createTempDirectory("zookeeper-snapshot").toFile();
        logDir = Files.createTempDirectory("zookeeper-logs").toFile();
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to start Kafka", ioe);
    }
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            FileUtils.deleteDirectory(snapshotDir);
            FileUtils.deleteDirectory(logDir);
        } catch (IOException ioe) {
        }
    }));
    try {
        int tickTime = 500;
        ZooKeeperServer zkServer = new ZooKeeperServer(snapshotDir, logDir, tickTime);
        factory = NIOServerCnxnFactory.createFactory();
        factory.configure(new InetSocketAddress("localhost", port), maxConnections);
        factory.startup(zkServer);
    } catch (InterruptedException var5) {
        Thread.currentThread().interrupt();
    } catch (IOException var6) {
        throw new RuntimeException("Unable to start ZooKeeper", var6);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Aggregations

ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)96 File (java.io.File)39 Test (org.junit.jupiter.api.Test)33 ZooKeeper (org.apache.zookeeper.ZooKeeper)31 InetSocketAddress (java.net.InetSocketAddress)28 IOException (java.io.IOException)27 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)26 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)25 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)10 Stat (org.apache.zookeeper.data.Stat)9 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)8 ArrayList (java.util.ArrayList)6 ServerConfig (org.apache.zookeeper.server.ServerConfig)6 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)4 KeeperException (org.apache.zookeeper.KeeperException)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)3 ACL (org.apache.zookeeper.data.ACL)3 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)3