Search in sources :

Example 41 with ZooKeeperServer

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

the class CommandsTest method testConsCommandSecureOnly.

@Test
public void testConsCommandSecureOnly() {
    // Arrange
    Commands.ConsCommand cmd = new Commands.ConsCommand();
    ZooKeeperServer zkServer = mock(ZooKeeperServer.class);
    ServerCnxnFactory cnxnFactory = mock(ServerCnxnFactory.class);
    when(zkServer.getSecureServerCnxnFactory()).thenReturn(cnxnFactory);
    // Act
    CommandResponse response = cmd.run(zkServer, null);
    // Assert
    assertThat(response.toMap().containsKey("connections"), is(true));
    assertThat(response.toMap().containsKey("secure_connections"), is(true));
}
Also used : ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 42 with ZooKeeperServer

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

the class MiniZooKeeperCluster method startup.

/**
 * @param baseDir
 * @param numZooKeeperServers
 * @return ClientPort server bound to, -1 if there was a
 *         binding problem and we couldn't pick another port.
 * @throws IOException
 * @throws InterruptedException
 */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0) {
        return -1;
    }
    setupTestEnv(sslEnabled);
    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));
        ServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = createServerCnxnFactory(currentClientPort);
            } 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
        standaloneServerFactory.startup(server);
        // Runs a 'stat' against the servers.
        if (!sslEnabled && !waitForServerUp(currentClientPort, connectionTimeout)) {
            throw new IOException("Waiting for startup of standalone server");
        }
        // 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 successful 'stat' " + "on client port=" + clientPort);
    return clientPort;
}
Also used : ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) BindException(java.net.BindException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 43 with ZooKeeperServer

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

the class MiniZooKeeperCluster method shutdown.

/**
 * @throws IOException
 */
public void shutdown() throws IOException {
    // shut down all the zk servers
    for (int i = 0; i < standaloneServerFactoryList.size(); i++) {
        ServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(i);
        int clientPort = clientPortList.get(i);
        standaloneServerFactory.shutdown();
        if (!waitForServerDown(clientPort, connectionTimeout)) {
            throw new IOException("Waiting for shutdown of standalone server");
        }
    }
    standaloneServerFactoryList.clear();
    for (ZooKeeperServer zkServer : zooKeeperServers) {
        // explicitly close ZKDatabase since ZookeeperServer does not close them
        zkServer.getZKDatabase().close();
        zkServer.shutdown(true);
    }
    zooKeeperServers.clear();
    // clear everything
    if (started) {
        started = false;
        activeZKServerIndex = 0;
        clientPortList.clear();
        LOG.info("Shutdown MiniZK cluster with all ZK servers");
    }
}
Also used : ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 44 with ZooKeeperServer

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

the class Zookeeper method mkInprocessZookeeper.

public static Factory mkInprocessZookeeper(String localDir, int port) throws IOException, InterruptedException {
    LOG.info("Starting in-process zookeeper at port " + port + " and dir " + localDir);
    File localFile = new File(localDir);
    ZooKeeperServer zk = new ZooKeeperServer(localFile, localFile, 2000);
    Factory factory = new Factory(new InetSocketAddress(port), 0);
    factory.startup(zk);
    return factory;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) LoggerFactory(org.slf4j.LoggerFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 45 with ZooKeeperServer

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

the class KeptTestBase method init.

@BeforeClass
public static void init() throws IOException, InterruptedException {
    final File dir = new File(System.getProperty("java.io.tmpdir"), "zookeeper").getAbsoluteFile();
    final ZooKeeperServer server = new ZooKeeperServer(dir, dir, 2000);
    KeptTestBase.nioServerCnxnFactory = new NIOServerCnxnFactory();
    KeptTestBase.nioServerCnxnFactory.configure(new InetSocketAddress(KeptTestBase.CLIENT_PORT), 5000);
    KeptTestBase.nioServerCnxnFactory.startup(server);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) BeforeClass(org.junit.BeforeClass)

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