Java Code Examples for org.elasticsearch.common.collect.ImmutableOpenMap#valuesIt()

The following examples show how to use org.elasticsearch.common.collect.ImmutableOpenMap#valuesIt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoutingProvider.java    From crate with Apache License 2.0 6 votes vote down vote up
public Routing forRandomMasterOrDataNode(RelationName relationName, DiscoveryNodes nodes) {
    DiscoveryNode localNode = nodes.getLocalNode();
    if (localNode.isMasterNode() || localNode.isDataNode()) {
        return forTableOnSingleNode(relationName, localNode.getId());
    }
    ImmutableOpenMap<String, DiscoveryNode> masterAndDataNodes = nodes.getMasterAndDataNodes();
    int randomIdx = seed % masterAndDataNodes.size();
    Iterator<DiscoveryNode> it = masterAndDataNodes.valuesIt();
    int currIdx = 0;
    while (it.hasNext()) {
        if (currIdx == randomIdx) {
            return forTableOnSingleNode(relationName, it.next().getId());
        }
        currIdx++;
    }
    throw new AssertionError("Cannot find a master or data node with given random index " + randomIdx);
}
 
Example 2
Source File: SysSnapshotsTableInfo.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private DiscoveryNode randomMasterOrDataNode() {
    ImmutableOpenMap<String, DiscoveryNode> masterAndDataNodes = clusterService.state().nodes().masterAndDataNodes();
    int randomIdx = random.nextInt(masterAndDataNodes.size());
    Iterator<DiscoveryNode> it = masterAndDataNodes.valuesIt();
    int currIdx = 0;
    while (it.hasNext()) {
        if (currIdx == randomIdx) {
            return it.next();
        }
        currIdx++;
    }
    throw new AssertionError(String.format(Locale.ENGLISH,
            "Cannot find a master or data node with given random index %d", randomIdx));
}