Search in sources :

Example 11 with RaftOptions

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));
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ExecutorService(java.util.concurrent.ExecutorService) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with RaftOptions

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());
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) Node(org.apache.ignite.raft.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 13 with RaftOptions

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());
}
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 14 with RaftOptions

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");
}
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 15 with RaftOptions

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));
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) NodeMetrics(org.apache.ignite.raft.jraft.core.NodeMetrics) LogManagerOptions(org.apache.ignite.raft.jraft.option.LogManagerOptions) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)23 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Test (org.junit.jupiter.api.Test)11 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)8 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)8 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)6 LocalFileMetaOutter (org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter)4 File (java.io.File)3 ByteBuffer (java.nio.ByteBuffer)3 TimerManager (org.apache.ignite.raft.jraft.core.TimerManager)3 CopyOptions (org.apache.ignite.raft.jraft.option.CopyOptions)3 SnapshotCopierOptions (org.apache.ignite.raft.jraft.option.SnapshotCopierOptions)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Node (org.apache.ignite.raft.jraft.Node)2 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)2 RpcClientEx (org.apache.ignite.raft.jraft.rpc.RpcClientEx)2