Search in sources :

Example 6 with QuorumPeer

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);
    }
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Election(org.apache.zookeeper.server.quorum.Election)

Example 7 with QuorumPeer

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);
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer)

Example 8 with QuorumPeer

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);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) QuorumMaj(org.apache.zookeeper.server.quorum.flexible.QuorumMaj) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZKDatabase(org.apache.zookeeper.server.ZKDatabase)

Example 9 with QuorumPeer

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");
    }
}
Also used : QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 10 with QuorumPeer

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());
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) QuorumCnxManager(org.apache.zookeeper.server.quorum.QuorumCnxManager) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)39 Test (org.junit.Test)24 InetSocketAddress (java.net.InetSocketAddress)19 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)19 ZooKeeper (org.apache.zookeeper.ZooKeeper)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 QuorumCnxManager (org.apache.zookeeper.server.quorum.QuorumCnxManager)6 ByteBuffer (java.nio.ByteBuffer)5 File (java.io.File)4 HashMap (java.util.HashMap)4 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)4 Stat (org.apache.zookeeper.data.Stat)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Socket (java.net.Socket)2 SocketChannel (java.nio.channels.SocketChannel)2 LinkedHashSet (java.util.LinkedHashSet)2 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)2 KeeperException (org.apache.zookeeper.KeeperException)2 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)2