Search in sources :

Example 31 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project pulsar by yahoo.

the class ZooKeeperUtil method startServer.

public void startServer() throws Exception {
    // create a ZooKeeper server(dataDir, dataLogDir, port)
    LOG.debug("Running ZK server");
    // ServerStats.registerAsConcrete();
    ClientBase.setupTestEnv();
    ZkTmpDir = File.createTempFile("zookeeper", "test");
    ZkTmpDir.delete();
    ZkTmpDir.mkdir();
    zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperServer.DEFAULT_TICK_TIME);
    serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(zkaddr, 100);
    serverFactory.startup(zks);
    boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(), ClientBase.CONNECTION_TIMEOUT);
    LOG.debug("Server up: " + b);
    // create a zookeeper client
    LOG.debug("Instantiate ZK Client");
    ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(10000);
    zkc = ZkUtils.createConnectedZookeeperClient(getZooKeeperConnectString(), w);
    // initialize the zk client with values
    zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
Also used : ZooKeeperWatcherBase(org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 32 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project pulsar by yahoo.

the class LocalBookkeeperEnsemble method runZookeeper.

/**
     * @param args
     */
private void runZookeeper(int maxCC) throws IOException {
    // create a ZooKeeper server(dataDir, dataLogDir, port)
    LOG.info("Starting ZK server");
    // ServerStats.registerAsConcrete();
    // ClientBase.setupTestEnv();
    File zkDataDir = isNotBlank(zkDataDirName) ? Files.createDirectories(Paths.get(zkDataDirName)).toFile() : Files.createTempDirectory("zktest").toFile();
    if (this.clearOldData) {
        cleanDirectory(zkDataDir);
    }
    try {
        zks = new ZooKeeperServer(zkDataDir, zkDataDir, ZooKeeperServer.DEFAULT_TICK_TIME);
        serverFactory = new NIOServerCnxnFactory();
        serverFactory.configure(new InetSocketAddress(ZooKeeperDefaultPort), maxCC);
        serverFactory.startup(zks);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        LOG.error("Exception while instantiating ZooKeeper", e);
    }
    boolean b = waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT);
    LOG.info("ZooKeeper server up: {}", b);
    LOG.debug("Local ZK started (port: {}, data_directory: {})", ZooKeeperDefaultPort, zkDataDir.getAbsolutePath());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 33 with NIOServerCnxnFactory

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

the class WatchLeakTest method testWatchesLeak.

/**
 * Check that if session has expired then no watch can be set
 */
@Test
public void testWatchesLeak() throws Exception {
    NIOServerCnxnFactory serverCnxnFactory = mock(NIOServerCnxnFactory.class);
    final SelectionKey sk = new FakeSK();
    MockSelectorThread selectorThread = mock(MockSelectorThread.class);
    when(selectorThread.addInterestOpsUpdateRequest(any(SelectionKey.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            SelectionKey sk = (SelectionKey) invocation.getArguments()[0];
            NIOServerCnxn nioSrvCnx = (NIOServerCnxn) sk.attachment();
            sk.interestOps(nioSrvCnx.getInterestOps());
            return true;
        }
    });
    ZKDatabase database = new ZKDatabase(null);
    database.setlastProcessedZxid(2L);
    QuorumPeer quorumPeer = mock(QuorumPeer.class);
    FileTxnSnapLog logfactory = mock(FileTxnSnapLog.class);
    // Directories are not used but we need it to avoid NPE
    when(logfactory.getDataDir()).thenReturn(new File(""));
    when(logfactory.getSnapDir()).thenReturn(new File(""));
    FollowerZooKeeperServer fzks = null;
    try {
        // Create a new follower
        fzks = new FollowerZooKeeperServer(logfactory, quorumPeer, database);
        fzks.startup();
        fzks.setServerCnxnFactory(serverCnxnFactory);
        quorumPeer.follower = new MyFollower(quorumPeer, fzks);
        LOG.info("Follower created");
        // Simulate a socket channel between a client and a follower
        final SocketChannel socketChannel = createClientSocketChannel();
        // Create the NIOServerCnxn that will handle the client requests
        final MockNIOServerCnxn nioCnxn = new MockNIOServerCnxn(fzks, socketChannel, sk, serverCnxnFactory, selectorThread);
        sk.attach(nioCnxn);
        // Send the connection request as a client do
        nioCnxn.doIO(sk);
        LOG.info("Client connection sent");
        // Send the valid or invalid session packet to the follower
        QuorumPacket qp = createValidateSessionPacketResponse(!sessionTimedout);
        quorumPeer.follower.processPacket(qp);
        LOG.info("Session validation sent");
        // OK, now the follower knows that the session is valid or invalid, let's try
        // to send the watches
        nioCnxn.doIO(sk);
        // wait for the the request processor to do his job
        Thread.sleep(1000L);
        LOG.info("Watches processed");
        // If session has not been validated, there must be NO watches
        int watchCount = database.getDataTree().getWatchCount();
        if (sessionTimedout) {
            // Session has not been re-validated !
            LOG.info("session is not valid, watches = {}", watchCount);
            assertEquals("Session is not valid so there should be no watches", 0, watchCount);
        } else {
            // Session has been re-validated
            LOG.info("session is valid, watches = {}", watchCount);
            assertEquals("Session is valid so the watch should be there", 1, watchCount);
        }
    } finally {
        if (fzks != null) {
            fzks.shutdown();
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) MockNIOServerCnxn(org.apache.zookeeper.server.MockNIOServerCnxn) NIOServerCnxn(org.apache.zookeeper.server.NIOServerCnxn) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) MockNIOServerCnxn(org.apache.zookeeper.server.MockNIOServerCnxn) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockSelectorThread(org.apache.zookeeper.server.MockSelectorThread) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) Test(org.junit.Test)

Example 34 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory in project camel by apache.

the class EmbeddedZookeeper method before.

@Override
public void before() throws IOException {
    this.snapshotDir = constructTempDir(perTest("zk-snapshot"));
    this.logDir = constructTempDir(perTest("zk-log"));
    try {
        zooKeeperServer = new ZooKeeperServer(snapshotDir, logDir, tickTime);
        cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(new InetSocketAddress("localhost", port), 1024);
        cnxnFactory.startup(zooKeeperServer);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 35 with NIOServerCnxnFactory

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

the class MiniZooKeeperCluster method killOneBackupZooKeeperServer.

/**
 * Kill one back up ZK servers
 *
 * @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);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException)

Aggregations

NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)55 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)24 IOException (java.io.IOException)22 InetSocketAddress (java.net.InetSocketAddress)19 File (java.io.File)16 Test (org.junit.Test)13 CuratorFramework (org.apache.curator.framework.CuratorFramework)12 RetryNTimes (org.apache.curator.retry.RetryNTimes)12 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)10 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)6 ZooKeeperGroup (org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup)6 ServerConfig (org.apache.zookeeper.server.ServerConfig)5 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)3 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)3 Field (java.lang.reflect.Field)2 SelectionKey (java.nio.channels.SelectionKey)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 SocketChannel (java.nio.channels.SocketChannel)2 ArrayList (java.util.ArrayList)2