Java Code Examples for com.alipay.sofa.jraft.Node#getLeaderId()

The following examples show how to use com.alipay.sofa.jraft.Node#getLeaderId() . 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: TestCluster.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure all peers leader is expectAddr
 * @param expectAddr expected address
 * @throws InterruptedException if interrupted
 */
public void ensureLeader(final Endpoint expectAddr) throws InterruptedException {
    while (true) {
        this.lock.lock();
        for (final Node node : this.nodes) {
            final PeerId leaderId = node.getLeaderId();
            if (!leaderId.getEndpoint().equals(expectAddr)) {
                this.lock.unlock();
                Thread.sleep(10);
                continue;
            }
        }
        // all is ready
        this.lock.unlock();
        return;
    }
}
 
Example 2
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure all peers leader is expectAddr
 * @param expectAddr expected address
 * @throws InterruptedException if interrupted
 */
public void ensureLeader(final Endpoint expectAddr) throws InterruptedException {
    while (true) {
        this.lock.lock();
        for (final Node node : this.nodes) {
            final PeerId leaderId = node.getLeaderId();
            if (!leaderId.getEndpoint().equals(expectAddr)) {
                this.lock.unlock();
                Thread.sleep(10);
                continue;
            }
        }
        // all is ready
        this.lock.unlock();
        return;
    }
}
 
Example 3
Source File: GetLeaderRequestProcessor.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public Message processRequest(final GetLeaderRequest request, final RpcRequestClosure done) {
    List<Node> nodes = new ArrayList<>();
    final String groupId = getGroupId(request);
    if (request.hasPeerId()) {
        final String peerIdStr = getPeerId(request);
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            final Status st = new Status();
            nodes.add(getNode(groupId, peer, st));
            if (!st.isOk()) {
                return RpcFactoryHelper //
                    .responseFactory() //
                    .newResponse(defaultResp(), st);
            }
        } else {
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    } else {
        nodes = NodeManager.getInstance().getNodesByGroupId(groupId);
    }
    if (nodes == null || nodes.isEmpty()) {
        return RpcFactoryHelper //
            .responseFactory() //
            .newResponse(defaultResp(), RaftError.ENOENT, "No nodes in group %s", groupId);
    }
    for (final Node node : nodes) {
        final PeerId leader = node.getLeaderId();
        if (leader != null && !leader.isEmpty()) {
            return GetLeaderResponse.newBuilder().setLeaderId(leader.toString()).build();
        }
    }
    return RpcFactoryHelper //
        .responseFactory() //
        .newResponse(defaultResp(), RaftError.EAGAIN, "Unknown leader");
}