use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class GetLeaderRequestProcessor method processRequest.
@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");
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
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);
}
}
use of com.alipay.sofa.jraft.Node in project mmqtt by MrHKing.
the class JRaftMaintainService method execute.
/**
* Execute relevant commands.
*
* @param args {@link Map}
* @return {@link RestResult}
*/
public RestResult<String> execute(Map<String, String> args) {
final CliService cliService = raftServer.getCliService();
if (args.containsKey(JRaftConstants.GROUP_ID)) {
final String groupId = args.get(JRaftConstants.GROUP_ID);
final Node node = raftServer.findNodeByGroup(groupId);
return single(cliService, groupId, node, args);
}
Map<String, JRaftServer.RaftGroupTuple> tupleMap = raftServer.getMultiRaftGroup();
for (Map.Entry<String, JRaftServer.RaftGroupTuple> entry : tupleMap.entrySet()) {
final String group = entry.getKey();
final Node node = entry.getValue().getNode();
RestResult<String> result = single(cliService, group, node, args);
if (!result.ok()) {
return result;
}
}
return RestResultUtils.success();
}
use of com.alipay.sofa.jraft.Node in project nacos by alibaba.
the class JRaftServer method applyOperation.
public void applyOperation(Node node, Message data, FailoverClosure closure) {
final Task task = new Task();
task.setDone(new NacosClosure(data, status -> {
NacosClosure.NacosStatus nacosStatus = (NacosClosure.NacosStatus) status;
closure.setThrowable(nacosStatus.getThrowable());
closure.setResponse(nacosStatus.getResponse());
closure.run(nacosStatus);
}));
// add request type field at the head of task data.
byte[] requestTypeFieldBytes = new byte[2];
requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
if (data instanceof ReadRequest) {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
} else {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
}
byte[] dataBytes = data.toByteArray();
task.setData((ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes).put(dataBytes).position(0));
node.apply(task);
}
use of com.alipay.sofa.jraft.Node in project nacos by alibaba.
the class NacosStateMachine method onApply.
@Override
public void onApply(Iterator iter) {
int index = 0;
int applied = 0;
Message message;
NacosClosure closure = null;
try {
while (iter.hasNext()) {
Status status = Status.OK();
try {
if (iter.done() != null) {
closure = (NacosClosure) iter.done();
message = closure.getMessage();
} else {
final ByteBuffer data = iter.getData();
message = ProtoMessageUtil.parse(data.array());
if (message instanceof ReadRequest) {
// 'iter.done() == null' means current node is follower, ignore read operation
applied++;
index++;
iter.next();
continue;
}
}
LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "receive log : {}", message);
if (message instanceof WriteRequest) {
Response response = processor.onApply((WriteRequest) message);
postProcessor(response, closure);
}
if (message instanceof ReadRequest) {
Response response = processor.onRequest((ReadRequest) message);
postProcessor(response, closure);
}
} catch (Throwable e) {
index++;
status.setError(RaftError.UNKNOWN, e.toString());
Optional.ofNullable(closure).ifPresent(closure1 -> closure1.setThrowable(e));
throw e;
} finally {
Optional.ofNullable(closure).ifPresent(closure1 -> closure1.run(status));
}
applied++;
index++;
iter.next();
}
} catch (Throwable t) {
Loggers.RAFT.error("processor : {}, stateMachine meet critical error: {}.", processor, t);
iter.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE, "StateMachine meet critical error: %s.", ExceptionUtil.getStackTrace(t)));
}
}
Aggregations