use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class QuorumBase method setupServer.
public void setupServer(int i) throws IOException {
int tickTime = 2000;
int initLimit = 3;
int syncLimit = 3;
if (peers == null) {
peers = new HashMap<Long, QuorumServer>();
peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress(LOCALADDR, port1), new InetSocketAddress(LOCALADDR, portLE1), new InetSocketAddress(LOCALADDR, portClient1), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress(LOCALADDR, port2), new InetSocketAddress(LOCALADDR, portLE2), new InetSocketAddress(LOCALADDR, portClient2), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress(LOCALADDR, port3), new InetSocketAddress(LOCALADDR, portLE3), new InetSocketAddress(LOCALADDR, portClient3), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress(LOCALADDR, port4), new InetSocketAddress(LOCALADDR, portLE4), new InetSocketAddress(LOCALADDR, portClient4), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress(LOCALADDR, port5), new InetSocketAddress(LOCALADDR, portLE5), new InetSocketAddress(LOCALADDR, portClient5), LearnerType.PARTICIPANT));
}
switch(i) {
case 1:
LOG.info("creating QuorumPeer 1 port " + portClient1);
s1 = new QuorumPeer(peers, s1dir, s1dir, portClient1, 3, 1, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient1, s1.getClientPort());
break;
case 2:
LOG.info("creating QuorumPeer 2 port " + portClient2);
s2 = new QuorumPeer(peers, s2dir, s2dir, portClient2, 3, 2, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient2, s2.getClientPort());
break;
case 3:
LOG.info("creating QuorumPeer 3 port " + portClient3);
s3 = new QuorumPeer(peers, s3dir, s3dir, portClient3, 3, 3, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient3, s3.getClientPort());
break;
case 4:
LOG.info("creating QuorumPeer 4 port " + portClient4);
s4 = new QuorumPeer(peers, s4dir, s4dir, portClient4, 3, 4, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient4, s4.getClientPort());
break;
case 5:
LOG.info("creating QuorumPeer 5 port " + portClient5);
s5 = new QuorumPeer(peers, s5dir, s5dir, portClient5, 3, 5, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient5, s5.getClientPort());
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class CnxManagerTest method testCnxManagerNPE.
/**
* Tests a bug in QuorumCnxManager that causes a NPE when a 3.4.6
* observer connects to a 3.5.0 server.
* {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1789}
*
* @throws Exception
*/
@Test
public void testCnxManagerNPE() throws Exception {
// the connecting peer (id = 2) is a 3.4.6 observer
peers.get(2L).type = LearnerType.OBSERVER;
QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 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);
SocketChannel sc = SocketChannel.open();
sc.socket().connect(peers.get(1L).electionAddr, 5000);
/*
* Write id (3.4.6 protocol). This previously caused a NPE in
* QuorumCnxManager.
*/
byte[] msgBytes = new byte[8];
ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes);
msgBuffer.putLong(2L);
msgBuffer.position(0);
sc.write(msgBuffer);
msgBuffer = ByteBuffer.wrap(new byte[8]);
// write length of message
msgBuffer.putInt(4);
// write message
msgBuffer.putInt(5);
msgBuffer.position(0);
sc.write(msgBuffer);
Message m = cnxManager.pollRecvQueue(1000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(m);
peer.shutdown();
cnxManager.halt();
Assert.assertFalse(cnxManager.listener.isAlive());
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class NonRecoverableErrorTest method testZooKeeperServiceAvailableOnLeader.
/**
* Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
* Test to verify that even after non recoverable error (error while
* writing transaction log), ZooKeeper is still available.
*/
@Test(timeout = 30000)
public void testZooKeeperServiceAvailableOnLeader() throws Exception {
int SERVER_COUNT = 3;
final int[] clientPorts = new int[SERVER_COUNT];
StringBuilder sb = new StringBuilder();
String server;
for (int i = 0; i < SERVER_COUNT; i++) {
clientPorts[i] = PortAssignment.unique();
server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i];
sb.append(server + "\n");
}
String currentQuorumCfgSection = sb.toString();
MainThread[] mt = new MainThread[SERVER_COUNT];
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
mt[i].start();
}
// ensure server started
for (int i = 0; i < SERVER_COUNT; i++) {
Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT));
}
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
String data = "originalData";
zk.create(NODE_PATH, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// get information of current leader
QuorumPeer leader = getLeaderQuorumPeer(mt);
assertNotNull("Leader must have been elected by now", leader);
// inject problem in leader
FileTxnSnapLog snapLog = leader.getActiveServer().getTxnLogFactory();
FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) {
@Override
public void commit() throws IOException {
throw new IOException("Input/output error");
}
};
ZKDatabase originalZKDatabase = leader.getActiveServer().getZKDatabase();
long leaderCurrentEpoch = leader.getCurrentEpoch();
ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
leader.getActiveServer().setZKDatabase(newDB);
try {
// do create operation, so that injected IOException is thrown
zk.create(uniqueZnode(), data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
fail("IOException is expected due to error injected to transaction log commit");
} catch (Exception e) {
// do nothing
}
// resetting watcher so that this watcher can be again used to ensure
// that the zkClient is able to re-establish connection with the
// newly elected zookeeper quorum.
watcher.reset();
waitForNewLeaderElection(leader, leaderCurrentEpoch);
// takes place
for (int i = 0; i < SERVER_COUNT; i++) {
Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT));
}
// revert back the error
leader.getActiveServer().setZKDatabase(originalZKDatabase);
// verify that now ZooKeeper service is up and running
leader = getLeaderQuorumPeer(mt);
assertNotNull("New leader must have been elected by now", leader);
String uniqueNode = uniqueZnode();
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
String createNode = zk.create(uniqueNode, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// if node is created successfully then it means that ZooKeeper service
// is available
assertEquals("Failed to create znode", uniqueNode, createNode);
zk.close();
// stop all severs
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i].shutdown();
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class ObserverLETest method testLEWithObserver.
/**
* See ZOOKEEPER-1294. Confirms that an observer will not support the quorum
* of a leader by forming a 5-node, 2-observer ensemble (so quorum size is 2).
* When all but the leader and one observer are shut down, the leader should
* enter the 'looking' state, not stay in the 'leading' state.
*/
@Test
public void testLEWithObserver() throws Exception {
QuorumPeer leader = null;
for (QuorumPeer server : Arrays.asList(qb.s1, qb.s2, qb.s3)) {
if (server.getServerState().equals(QuorumStats.Provider.FOLLOWING_STATE)) {
server.shutdown();
assertTrue("Waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + server.getClientPort(), ClientBase.CONNECTION_TIMEOUT));
} else {
assertNull("More than one leader found", leader);
leader = server;
}
}
assertTrue("Leader is not in Looking state", ClientBase.waitForServerState(leader, ClientBase.CONNECTION_TIMEOUT, QuorumStats.Provider.LOOKING_STATE));
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.
the class QuorumBase method startServers.
void startServers(boolean withObservers) throws Exception {
int tickTime = 2000;
int initLimit = 3;
int syncLimit = 3;
Map<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress(LOCALADDR, port1), new InetSocketAddress(LOCALADDR, portLE1), new InetSocketAddress(LOCALADDR, portClient1), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress(LOCALADDR, port2), new InetSocketAddress(LOCALADDR, portLE2), new InetSocketAddress(LOCALADDR, portClient2), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress(LOCALADDR, port3), new InetSocketAddress(LOCALADDR, portLE3), new InetSocketAddress(LOCALADDR, portClient3), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress(LOCALADDR, port4), new InetSocketAddress(LOCALADDR, portLE4), new InetSocketAddress(LOCALADDR, portClient4), LearnerType.PARTICIPANT));
peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress(LOCALADDR, port5), new InetSocketAddress(LOCALADDR, portLE5), new InetSocketAddress(LOCALADDR, portClient5), LearnerType.PARTICIPANT));
if (withObservers) {
peers.get(Long.valueOf(4)).type = LearnerType.OBSERVER;
peers.get(Long.valueOf(5)).type = LearnerType.OBSERVER;
}
LOG.info("creating QuorumPeer 1 port " + portClient1);
s1 = new QuorumPeer(peers, s1dir, s1dir, portClient1, 3, 1, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient1, s1.getClientPort());
LOG.info("creating QuorumPeer 2 port " + portClient2);
s2 = new QuorumPeer(peers, s2dir, s2dir, portClient2, 3, 2, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient2, s2.getClientPort());
LOG.info("creating QuorumPeer 3 port " + portClient3);
s3 = new QuorumPeer(peers, s3dir, s3dir, portClient3, 3, 3, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient3, s3.getClientPort());
LOG.info("creating QuorumPeer 4 port " + portClient4);
s4 = new QuorumPeer(peers, s4dir, s4dir, portClient4, 3, 4, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient4, s4.getClientPort());
LOG.info("creating QuorumPeer 5 port " + portClient5);
s5 = new QuorumPeer(peers, s5dir, s5dir, portClient5, 3, 5, tickTime, initLimit, syncLimit);
Assert.assertEquals(portClient5, s5.getClientPort());
if (withObservers) {
s4.setLearnerType(LearnerType.OBSERVER);
s5.setLearnerType(LearnerType.OBSERVER);
}
LOG.info("QuorumPeer 1 voting view: " + s1.getVotingView());
LOG.info("QuorumPeer 2 voting view: " + s2.getVotingView());
LOG.info("QuorumPeer 3 voting view: " + s3.getVotingView());
LOG.info("QuorumPeer 4 voting view: " + s4.getVotingView());
LOG.info("QuorumPeer 5 voting view: " + s5.getVotingView());
s1.enableLocalSessions(localSessionsEnabled);
s2.enableLocalSessions(localSessionsEnabled);
s3.enableLocalSessions(localSessionsEnabled);
s4.enableLocalSessions(localSessionsEnabled);
s5.enableLocalSessions(localSessionsEnabled);
s1.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled);
s2.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled);
s3.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled);
s4.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled);
s5.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled);
LOG.info("start QuorumPeer 1");
s1.start();
LOG.info("start QuorumPeer 2");
s2.start();
LOG.info("start QuorumPeer 3");
s3.start();
LOG.info("start QuorumPeer 4");
s4.start();
LOG.info("start QuorumPeer 5");
s5.start();
LOG.info("started QuorumPeer 5");
LOG.info("Checking ports " + hostPort);
for (String hp : hostPort.split(",")) {
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT));
LOG.info(hp + " is accepting client connections");
}
// interesting to see what's there...
JMXEnv.dump();
// make sure we have these 5 servers listed
Set<String> ensureNames = new LinkedHashSet<String>();
for (int i = 1; i <= 5; i++) {
ensureNames.add("InMemoryDataTree");
}
for (int i = 1; i <= 5; i++) {
ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + i + ",name2=");
}
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + j);
}
}
for (int i = 1; i <= 5; i++) {
ensureNames.add("name0=ReplicatedServer_id" + i);
}
JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()]));
}
Aggregations