Example usage for org.apache.zookeeper.server NIOServerCnxnFactory shutdown

List of usage examples for org.apache.zookeeper.server NIOServerCnxnFactory shutdown

Introduction

In this page you can find the example usage for org.apache.zookeeper.server NIOServerCnxnFactory shutdown.

Prototype

public void shutdown() 

Source Link

Usage

From source file:io.fabric8.core.zookeeper.FabricZooKeeperServer.java

License:Apache License

private Destroyable activateInternal(Map<String, ?> configuration) throws Exception {
    LOGGER.info("Creating zookeeper server with: {}", configuration);

    Properties props = new Properties();
    for (Map.Entry<String, ?> entry : configuration.entrySet()) {
        props.put(entry.getKey(), entry.getValue());
    }//from   w w  w.  ja  v  a  2  s .c o m

    //Check required directories exist or create them.
    if (!dataDir.exists() && !dataDir.mkdirs()) {
        throw new IOException("Failed to create ZooKeeper dataDir at: " + dataDir.getAbsolutePath());
    }

    if (!dataLogDir.exists() && !dataLogDir.mkdirs()) {
        throw new IOException("Failed to create ZooKeeper dataLogDir at: " + dataLogDir.getAbsolutePath());
    }

    // Create myid file
    String serverId = (String) props.get(SERVER_ID);
    if (serverId != null) {
        props.remove(SERVER_ID);
        File myId = new File(dataDir, MY_ID);
        if (myId.exists() && !myId.delete()) {
            throw new IOException("Failed to delete " + myId);
        }
        if (myId.getParentFile() == null
                || (!myId.getParentFile().exists() && !myId.getParentFile().mkdirs())) {
            throw new IOException("Failed to create " + myId.getParent());
        }
        FileOutputStream fos = new FileOutputStream(myId);
        try {
            fos.write((serverId + "\n").getBytes());
        } finally {
            fos.close();
        }
    }

    QuorumPeerConfig peerConfig = getPeerConfig(props);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        // Register stats provider
        ClusteredServer server = new ClusteredServer(quorumPeer);
        return server;
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        cnxnFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        SimpleServer server = new SimpleServer(zkServer, cnxnFactory);

        return server;
    }
}

From source file:io.fabric8.groups.GroupTest.java

License:Apache License

@Test
public void testJoinAfterConnect() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(3000);

    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port)
            .retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();/*from   www  .  j  av a2s .co m*/

    final Group<NodeState> group = new ZooKeeperGroup<NodeState>(curator,
            "/singletons/test" + System.currentTimeMillis(), NodeState.class);
    group.add(listener);
    group.start();

    assertFalse(group.isConnected());
    assertFalse(group.isMaster());

    GroupCondition groupCondition = new GroupCondition();
    group.add(groupCondition);

    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);

    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();

    assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
    assertFalse(group.isMaster());

    group.update(new NodeState("foo"));
    assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));

    group.close();
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}

From source file:io.fabric8.groups.GroupTest.java

License:Apache License

@Test
public void testJoinBeforeConnect() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(3010);

    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port)
            .retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();//from w  w w. ja v  a  2 s.c  o  m

    Group<NodeState> group = new ZooKeeperGroup<NodeState>(curator,
            "/singletons/test" + System.currentTimeMillis(), NodeState.class);
    group.add(listener);
    group.start();

    GroupCondition groupCondition = new GroupCondition();
    group.add(groupCondition);

    assertFalse(group.isConnected());
    assertFalse(group.isMaster());
    group.update(new NodeState("foo"));

    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);

    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();

    assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
    assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));

    group.close();
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}

From source file:io.fabric8.groups.GroupTest.java

License:Apache License

@Test
public void testRejoinAfterDisconnect() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(3020);

    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port)
            .retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();//from w  ww . j  a va  2s .c om

    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    Group<NodeState> group = new ZooKeeperGroup<NodeState>(curator,
            "/singletons/test" + System.currentTimeMillis(), NodeState.class);
    group.add(listener);
    group.update(new NodeState("foo"));
    group.start();

    GroupCondition groupCondition = new GroupCondition();
    group.add(groupCondition);

    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
    assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));

    cnxnFactory.shutdown();
    cnxnFactory.join();

    groupCondition.waitForDisconnected(5, TimeUnit.SECONDS);
    group.remove(groupCondition);

    assertFalse(group.isConnected());
    assertFalse(group.isMaster());

    groupCondition = new GroupCondition();
    group.add(groupCondition);

    cnxnFactory = startZooKeeper(port);

    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
    assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));

    group.close();
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}

From source file:io.fabric8.groups.GroupTest.java

License:Apache License

@Test
public void testGroupClose() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(3030);

    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);

    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port)
            .retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();/*from  ww w  .  j a  v  a  2 s  . co m*/
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    String groupNode = "/singletons/test" + System.currentTimeMillis();
    curator.create().creatingParentsIfNeeded().forPath(groupNode);

    for (int i = 0; i < 10; i++) {
        Group<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
        group.add(listener);
        group.update(new NodeState("foo"));
        group.start();
        group.close();
        List<String> entries = curator.getChildren().forPath(groupNode);
        assertTrue(entries.isEmpty());
    }

    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}

From source file:io.fabric8.zookeeper.bootstrap.ZooKeeperServerFactory.java

License:Apache License

public ZooKeeperServerFactory(QuorumPeerConfig peerConfig, String serverId)
        throws IOException, InterruptedException {
    this.peerConfig = peerConfig;
    this.serverId = serverId;

    LOGGER.info("Creating zookeeper server with: {}", peerConfig);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(/*from  ww w .j  a  va2s  . c  o  m*/
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        updateZooKeeperURL(cnxnFactory.getLocalAddress(), cnxnFactory.getLocalPort());

        // Register stats provider
        this.clusteredServer = new ClusteredServer(quorumPeer);
        /*
                    registration = context.registerService(QuorumStats.Provider.class, server, null);
                
        */
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        InetSocketAddress clientPortAddress = serverConfig.getClientPortAddress();
        cnxnFactory.configure(clientPortAddress, serverConfig.getMaxClientCnxns());
        updateZooKeeperURL(cnxnFactory.getLocalAddress(), cnxnFactory.getLocalPort());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        this.simplerServer = new SimpleServer(zkServer, cnxnFactory);
        /*
                    registration = context.registerService(ServerStats.Provider.class, server, null);
        */
    }
}

From source file:org.apache.drill.cv.exec.coord.zk.MiniZooKeeperCluster.java

License:Apache License

/**
 * @throws IOException/* w  ww.  jav a 2  s. c om*/
 */
public void shutdown() throws IOException {
    if (!started) {
        return;
    }
    // shut down all the zk servers
    for (int i = 0; i < standaloneServerFactoryList.size(); i++) {
        NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(i);
        int clientPort = clientPortList.get(i);

        standaloneServerFactory.shutdown();
        if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
            throw new IOException("Waiting for shutdown of standalone server");
        }
    }

    // clear everything
    started = false;
    activeZKServerIndex = 0;
    standaloneServerFactoryList.clear();
    clientPortList.clear();
    zooKeeperServers.clear();

    LOG.info("Shutdown MiniZK cluster with all ZK servers");
}

From source file:org.apache.drill.cv.exec.coord.zk.MiniZooKeeperCluster.java

License:Apache License

/**
 * @return clientPort return clientPort if there is another ZK backup can run
 *         when killing the current active; return -1, if there is no backups.
 * @throws IOException/*  w w  w . j  a  va2s  . co m*/
 * @throws InterruptedException
 */
public int killCurrentActiveZooKeeperServer() throws IOException, InterruptedException {
    if (!started || activeZKServerIndex < 0) {
        return -1;
    }

    // Shutdown the current active one
    NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(activeZKServerIndex);
    int clientPort = clientPortList.get(activeZKServerIndex);

    standaloneServerFactory.shutdown();
    if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
        throw new IOException("Waiting for shutdown of standalone server");
    }

    // remove the current active zk server
    standaloneServerFactoryList.remove(activeZKServerIndex);
    clientPortList.remove(activeZKServerIndex);
    zooKeeperServers.remove(activeZKServerIndex);
    LOG.info("Kill the current active ZK servers in the cluster " + "on client port: " + clientPort);

    if (standaloneServerFactoryList.size() == 0) {
        // there is no backup servers;
        return -1;
    }
    clientPort = clientPortList.get(activeZKServerIndex);
    LOG.info("Activate a backup zk server in the cluster " + "on client port: " + clientPort);
    // return the next back zk server's port
    return clientPort;
}

From source file:org.apache.drill.cv.exec.coord.zk.MiniZooKeeperCluster.java

License:Apache License

/**
 * Kill one back up ZK servers//from  w w  w .ja v a2s .c o  m
 *
 * @throws IOException
 * @throws InterruptedException
 */
public void killOneBackupZooKeeperServer() throws IOException, InterruptedException {
    if (!started || activeZKServerIndex < 0 || standaloneServerFactoryList.size() <= 1) {
        return;
    }

    int backupZKServerIndex = activeZKServerIndex + 1;
    // Shutdown the current active one
    NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(backupZKServerIndex);
    int clientPort = clientPortList.get(backupZKServerIndex);

    standaloneServerFactory.shutdown();
    if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
        throw new IOException("Waiting for shutdown of standalone server");
    }

    // remove this backup zk server
    standaloneServerFactoryList.remove(backupZKServerIndex);
    clientPortList.remove(backupZKServerIndex);
    zooKeeperServers.remove(backupZKServerIndex);
    LOG.info("Kill one backup ZK servers in the cluster " + "on client port: " + clientPort);
}

From source file:org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster.java

License:Apache License

/**
 * @throws IOException// ww  w  .ja v  a  2 s .c om
 */
public void shutdown() throws IOException {
    if (!started) {
        return;
    }

    // shut down all the zk servers
    for (int i = 0; i < standaloneServerFactoryList.size(); i++) {
        NIOServerCnxnFactory standaloneServerFactory = standaloneServerFactoryList.get(i);
        int clientPort = clientPortList.get(i);

        standaloneServerFactory.shutdown();
        if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
            throw new IOException("Waiting for shutdown of standalone server");
        }
    }
    for (ZooKeeperServer zkServer : zooKeeperServers) {
        //explicitly close ZKDatabase since ZookeeperServer does not close them
        zkServer.getZKDatabase().close();
    }

    // clear everything
    started = false;
    activeZKServerIndex = 0;
    standaloneServerFactoryList.clear();
    clientPortList.clear();
    zooKeeperServers.clear();

    LOG.info("Shutdown MiniZK cluster with all ZK servers");
}