use of io.dingodb.raft.Node in project dingo by dingodb.
the class AppendEntriesRequestProcessor method processRequest0.
@Override
public Message processRequest0(final RaftServerService service, final RpcRequests.AppendEntriesRequest request, final RpcRequestClosure done) {
final Node node = (Node) service;
if (node.getRaftOptions().isReplicatorPipeline()) {
final String groupId = request.getGroupId();
final PeerPair pair = pairOf(request.getPeerId(), request.getServerId());
boolean isHeartbeat = isHeartbeatRequest(request);
int reqSequence = -1;
if (!isHeartbeat) {
reqSequence = getAndIncrementSequence(groupId, pair, done.getRpcCtx().getConnection());
}
final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, defaultResp(), groupId, pair, reqSequence, isHeartbeat));
if (response != null) {
if (isHeartbeat) {
done.getRpcCtx().sendResponse(response);
} else {
sendSequenceResponse(groupId, pair, reqSequence, done.getRpcCtx(), response);
}
}
return null;
} else {
return service.handleAppendEntriesRequest(request, done);
}
}
use of io.dingodb.raft.Node 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.Node in project dingo by dingodb.
the class BaseCliRequestProcessor method processRequest.
@Override
public Message processRequest(T request, RpcRequestClosure done) {
String groupId = getGroupId(request);
String peerIdStr = getPeerId(request);
PeerId peerId = null;
if (!StringUtils.isBlank(peerIdStr)) {
peerId = new PeerId();
if (!peerId.parse(peerIdStr)) {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer: %s", peerIdStr);
}
}
Status st = new Status();
Node node = getNode(groupId, peerId, st);
if (!st.isOk()) {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st.getCode(), st.getErrorMsg());
} else {
return processRequest0(new CliRequestContext(node, groupId, peerId), request, done);
}
}
use of io.dingodb.raft.Node 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");
}
use of io.dingodb.raft.Node in project dingo by dingodb.
the class NodeRequestProcessor method processRequest.
@Override
public Message processRequest(final T request, final RpcRequestClosure done) {
final PeerId peer = new PeerId();
final String peerIdStr = getPeerId(request);
if (peer.parse(peerIdStr)) {
final String groupId = getGroupId(request);
final Node node = NodeManager.getInstance().get(groupId, peer);
if (node != null) {
return processRequest0((RaftServerService) node, request, done);
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr, groupId);
}
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
}
}
Aggregations