use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class RemoveLearnersRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.RemoveLearnersRequest request, final RpcRequestClosure done) {
final List<PeerId> oldLearners = ctx.node.listLearners();
final List<PeerId> removeingLearners = 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);
}
removeingLearners.add(peer);
}
LOG.info("Receive RemoveLearnersRequest to {} from {}, removing {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), removeingLearners);
ctx.node.removeLearners(removeingLearners, 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());
if (!removeingLearners.contains(peer)) {
rb.addNewLearners(peer.toString());
}
}
done.sendResponse(rb.build());
}
});
return null;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class ResetPeerRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.ResetPeerRequest request, final RpcRequestClosure done) {
final Configuration newConf = new Configuration();
for (final String peerIdStr : request.getNewPeersList()) {
final PeerId peer = new PeerId();
if (peer.parse(peerIdStr)) {
newConf.addPeer(peer);
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
}
}
LOG.info("Receive ResetPeerRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), newConf);
final Status st = ctx.node.resetPeers(newConf);
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.
@SuppressWarnings("unchecked")
PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, final Connection conn) {
ConcurrentMap<PeerPair, PeerRequestContext> groupContexts = this.peerRequestContexts.get(groupId);
if (groupContexts == null) {
groupContexts = new ConcurrentHashMap<>();
final ConcurrentMap<PeerPair, PeerRequestContext> existsCtxs = this.peerRequestContexts.putIfAbsent(groupId, groupContexts);
if (existsCtxs != null) {
groupContexts = existsCtxs;
}
}
PeerRequestContext peerCtx = groupContexts.get(pair);
if (peerCtx == null) {
synchronized (Utils.withLockObject(groupContexts)) {
peerCtx = groupContexts.get(pair);
// double check in lock
if (peerCtx == null) {
// only one thread to process append entries for every jraft node
final PeerId peer = new PeerId();
final boolean parsed = peer.parse(pair.local);
assert (parsed);
final Node node = NodeManager.getInstance().get(groupId, peer);
assert (node != null);
peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
groupContexts.put(pair, peerCtx);
}
}
}
// Add the pair to connection attribute metadata.
if (conn != null) {
Set<PeerPair> pairs;
if ((pairs = (Set<PeerPair>) conn.getAttribute(PAIR_ATTR)) == null) {
pairs = new ConcurrentHashSet<>();
Set<PeerPair> existsPairs = (Set<PeerPair>) conn.setAttributeIfAbsent(PAIR_ATTR, pairs);
if (existsPairs != null) {
pairs = existsPairs;
}
}
pairs.add(pair);
}
return peerCtx;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class ReplicatorGroupImpl method stopAllAndFindTheNextCandidate.
@Override
public ThreadId stopAllAndFindTheNextCandidate(final ConfigurationEntry conf) {
ThreadId candidate = null;
final PeerId candidateId = findTheNextCandidate(conf);
if (candidateId != null) {
candidate = this.replicatorMap.get(candidateId);
} else {
LOG.info("Fail to find the next candidate.");
}
for (final ThreadId r : this.replicatorMap.values()) {
if (r != candidate) {
Replicator.stop(r);
}
}
this.replicatorMap.clear();
this.failureReplicators.clear();
return candidate;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class AddLearnersRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.AddLearnersRequest request, final RpcRequestClosure done) {
final List<PeerId> oldLearners = ctx.node.listLearners();
final List<PeerId> addingLearners = 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);
}
addingLearners.add(peer);
}
LOG.info("Receive AddLearnersRequest to {} from {}, adding {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), addingLearners);
ctx.node.addLearners(addingLearners, 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());
rb.addNewLearners(peer.toString());
}
for (final PeerId peer : addingLearners) {
if (!oldLearners.contains(peer)) {
rb.addNewLearners(peer.toString());
}
}
done.sendResponse(rb.build());
}
});
return null;
}
Aggregations