Search in sources :

Example 21 with ZooKeeperServer

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

the class MiniZooKeeperCluster method startup.

/**
 * @param baseDir             the base directory to use
 * @param numZooKeeperServers the number of ZooKeeper servers
 * @return ClientPort server bound to, -1 if there was a binding problem and we couldn't pick
 *   another port.
 * @throws IOException          if an operation fails during the startup
 * @throws InterruptedException if the startup fails
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }
    setupTestEnv();
    shutdown();
    // the seed port
    int tentativePort = -1;
    int currentClientPort;
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
        createDir(dir);
        int tickTimeToUse;
        if (this.tickTime > 0) {
            tickTimeToUse = this.tickTime;
        } else {
            tickTimeToUse = TICK_TIME;
        }
        // Set up client port - if we have already had a list of valid ports, use it.
        if (hasValidClientPortInList(i)) {
            currentClientPort = clientPortList.get(i);
        } else {
            // update the seed
            tentativePort = selectClientPort(tentativePort);
            currentClientPort = tentativePort;
        }
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
        // Setting {min,max}SessionTimeout defaults to be the same as in Zookeeper
        server.setMinSessionTimeout(configuration.getInt("hbase.zookeeper.property.minSessionTimeout", -1));
        server.setMaxSessionTimeout(configuration.getInt("hbase.zookeeper.property.maxSessionTimeout", -1));
        NIOServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = new NIOServerCnxnFactory();
                standaloneServerFactory.configure(new InetSocketAddress(LOOPBACK_HOST, currentClientPort), configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, HConstants.DEFAULT_ZOOKEEPER_MAX_CLIENT_CNXNS));
            } catch (BindException e) {
                LOG.debug("Failed binding ZK Server to client port: " + currentClientPort, e);
                // We're told to use some port but it's occupied, fail
                if (hasValidClientPortInList(i)) {
                    return -1;
                }
                // This port is already in use, try to use another.
                tentativePort = selectClientPort(tentativePort);
                currentClientPort = tentativePort;
                continue;
            }
            break;
        }
        // Start up this ZK server. Dump its stats.
        standaloneServerFactory.startup(server);
        LOG.info("Started connectionTimeout={}, dir={}, {}", connectionTimeout, dir, getServerConfigurationOnOneLine(server));
        // Runs a 'stat' against the servers.
        if (!waitForServerUp(currentClientPort, connectionTimeout)) {
            Threads.printThreadInfo(System.out, "Why is zk standalone server not coming up?");
            throw new IOException("Waiting for startup of standalone server; " + "server isRunning=" + server.isRunning());
        }
        // We have selected a port as a client port.  Update clientPortList if necessary.
        if (clientPortList.size() <= i) {
            // it is not in the list, add the port
            clientPortList.add(currentClientPort);
        } else if (clientPortList.get(i) <= 0) {
            // the list has invalid port, update with valid port
            clientPortList.remove(i);
            clientPortList.add(i, currentClientPort);
        }
        standaloneServerFactoryList.add(standaloneServerFactory);
        zooKeeperServers.add(server);
    }
    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    int clientPort = clientPortList.get(activeZKServerIndex);
    LOG.info("Started MiniZooKeeperCluster and ran 'stat' on client port={}", clientPort);
    return clientPort;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 22 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project rest.li by linkedin.

the class ZKPeer method killPeerZkServer.

public void killPeerZkServer() {
    ZooKeeperServer zserver = _peer.getActiveServer();
    try {
        _log.info("Killing quorum zk server. Peer #" + getPeerPortsInfo());
        Field cnxnFactoryField = ZooKeeperServer.class.getDeclaredField("serverCnxnFactory");
        cnxnFactoryField.setAccessible(true);
        NIOServerCnxnFactory cnxnFactory = (NIOServerCnxnFactory) cnxnFactoryField.get(zserver);
        cnxnFactory.shutdown();
        Field ssField = cnxnFactory.getClass().getDeclaredField("ss");
        ssField.setAccessible(true);
        ServerSocketChannel ss = (ServerSocketChannel) ssField.get(cnxnFactory);
        ss.close();
        close();
    } catch (Exception e) {
    // do nothing - this method is only for testing
    }
}
Also used : Field(java.lang.reflect.Field) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException)

Example 23 with ZooKeeperServer

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

the class KafkaNotification method startZk.

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

Example 24 with ZooKeeperServer

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

the class ThrottledOpObserverTest method testThrottledOpObserver.

@Test
public void testThrottledOpObserver() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        zk = createClient("localhost:" + getFirstObserverClientPort());
        ZooKeeperServer zs = getFirstObserver().getActiveServer();
        ThrottledOpHelper test = new ThrottledOpHelper();
        test.testThrottledOp(zk, zs);
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 25 with ZooKeeperServer

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

the class ThrottledOpStandaloneTest method testThrottledOp.

@Test
public void testThrottledOp() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        zk = createClient(hostPort);
        ZooKeeperServer zs = serverFactory.getZooKeeperServer();
        ThrottledOpHelper test = new ThrottledOpHelper();
        test.testThrottledOp(zk, zs);
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

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