Search in sources :

Example 1 with RpcClientEx

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

the class ItNodeTest method blockMessagesOnFollowers.

private void blockMessagesOnFollowers(List<Node> followers, BiPredicate<Object, String> blockingPredicate) {
    for (Node follower : followers) {
        RpcClientEx rpcClientEx = sender(follower);
        rpcClientEx.blockMessages(blockingPredicate);
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) RpcClientEx(org.apache.ignite.raft.jraft.rpc.RpcClientEx)

Example 2 with RpcClientEx

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

the class ItNodeTest method testLeaseReadAfterSegmentation.

/**
 * Tests if a read using leader leases works correctly after previous leader segmentation.
 */
@Test
public void testLeaseReadAfterSegmentation() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unittest", dataPath, peers, 3_000, testInfo);
    for (PeerId peer : peers) {
        RaftOptions opts = new RaftOptions();
        // Election timeout divisor.
        opts.setElectionHeartbeatFactor(2);
        opts.setReadOnlyOptions(ReadOnlyOption.ReadOnlyLeaseBased);
        assertTrue(cluster.start(peer.getEndpoint(), false, 300, false, null, opts));
    }
    cluster.waitLeader();
    NodeImpl leader = (NodeImpl) cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    sendTestTaskAndWait(leader);
    cluster.ensureSame();
    DefaultRaftClientService rpcService = (DefaultRaftClientService) leader.getRpcClientService();
    RpcClientEx rpcClientEx = (RpcClientEx) rpcService.getRpcClient();
    AtomicInteger cnt = new AtomicInteger();
    rpcClientEx.blockMessages((msg, nodeId) -> {
        assertTrue(msg instanceof RpcRequests.AppendEntriesRequest);
        if (cnt.get() >= 2)
            return true;
        LOG.info("Send heartbeat: " + msg + " to " + nodeId);
        cnt.incrementAndGet();
        return false;
    });
    assertTrue(waitForCondition(() -> cluster.getLeader() != null && !leader.getNodeId().equals(cluster.getLeader().getNodeId()), 10_000));
    CompletableFuture<Status> res = new CompletableFuture<>();
    cluster.getLeader().readIndex(null, new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            res.complete(status);
        }
    });
    assertTrue(res.get().isOk());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) CompletableFuture(java.util.concurrent.CompletableFuture) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) RpcClientEx(org.apache.ignite.raft.jraft.rpc.RpcClientEx) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 3 with RpcClientEx

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

the class ItNodeTest method stopBlockingMessagesOnFollowers.

private void stopBlockingMessagesOnFollowers(List<Node> followers) {
    for (Node follower : followers) {
        RpcClientEx rpcClientEx = sender(follower);
        rpcClientEx.stopBlock();
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) RpcClientEx(org.apache.ignite.raft.jraft.rpc.RpcClientEx)

Example 4 with RpcClientEx

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

the class ItNodeTest method testLeaderPropagatedBeforeVote.

@Test
public void testLeaderPropagatedBeforeVote() throws Exception {
    // start five nodes
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, 3_000, testInfo);
    for (PeerId peer : peers) {
        RaftOptions opts = new RaftOptions();
        // Election timeout divisor.
        opts.setElectionHeartbeatFactor(4);
        assertTrue(cluster.start(peer.getEndpoint(), false, 300, false, null, opts));
    }
    List<NodeImpl> nodes = cluster.getNodes();
    AtomicReference<String> guard = new AtomicReference();
    // Block only one vote message.
    for (NodeImpl node : nodes) {
        RpcClientEx rpcClientEx = sender(node);
        rpcClientEx.recordMessages((msg, nodeId) -> true);
        rpcClientEx.blockMessages((msg, nodeId) -> {
            if (msg instanceof RpcRequests.RequestVoteRequest) {
                RpcRequests.RequestVoteRequest msg0 = (RpcRequests.RequestVoteRequest) msg;
                if (msg0.preVote())
                    return false;
                if (guard.compareAndSet(null, nodeId))
                    return true;
            }
            if (msg instanceof RpcRequests.AppendEntriesRequest && nodeId.equals(guard.get())) {
                RpcRequests.AppendEntriesRequest tmp = (RpcRequests.AppendEntriesRequest) msg;
                if (tmp.entriesList() != null && !tmp.entriesList().isEmpty()) {
                    return true;
                }
            }
            return false;
        });
    }
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    cluster.ensureLeader(leader);
    RpcClientEx client = sender(leader);
    // Unblock vote message.
    client.stopBlock(1);
    // The follower shouldn't stop following on receiving stale vote request.
    Node follower = cluster.getNode(new Endpoint(NetworkAddress.from(guard.get())));
    boolean res = waitForCondition(() -> ((MockStateMachine) follower.getOptions().getFsm()).getOnStopFollowingTimes() != 0, 1_000);
    assertFalse(res, "The follower shouldn't stop following");
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) Node(org.apache.ignite.raft.jraft.Node) AtomicReference(java.util.concurrent.atomic.AtomicReference) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RpcClientEx(org.apache.ignite.raft.jraft.rpc.RpcClientEx) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 5 with RpcClientEx

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

the class TestUtils method sender.

/**
 * Returns a message sender for this node.
 *
 * @param node The node.
 * @return The message sender.
 */
public static RpcClientEx sender(Node node) {
    NodeImpl node0 = (NodeImpl) node;
    DefaultRaftClientService rpcService = (DefaultRaftClientService) node0.getRpcClientService();
    return (RpcClientEx) rpcService.getRpcClient();
}
Also used : NodeImpl(org.apache.ignite.raft.jraft.core.NodeImpl) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) RpcClientEx(org.apache.ignite.raft.jraft.rpc.RpcClientEx)

Aggregations

RpcClientEx (org.apache.ignite.raft.jraft.rpc.RpcClientEx)5 Node (org.apache.ignite.raft.jraft.Node)3 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)2 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)2 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)2 DefaultRaftClientService (org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService)2 Test (org.junit.jupiter.api.Test)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Status (org.apache.ignite.raft.jraft.Status)1 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)1 NodeImpl (org.apache.ignite.raft.jraft.core.NodeImpl)1 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)1