Search in sources :

Example 16 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class BaseCliRequestProcessorTest method testOK.

@Test
public void testOK() {
    Node node = mockNode(false);
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(this.processor.done);
    assertSame(this.processor.ctx.node, node);
    assertNotNull(resp);
    assertEquals(0, resp.errorCode());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 17 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class BaseCliRequestProcessorTest method testManyNodes.

@Test
public void testManyNodes() {
    Node node1 = Mockito.mock(Node.class);
    Mockito.when(node1.getGroupId()).thenReturn("test");
    Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    this.asyncContext.getNodeManager().add(node1);
    Node node2 = Mockito.mock(Node.class);
    Mockito.when(node2.getGroupId()).thenReturn("test");
    Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
    this.asyncContext.getNodeManager().add(node2);
    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.errorCode());
    assertEquals("Peer must be specified since there're 2 nodes in group test", resp.errorMsg());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 18 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class Replicator method notifyReplicatorStatusListener.

/**
 * Notify replicator event(such as created, error, destroyed) to replicatorStateListener which is implemented by
 * users.
 *
 * @param replicator replicator object
 * @param event replicator's state listener event type
 * @param status replicator's error detailed status
 */
private static void notifyReplicatorStatusListener(final Replicator replicator, final ReplicatorEvent event, final Status status) {
    final ReplicatorOptions replicatorOpts = Requires.requireNonNull(replicator.getOpts(), "replicatorOptions");
    final Node node = Requires.requireNonNull(replicatorOpts.getNode(), "node");
    final PeerId peer = Requires.requireNonNull(replicatorOpts.getPeerId(), "peer");
    final List<ReplicatorStateListener> listenerList = node.getReplicatorStateListeners();
    for (int i = 0; i < listenerList.size(); i++) {
        final ReplicatorStateListener listener = listenerList.get(i);
        if (listener != null) {
            try {
                switch(event) {
                    case CREATED:
                        Utils.runInThread(replicatorOpts.getCommonExecutor(), () -> listener.onCreated(peer));
                        break;
                    case ERROR:
                        Utils.runInThread(replicatorOpts.getCommonExecutor(), () -> listener.onError(peer, status));
                        break;
                    case DESTROYED:
                        Utils.runInThread(replicatorOpts.getCommonExecutor(), () -> listener.onDestroyed(peer));
                        break;
                    default:
                        break;
                }
            } catch (final Exception e) {
                LOG.error("Fail to notify ReplicatorStatusListener, listener={}, event={}.", listener, event);
            }
        }
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) ReplicatorOptions(org.apache.ignite.raft.jraft.option.ReplicatorOptions) RaftException(org.apache.ignite.raft.jraft.error.RaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 19 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.

PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, NodeManager nodeManager) {
    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.get(groupId, peer);
                assert (node != null);
                peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
                peerCtx.executor = node.getOptions().getStripedExecutor().next();
                groupContexts.put(pair, peerCtx);
            }
        }
    }
    return peerCtx;
}
Also used : Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 20 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

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.peerId() != null) {
        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, done.getRpcCtx().getNodeManager()));
            if (!st.isOk()) {
                return // 
                RaftRpcFactory.DEFAULT.newResponse(msgFactory(), st);
            }
        } else {
            return // 
            RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    } else {
        nodes = done.getRpcCtx().getNodeManager().getNodesByGroupId(groupId);
    }
    if (nodes == null || nodes.isEmpty()) {
        return // 
        RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.ENOENT, "No nodes in group %s", groupId);
    }
    for (final Node node : nodes) {
        final PeerId leader = node.getLeaderId();
        if (leader != null && !leader.isEmpty()) {
            return msgFactory().getLeaderResponse().leaderId(leader.toString()).currentTerm(node.getCurrentTerm()).build();
        }
    }
    return // 
    RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EAGAIN, "Unknown leader");
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) 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