Search in sources :

Example 76 with Node

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());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) RaftServerService(org.apache.ignite.raft.jraft.rpc.RaftServerService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 77 with Node

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);
        }
    }
}
Also used : WriteCommand(org.apache.ignite.raft.client.WriteCommand) Node(org.apache.ignite.raft.jraft.Node) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 78 with Node

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));
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 79 with Node

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);
    }
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) Node(org.apache.ignite.raft.jraft.Node)

Example 80 with Node

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);
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

Node (org.apache.ignite.raft.jraft.Node)84 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)75 Test (org.junit.jupiter.api.Test)66 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)38 CountDownLatch (java.util.concurrent.CountDownLatch)30 ArrayList (java.util.ArrayList)26 Status (org.apache.ignite.raft.jraft.Status)23 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)23 Task (org.apache.ignite.raft.jraft.entity.Task)18 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)17 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)16 ByteBuffer (java.nio.ByteBuffer)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)12 RaftException (org.apache.ignite.raft.jraft.error.RaftException)11 LinkedHashSet (java.util.LinkedHashSet)10 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)10 List (java.util.List)9 LogIndexOutOfBoundsException (org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException)9 LogNotFoundException (org.apache.ignite.raft.jraft.error.LogNotFoundException)9