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();
}
}
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);
}
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
}
}
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();
}
}
}
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);
}
Aggregations