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(value = 30)
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++) {
assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
}
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, "Leader must have been elected by now");
// 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++) {
assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
}
// revert back the error
leader.getActiveServer().setZKDatabase(originalZKDatabase);
// verify that now ZooKeeper service is up and running
leader = getLeaderQuorumPeer(mt);
assertNotNull(leader, "New leader must have been elected by now");
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(uniqueNode, createNode, "Failed to create znode");
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 QuorumUtil method start.
public void start(int id) throws IOException {
PeerStruct ps = getPeer(id);
LOG.info("Creating QuorumPeer {}; public port {}", ps.id, ps.clientPort);
ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, ps.id, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
if (localSessionEnabled) {
ps.peer.enableLocalSessions(true);
}
assertEquals(ps.clientPort, ps.peer.getClientPort());
ps.peer.start();
}
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(ClientBase.waitForServerDown("127.0.0.1:" + server.getClientPort(), ClientBase.CONNECTION_TIMEOUT), "Waiting for server down");
} else {
assertNull(leader, "More than one leader found");
leader = server;
}
}
assertTrue(ClientBase.waitForServerState(leader, ClientBase.CONNECTION_TIMEOUT, QuorumStats.Provider.LOOKING_STATE), "Leader is not in Looking state");
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project coprhd-controller by CoprHD.
the class CoordinatorImpl method runFromConfig.
// Start Zookeeper peer in cluster mode
private void runFromConfig(SpringQuorumPeerConfig config) throws Exception {
_log.info(String.format("Starting quorum peer from config for %d", config.getServerId()));
ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
QuorumPeer quorumPeer = new QuorumPeer();
quorumPeer.setClientPortAddress(config.getClientPortAddress());
quorumPeer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
quorumPeer.setQuorumPeers(config.getServers());
quorumPeer.setElectionType(config.getElectionAlg());
quorumPeer.setMyid(config.getServerId());
quorumPeer.setTickTime(config.getTickTime());
quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout());
quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout());
quorumPeer.setInitLimit(config.getInitLimit());
quorumPeer.setSyncLimit(config.getSyncLimit());
quorumPeer.setQuorumVerifier(config.getQuorumVerifier());
quorumPeer.setCnxnFactory(cnxnFactory);
quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
quorumPeer.setLearnerType(config.getPeerType());
quorumPeer.setSyncEnabled(config.getSyncEnabled());
quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs());
quorumPeer.start();
}
use of org.apache.zookeeper.server.quorum.QuorumPeer in project ecf by eclipse.
the class ZooDiscoveryContainer method startQuorumPeer.
/**
* Start a local ZooKeeer server to write nodes to. It plays as a peer
* within a replicated servers configuration. Implied by
* {@link IDiscoveryConfig#ZOODISCOVERY_FLAVOR_REPLICATED} configuration.
*
* @param conf
*/
void startQuorumPeer(final Configuration conf) {
if (this.quorumPeer != null && this.quorumPeer.isAlive()) {
return;
} else if (this.quorumPeer != null && !this.quorumPeer.isAlive()) {
this.quorumPeer.start();
return;
}
try {
final QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
quorumPeerConfig.parse(conf.getConfFile());
QuorumPeer.Factory qpFactory = new QuorumPeer.Factory() {
public QuorumPeer create(NIOServerCnxn.Factory cnxnFactory) throws IOException {
ServerConfig serverConfig = new ServerConfig();
serverConfig.readFrom(quorumPeerConfig);
QuorumPeer peer = new QuorumPeer(quorumPeerConfig.getServers(), new File(serverConfig.getDataDir()), new File(serverConfig.getDataLogDir()), quorumPeerConfig.getElectionAlg(), quorumPeerConfig.getServerId(), quorumPeerConfig.getTickTime(), quorumPeerConfig.getInitLimit(), quorumPeerConfig.getSyncLimit(), cnxnFactory, quorumPeerConfig.getQuorumVerifier());
ZooDiscoveryContainer.this.quorumPeer = peer;
return peer;
}
public NIOServerCnxn.Factory createConnectionFactory() throws IOException {
return new NIOServerCnxn.Factory(quorumPeerConfig.getClientPortAddress());
}
};
quorumPeer = qpFactory.create(qpFactory.createConnectionFactory());
quorumPeer.start();
quorumPeer.setDaemon(true);
isQuorumPeerReady = true;
} catch (Exception e) {
// $NON-NLS-1$
Logger.log(LogService.LOG_ERROR, "Zookeeper quorum cannot be started! ", e);
isQuorumPeerReady = false;
}
}
Aggregations