use of org.apache.ignite.raft.jraft.option.RaftOptions in project ignite-3 by apache.
the class ReadOnlyServiceTest method setup.
@BeforeEach
public void setup() {
this.readOnlyServiceImpl = new ReadOnlyServiceImpl();
RaftOptions raftOptions = new RaftOptions();
this.msgFactory = raftOptions.getRaftMessagesFactory();
final ReadOnlyServiceOptions opts = new ReadOnlyServiceOptions();
opts.setFsmCaller(this.fsmCaller);
opts.setNode(this.node);
opts.setRaftOptions(raftOptions);
opts.setGroupId("TestSrv");
opts.setReadOnlyServiceDisruptor(disruptor = new StripedDisruptor<>("TestReadOnlyServiceDisruptor", 1024, () -> new ReadOnlyServiceImpl.ReadIndexEvent(), 1));
NodeOptions nodeOptions = new NodeOptions();
ExecutorService executor = JRaftUtils.createExecutor("test-executor", Utils.cpus());
executors.add(executor);
nodeOptions.setCommonExecutor(executor);
ExecutorService clientExecutor = JRaftUtils.createClientExecutor(nodeOptions, "unittest");
executors.add(clientExecutor);
nodeOptions.setClientExecutor(clientExecutor);
Scheduler scheduler = JRaftUtils.createScheduler(nodeOptions);
this.scheduler = scheduler;
nodeOptions.setScheduler(scheduler);
Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
Mockito.when(this.node.getGroupId()).thenReturn("test");
Mockito.when(this.node.getOptions()).thenReturn(nodeOptions);
Mockito.when(this.node.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost:8081", 0)));
Mockito.when(this.node.getServerId()).thenReturn(new PeerId("localhost:8081", 0));
assertTrue(this.readOnlyServiceImpl.init(opts));
}
use of org.apache.ignite.raft.jraft.option.RaftOptions in project ignite-3 by apache.
the class ItNodeTest method testInstallLargeSnapshot.
@Test
public void testInstallLargeSnapshot() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(4);
cluster = new TestCluster("unitest", dataPath, peers.subList(0, 3), testInfo);
for (int i = 0; i < peers.size() - 1; i++) {
PeerId peer = peers.get(i);
boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
assertTrue(started);
}
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
cluster.ensureLeader(leader);
// apply tasks to leader
sendTestTaskAndWait(leader, 0, RaftError.SUCCESS);
cluster.ensureSame();
// apply something more
for (int i = 1; i < 100; i++) sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
Thread.sleep(1000);
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader);
// apply something more
for (int i = 100; i < 200; i++) sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader, 2);
// wait leader to compact logs
Thread.sleep(1000);
// add follower
PeerId newPeer = peers.get(3);
RaftOptions raftOptions = new RaftOptions();
raftOptions.setMaxByteCountPerRpc(128);
boolean started = cluster.start(newPeer.getEndpoint(), false, 300, false, null, raftOptions);
assertTrue(started);
CountDownLatch latch = new CountDownLatch(1);
leader.addPeer(newPeer, status -> {
assertTrue(status.isOk(), status.toString());
latch.countDown();
});
waitLatch(latch);
cluster.ensureSame();
assertEquals(4, cluster.getFsms().size());
for (MockStateMachine fsm : cluster.getFsms()) assertEquals(2000, fsm.getLogs().size());
}
use of org.apache.ignite.raft.jraft.option.RaftOptions 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.option.RaftOptions 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.option.RaftOptions in project ignite-3 by apache.
the class LogManagerTest method setup.
@BeforeEach
public void setup() throws Exception {
this.confManager = new ConfigurationManager();
this.raftOptions = new RaftOptions();
this.logStorage = newLogStorage(raftOptions);
this.logManager = new LogManagerImpl();
final LogManagerOptions opts = new LogManagerOptions();
NodeOptions nodeOptions = new NodeOptions();
executor = JRaftUtils.createExecutor("test-executor", Utils.cpus());
nodeOptions.setCommonExecutor(executor);
Mockito.when(node.getOptions()).thenReturn(nodeOptions);
opts.setConfigurationManager(this.confManager);
opts.setLogEntryCodecFactory(LogEntryV1CodecFactory.getInstance());
opts.setFsmCaller(this.fsmCaller);
opts.setNode(node);
opts.setNodeMetrics(new NodeMetrics(false));
opts.setLogStorage(this.logStorage);
opts.setRaftOptions(raftOptions);
opts.setGroupId("TestSrv");
opts.setLogManagerDisruptor(disruptor = new StripedDisruptor<>("TestLogManagerDisruptor", 1024, () -> new LogManagerImpl.StableClosureEvent(), 1));
assertTrue(this.logManager.init(opts));
}
Aggregations