Search in sources :

Example 1 with TestEndpoint

use of com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint in project commons by twitter.

the class AngryBirdZooKeeperServer method getSessionIdFromHostPair.

/**
 * Returns the session whose corresponding znode encodes "host:port"
 *
 * @param host ip address of the endpoint
 * @param port endpoint port
 * @return session id of the corresponding zk session if a match is found.
 */
private Optional<Long> getSessionIdFromHostPair(String host, int port) {
    // TODO(vinod): Instead of (host, port) args use the more generic byte[] as args
    // so that comparison can be made on znodes that are ServerSet ephemerals
    ZKDatabase zkDb = zooKeeperServer.getZKDatabase();
    for (long sessionId : zkDb.getSessions()) {
        for (String path : zkDb.getEphemerals(sessionId)) {
            LOG.info("SessionId:" + sessionId + " Path:" + path);
            try {
                String data = new String(zkDb.getData(path, new Stat(), null));
                LOG.info("Data in znode: " + data);
                TestEndpoint endpoint = parseEndpoint(data);
                LOG.info("Extracted endpoint " + endpoint);
                if (endpoint.getHost().equals(host) && endpoint.getPort() == port) {
                    LOG.info(String.format("Matching session id %s found for endpoint %s:%s", sessionId, host, port));
                    return Optional.of(sessionId);
                }
            } catch (NoNodeException e) {
                LOG.severe("Exception getting data for Path:" + path + " : " + e);
            } catch (ParseException e) {
                LOG.severe("Exception parsing data: " + e);
            } catch (NumberFormatException e) {
                LOG.severe("Exception in url format " + 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 2 with TestEndpoint

use of com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint in project commons by twitter.

the class AngryBirdZooKeeperServer method parseEndpoint.

private TestEndpoint parseEndpoint(String data) throws ParseException {
    ImmutableList<String> endpointComponents = ImmutableList.copyOf(AT_SPLITTER.split(data));
    if (endpointComponents.size() != 2) {
        throw new ParseException("Unknown znode data: Expected format id@host:port", 0);
    }
    String nodeId = endpointComponents.get(0);
    HostAndPort pair;
    try {
        pair = HostAndPort.fromString(endpointComponents.get(1));
    } catch (IllegalArgumentException e) {
        throw new ParseException("Failed to parse endpoint data: " + endpointComponents.get(1), data.indexOf('@'));
    }
    TestEndpoint endpoint = new TestEndpoint();
    endpoint.setNodeId(nodeId);
    endpoint.setHost(pair.getHostText());
    endpoint.setPort(pair.getPort());
    return endpoint;
}
Also used : HostAndPort(com.google.common.net.HostAndPort) TestEndpoint(com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint) ParseException(java.text.ParseException)

Example 3 with TestEndpoint

use of com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint 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)

Aggregations

TestEndpoint (com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint)3 ParseException (java.text.ParseException)3 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)2 Stat (org.apache.zookeeper.data.Stat)2 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)2 HostAndPort (com.google.common.net.HostAndPort)1