use of org.apache.zookeeper.server.ZKDatabase 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.ZKDatabase in project commons by twitter.
the class AngryBirdZooKeeperServer method getFollowerSessionIdFromPath.
/**
* Return the session id of a follower candidate
*
* @param zkPath Znode path prefix of the candidates
* @param candidateId (optional) specific candidate id of follower to expire, otherwise random.
* @return session id of the corresponding zk session if a match is found
*/
private Optional<Long> getFollowerSessionIdFromPath(String zkPath, Optional<String> nodeId) {
Optional<Long> leaderSessionId = getLeaderSessionIdFromPath(zkPath);
if (!leaderSessionId.isPresent()) {
return leaderSessionId;
}
ZKDatabase zkDb = zooKeeperServer.getZKDatabase();
for (long sessionId : zkDb.getSessions()) {
if (sessionId == leaderSessionId.get()) {
continue;
}
for (String path : zkDb.getEphemerals(sessionId)) {
if (StringUtils.startsWith(path, zkPath)) {
LOG.info(String.format("Found session follower for %s: %s", zkPath, sessionId));
if (!nodeId.isPresent()) {
return Optional.of(sessionId);
} else {
TestEndpoint endpoint;
try {
endpoint = parseEndpoint(new String(zkDb.getData(path, new Stat(), null)));
if (endpoint.getNodeId().equals(nodeId.get())) {
return Optional.of(sessionId);
}
} catch (ParseException e) {
LOG.severe("Failed to parse endpoint " + path + ": " + e);
} catch (NoNodeException e) {
LOG.severe("Exception getting data for Path:" + path + " :" + e);
}
}
}
}
}
return Optional.absent();
}
use of org.apache.zookeeper.server.ZKDatabase in project commons by twitter.
the class AngryBirdZooKeeperServer method getLeaderSessionIdFromPath.
/**
* Return the session id of the leader candidate.
* See http://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
*
* @param zkPath Znode path prefix of the candidates
* @return the session id of the corresponding zk session if a match is found.
*/
private Optional<Long> getLeaderSessionIdFromPath(String zkPath) {
ZKDatabase zkDb = zooKeeperServer.getZKDatabase();
Long leaderSessionId = null;
Long masterSeq = Long.MAX_VALUE;
// Reg-ex pattern for sequence numbers in znode paths.
Pattern pattern = Pattern.compile("\\d+$");
// First find the session id of the leading scheduler.
for (long sessionId : zkDb.getSessions()) {
for (String path : zkDb.getEphemerals(sessionId)) {
if (StringUtils.startsWith(path, zkPath)) {
try {
// Get the sequence number.
Matcher matcher = pattern.matcher(path);
if (matcher.find()) {
LOG.info("Pattern matched path: " + path + " session: " + sessionId);
Long seq = Long.parseLong(matcher.group());
if (seq < masterSeq) {
masterSeq = seq;
leaderSessionId = sessionId;
}
}
} catch (NumberFormatException e) {
LOG.severe("Exception formatting sequence number " + e);
}
}
}
}
if (leaderSessionId != null) {
LOG.info(String.format("Found session leader for %s: %s", zkPath, leaderSessionId));
}
return Optional.of(leaderSessionId);
}
Aggregations