use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class ResetLearnersRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.ResetLearnersRequest request, final RpcRequestClosure done) {
final List<PeerId> oldLearners = ctx.node.listLearners();
final List<PeerId> newLearners = new ArrayList<>(request.getLearnersCount());
for (final String peerStr : request.getLearnersList()) {
final PeerId peer = new PeerId();
if (!peer.parse(peerStr)) {
return RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerStr);
}
newLearners.add(peer);
}
LOG.info("Receive ResetLearnersRequest to {} from {}, resetting into {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), newLearners);
ctx.node.resetLearners(newLearners, status -> {
if (!status.isOk()) {
done.run(status);
} else {
final CliRequests.LearnersOpResponse.Builder rb = CliRequests.LearnersOpResponse.newBuilder();
for (final PeerId peer : oldLearners) {
rb.addOldLearners(peer.toString());
}
for (final PeerId peer : newLearners) {
rb.addNewLearners(peer.toString());
}
done.sendResponse(rb.build());
}
});
return null;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class TransferLeaderRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.TransferLeaderRequest request, final RpcRequestClosure done) {
final PeerId peer = new PeerId();
if (request.hasPeerId() && !peer.parse(request.getPeerId())) {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", request.getPeerId());
}
LOG.info("Receive TransferLeaderRequest to {} from {}, newLeader will be {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), peer);
final Status st = ctx.node.transferLeadershipTo(peer);
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class ClientServiceConnectionEventProcessor method onEvent.
@Override
public void onEvent(final String remoteAddr, final Connection conn) {
final PeerId peer = new PeerId();
if (peer.parse(remoteAddr)) {
LOG.info("Peer {} is connected", peer);
this.rgGroup.checkReplicator(peer, true);
} else {
LOG.error("Fail to parse peer: {}", remoteAddr);
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class AddPeerRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.AddPeerRequest request, final RpcRequestClosure done) {
final List<PeerId> oldPeers = ctx.node.listPeers();
final String addingPeerIdStr = request.getPeerId();
final PeerId addingPeer = new PeerId();
if (addingPeer.parse(addingPeerIdStr)) {
LOG.info("Receive AddPeerRequest to {} from {}, adding {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), addingPeerIdStr);
ctx.node.addPeer(addingPeer, status -> {
if (!status.isOk()) {
done.run(status);
} else {
final CliRequests.AddPeerResponse.Builder rb = CliRequests.AddPeerResponse.newBuilder();
boolean alreadyExists = false;
for (final PeerId oldPeer : oldPeers) {
rb.addOldPeers(oldPeer.toString());
rb.addNewPeers(oldPeer.toString());
if (oldPeer.equals(addingPeer)) {
alreadyExists = true;
}
}
if (!alreadyExists) {
rb.addNewPeers(addingPeerIdStr);
}
done.sendResponse(rb.build());
}
});
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", addingPeerIdStr);
}
return null;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class GetLeaderRequestProcessor method processRequest.
@Override
public Message processRequest(final CliRequests.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 CliRequests.GetLeaderResponse.newBuilder().setLeaderId(leader.toString()).build();
}
}
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EAGAIN, "Unknown leader");
}
Aggregations