use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class QuorumUtil method shutdown.
public void shutdown(int id) {
QuorumPeer qp = getPeer(id).peer;
try {
LOG.info("Shutting down quorum peer " + qp.getName());
qp.shutdown();
Election e = qp.getElectionAlg();
if (e != null) {
LOG.info("Shutting down leader election " + qp.getName());
e.shutdown();
} else {
LOG.info("No election available to shutdown " + qp.getName());
}
LOG.info("Waiting for " + qp.getName() + " to exit thread");
qp.join(30000);
if (qp.isAlive()) {
Assert.fail("QP failed to shutdown in 30 seconds: " + qp.getName());
}
} catch (InterruptedException e) {
LOG.debug("QP interrupted: " + qp.getName(), e);
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class QuorumUtil method startThenShutdown.
public void startThenShutdown(int id) throws IOException {
PeerStruct ps = getPeer(id);
LOG.info("Creating QuorumPeer " + ps.id + "; public port " + ps.clientPort);
ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, ps.id, tickTime, initLimit, syncLimit);
if (localSessionEnabled) {
ps.peer.enableLocalSessions(true);
}
Assert.assertEquals(ps.clientPort, ps.peer.getClientPort());
ps.peer.start();
Assert.assertTrue("Waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + getPeer(id).clientPort, ClientBase.CONNECTION_TIMEOUT));
shutdown(id);
}
use of org.apache.zookeeper.server.quorum.QuorumPeer 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 = new QuorumPeer();
_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.quorum.QuorumPeer in project zookeeper by apache.
the class FLEBackwardElectionRoundTest method testBackwardElectionRound.
/**
* This test is checking the following case. A server S is
* currently LOOKING and it receives notifications from
* a quorum indicating they are following S. The election
* round E of S is higher than the election round E' in the
* notification messages, so S becomes the leader and sets
* its epoch back to E'. In the meanwhile, one or more
* followers turn to LOOKING and elect S in election round E.
* Having leader and followers with different election rounds
* might prevent other servers from electing a leader because
* they can't get a consistent set of notifications from a
* quorum.
*
* {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1514}
*
* @throws Exception
*/
@Test
public void testBackwardElectionRound() throws Exception {
LOG.info("TestLE: {}, {}", getTestName(), count);
for (int i = 0; i < count; i++) {
int clientport = PortAssignment.unique();
peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(clientport), new InetSocketAddress(PortAssignment.unique())));
tmpdir[i] = ClientBase.createTmpDir();
port[i] = clientport;
}
ByteBuffer initialMsg0 = getMsg();
ByteBuffer initialMsg1 = getMsg();
/*
* Start server 0
*/
QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2);
peer.startLeaderElection();
FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 0);
thread.start();
/*
* Start mock server 1
*/
QuorumPeer mockPeer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2);
cnxManagers[0] = mockPeer.createCnxnManager();
cnxManagers[0].listener.start();
cnxManagers[0].toSend(0l, initialMsg0);
/*
* Start mock server 2
*/
mockPeer = new QuorumPeer(peers, tmpdir[2], tmpdir[2], port[2], 3, 2, 1000, 2, 2);
cnxManagers[1] = mockPeer.createCnxnManager();
cnxManagers[1].listener.start();
cnxManagers[1].toSend(0l, initialMsg1);
/*
* Run another instance of leader election.
*/
thread.join(5000);
thread = new FLETestUtils.LEThread(peer, 0);
thread.start();
/*
* Send the same messages, this time should not make 0 the leader.
*/
cnxManagers[0].toSend(0l, initialMsg0);
cnxManagers[1].toSend(0l, initialMsg1);
thread.join(5000);
if (!thread.isAlive()) {
Assert.fail("Should not have joined");
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class CnxManagerTest method testSocketTimeout.
/*
* Test if a receiveConnection is able to timeout on socket errors
*/
@Test
public void testSocketTimeout() throws Exception {
QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 2000, 2, 2);
QuorumCnxManager cnxManager = peer.createCnxnManager();
QuorumCnxManager.Listener listener = cnxManager.listener;
if (listener != null) {
listener.start();
} else {
LOG.error("Null listener when initializing cnx manager");
}
int port = peers.get(peer.getId()).electionAddr.getPort();
LOG.info("Election port: " + port);
Thread.sleep(1000);
Socket sock = new Socket();
sock.connect(peers.get(1L).electionAddr, 5000);
long begin = Time.currentElapsedTime();
// Read without sending data. Verify timeout.
cnxManager.receiveConnection(sock);
long end = Time.currentElapsedTime();
if ((end - begin) > ((peer.getSyncLimit() * peer.getTickTime()) + 500))
Assert.fail("Waited more than necessary");
cnxManager.halt();
Assert.assertFalse(cnxManager.listener.isAlive());
}
Aggregations