Search in sources :

Example 1 with SetLeaderRequest

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderRequest in project incubator-hugegraph by apache.

the class RaftGroupManagerImpl method setLeader.

@Override
public String setLeader(String endpoint) {
    PeerId newLeaderId = PeerId.parsePeer(endpoint);
    Node node = this.raftNode.node();
    // If expected endpoint has already been raft leader
    if (node.getLeaderId().equals(newLeaderId)) {
        return newLeaderId.toString();
    }
    if (this.raftNode.selfIsLeader()) {
        // If current node is the leader, transfer directly
        this.transferLeaderTo(endpoint);
    } else {
        // If current node is not leader, forward request to leader
        SetLeaderRequest request = SetLeaderRequest.newBuilder().setEndpoint(endpoint).build();
        try {
            RaftClosure<SetLeaderResponse> future;
            future = this.forwardToLeader(request);
            future.waitFinished();
        } catch (Throwable e) {
            throw new BackendException("Failed to set leader to '%s'", e, endpoint);
        }
    }
    return newLeaderId.toString();
}
Also used : SetLeaderResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderResponse) Node(com.alipay.sofa.jraft.Node) SetLeaderRequest(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

Node (com.alipay.sofa.jraft.Node)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 BackendException (com.baidu.hugegraph.backend.BackendException)1 SetLeaderRequest (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderRequest)1 SetLeaderResponse (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderResponse)1