Search in sources :

Example 36 with ZKDatabase

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();
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 37 with ZKDatabase

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();
}
Also used : TestEndpoint(com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint) Stat(org.apache.zookeeper.data.Stat) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ParseException(java.text.ParseException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase)

Example 38 with ZKDatabase

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ZKDatabase(org.apache.zookeeper.server.ZKDatabase)

Aggregations

ZKDatabase (org.apache.zookeeper.server.ZKDatabase)38 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)17 File (java.io.File)13 IOException (java.io.IOException)9 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)9 Test (org.junit.jupiter.api.Test)9 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)8 Stat (org.apache.zookeeper.data.Stat)6 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 ServerStats (org.apache.zookeeper.server.ServerStats)3 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 TestEndpoint (com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint)2 InetSocketAddress (java.net.InetSocketAddress)2 SelectionKey (java.nio.channels.SelectionKey)2 SocketChannel (java.nio.channels.SocketChannel)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2