use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class FLERestartTest method testLERestart.
@Test
public void testLERestart() throws Exception {
LOG.info("TestLE: " + getTestName() + ", " + count);
for (int i = 0; i < count; i++) {
peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
tmpdir[i] = ClientBase.createTmpDir();
port[i] = PortAssignment.unique();
}
for (int i = 0; i < count; i++) {
QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2);
peer.startLeaderElection();
FLERestartThread thread = new FLERestartThread(peer, i);
thread.start();
restartThreads.add(thread);
}
LOG.info("Started threads " + getTestName());
for (int i = 0; i < restartThreads.size(); i++) {
restartThreads.get(i).join(10000);
if (restartThreads.get(i).isAlive()) {
Assert.fail("Threads didn't join");
}
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class LENonTerminateTest method testNonTermination.
/**
* This tests ZK-569.
* With three peers A, B and C, the following could happen:
* 1. Round 1, A,B and C all vote for themselves
* 2. Round 2, C dies, A and B vote for C
* 3. Because C has died, votes for it are ignored, but A and B never
* reset their votes. Hence LE never terminates. ZK-569 fixes this by
* resetting votes to themselves if the set of votes for live peers is null.
*/
@Test
public void testNonTermination() throws Exception {
LOG.info("TestNonTermination: " + getTestName() + ", " + count);
for (int i = 0; i < count; i++) {
int clientport = PortAssignment.unique();
peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", clientport), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
tmpdir[i] = ClientBase.createTmpDir();
port[i] = clientport;
}
/*
* peer1 and peer2 are A and B in the above example.
*/
QuorumPeer peer1 = new MockQuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 0, 0, 2, 2, 2);
peer1.startLeaderElection();
LEThread thread1 = new LEThread(peer1, 0);
QuorumPeer peer2 = new MockQuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 0, 1, 2, 2, 2);
peer2.startLeaderElection();
LEThread thread2 = new LEThread(peer2, 1);
/*
* Start mock server.
*/
Thread thread3 = new Thread() {
public void run() {
try {
mockServer();
} catch (Exception e) {
LOG.error("exception", e);
Assert.fail("Exception when running mocked server " + e);
}
}
};
thread3.start();
Assert.assertTrue("mockServer did not start in 5s", mockLatch.await(5000, TimeUnit.MILLISECONDS));
thread1.start();
thread2.start();
/*
* Occasionally seen false negatives with a 5s timeout.
*/
thread1.join(15000);
thread2.join(15000);
thread3.join(15000);
if (thread1.isAlive() || thread2.isAlive() || thread3.isAlive()) {
Assert.fail("Threads didn't join");
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class LETest method testLE.
@Test
public void testLE() throws Exception {
int count = 30;
HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>(count);
ArrayList<LEThread> threads = new ArrayList<LEThread>(count);
File[] tmpdir = new File[count];
int[] port = new int[count];
votes = new Vote[count];
for (int i = 0; i < count; i++) {
peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
tmpdir[i] = ClientBase.createTmpDir();
port[i] = PortAssignment.unique();
}
LeaderElection[] le = new LeaderElection[count];
leaderDies = true;
boolean allowOneBadLeader = leaderDies;
for (int i = 0; i < le.length; i++) {
QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 0, i, 1000, 2, 2);
peer.startLeaderElection();
le[i] = new LeaderElection(peer);
LEThread thread = new LEThread(le[i], peer, i);
thread.start();
threads.add(thread);
}
for (int i = 0; i < threads.size(); i++) {
threads.get(i).join(15000);
if (threads.get(i).isAlive()) {
Assert.fail("Threads didn't join");
}
}
long id = votes[0].getId();
for (int i = 1; i < votes.length; i++) {
if (votes[i] == null) {
Assert.fail("Thread " + i + " had a null vote");
}
if (votes[i].getId() != id) {
if (allowOneBadLeader && votes[i].getId() == i) {
allowOneBadLeader = false;
} else {
Assert.fail("Thread " + i + " got " + votes[i].getId() + " expected " + id);
}
}
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class QuorumUtil method start.
public void start(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();
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class QuorumBase method getPeersMatching.
public String getPeersMatching(ServerState state) {
StringBuilder hosts = new StringBuilder();
for (QuorumPeer p : getPeerList()) {
if (p.getPeerState() == state) {
hosts.append(String.format("%s:%d,", LOCALADDR, p.getClientAddress().getPort()));
}
}
LOG.info("getPeersMatching ports are {}", hosts);
return hosts.toString();
}
Aggregations