use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class NodeRequestProcessorTest method testOK.
@Test
public void testOK() {
Node node = Mockito.mock(Node.class, withSettings().extraInterfaces(RaftServerService.class));
Mockito.when(node.getGroupId()).thenReturn("test");
PeerId peerId = new PeerId("localhost", 8081);
Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", peerId));
asyncContext.getNodeManager().add(node);
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertEquals(0, resp.errorCode());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ActionRequestProcessor method handleRequest.
/**
* {@inheritDoc}
*/
@Override
public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
Node node = rpcCtx.getNodeManager().get(request.groupId(), new PeerId(rpcCtx.getLocalAddress()));
if (node == null) {
rpcCtx.sendResponse(factory.errorResponse().errorCode(RaftError.UNKNOWN.getNumber()).build());
return;
}
JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
// Apply a filter before commiting to STM.
CompletableFuture<Void> fut = fsm.getListener().onBeforeApply(request.command());
if (fut != null) {
fut.handle(new BiFunction<Void, Throwable, Void>() {
@Override
public Void apply(Void ignored, Throwable err) {
if (err == null) {
if (request.command() instanceof WriteCommand) {
applyWrite(node, request, rpcCtx);
} else {
applyRead(node, request, rpcCtx);
}
} else {
sendSMError(rpcCtx, err, false);
}
return null;
}
});
} else {
if (request.command() instanceof WriteCommand) {
applyWrite(node, request, rpcCtx);
} else {
applyRead(node, request, rpcCtx);
}
}
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
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 //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EINVAL, "Fail to parse peer: %s", peerIdStr);
}
}
Status st = new Status();
Node node = getNode(groupId, peerId, st, done.getRpcCtx().getNodeManager());
if (!st.isOk()) {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), st.getCode(), st.getErrorMsg());
} else {
return processRequest0(new CliRequestContext(node, groupId, peerId), request, new IgniteCliRpcRequestClosure(node, done));
}
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class AppendEntriesRequestProcessor method processRequest0.
@Override
public Message processRequest0(final RaftServerService service, final AppendEntriesRequest request, final RpcRequestClosure done) {
final Node node = (Node) service;
if (node.getRaftOptions().isReplicatorPipeline()) {
final String groupId = request.groupId();
final PeerPair pair = pairOf(request.peerId(), request.serverId());
boolean isHeartbeat = isHeartbeatRequest(request);
int reqSequence = -1;
if (!isHeartbeat) {
reqSequence = getAndIncrementSequence(groupId, pair, done.getRpcCtx().getNodeManager());
}
final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, groupId, pair, reqSequence, isHeartbeat));
if (response != null) {
// heartbeat or probe request
if (isHeartbeat) {
done.getRpcCtx().sendResponse(response);
} else {
sendSequenceResponse(groupId, pair, reqSequence, done.getRpcCtx(), response);
}
}
return null;
} else {
return service.handleAppendEntriesRequest(request, done);
}
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
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 = done.getRpcCtx().getNodeManager().get(groupId, peer);
if (node != null) {
return processRequest0((RaftServerService) node, request, done);
} else {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr, groupId);
}
} else {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
}
}
Aggregations