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);
}
}
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());
}
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();
}
}
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");
}
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();
}
Aggregations