Search in sources :

Example 21 with NIOServerCnxnFactory

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

the class ZKPeer method killQuorumPeer.

public void killQuorumPeer() {
    try {
        if (_peer != null) {
            _log.info("Killing quorum peer #" + getPeerPortsInfo());
            Field cnxnFactoryField = QuorumPeer.class.getDeclaredField("cnxnFactory");
            cnxnFactoryField.setAccessible(true);
            NIOServerCnxnFactory cnxnFactory = (NIOServerCnxnFactory) cnxnFactoryField.get(_peer);
            cnxnFactory.shutdown();
            Field ssField = cnxnFactory.getClass().getDeclaredField("ss");
            ssField.setAccessible(true);
            ServerSocketChannel ss = (ServerSocketChannel) ssField.get(cnxnFactory);
            ss.close();
        }
        close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Field(java.lang.reflect.Field) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException)

Example 22 with NIOServerCnxnFactory

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

the class ZKPeer method setQuorumPeer.

public void setQuorumPeer(int peersCount, Map<Long, QuorumServer> peersView, FileTxnSnapLog fts) throws IOException {
    NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
    cnxnFactory.configure(new InetSocketAddress("127.0.0.1", _clientPort), _maxClientCnxns);
    _peer = QuorumPeer.testingQuorumPeer();
    _peer.setClientPortAddress(new InetSocketAddress("127.0.0.1", _clientPort));
    _peer.setTxnFactory(fts);
    _peer.setQuorumPeers(peersView);
    _peer.setElectionType(_electionAlg);
    _peer.setMyid(_id);
    _peer.setTickTime(_tickTime);
    _peer.setMinSessionTimeout(_minSessionTimeout);
    _peer.setMaxSessionTimeout(_maxSessionTimeout);
    _peer.setInitLimit(_initLimit);
    _peer.setSyncLimit(_syncLimit);
    _peer.setQuorumVerifier(new QuorumMaj(peersCount));
    _peer.setCnxnFactory(cnxnFactory);
    _peer.setZKDatabase(new ZKDatabase(_peer.getTxnFactory()));
    _peer.setLearnerType(LearnerType.PARTICIPANT);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) QuorumMaj(org.apache.zookeeper.server.quorum.flexible.QuorumMaj) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZKDatabase(org.apache.zookeeper.server.ZKDatabase)

Example 23 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory 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 24 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
 */
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testWatchesLeak(boolean sessionTimedout) 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(0, watchCount, "Session is not valid so there should be no watches");
        } else {
            // Session has been re-validated
            LOG.info("session is valid, watches = {}", watchCount);
            assertEquals(1, watchCount, "Session is valid so the watch should be there");
        }
    } 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) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with NIOServerCnxnFactory

use of org.apache.zookeeper.server.NIOServerCnxnFactory 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

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