Search in sources :

Example 16 with NodeOptions

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

the class ItNodeTest method testNoSnapshot.

@Test
public void testNoSnapshot() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    NodeOptions nodeOptions = createNodeOptions();
    MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
    RaftGroupService service = createService("unittest", new PeerId(addr, 0), nodeOptions);
    Node node = service.start();
    // wait node elect self as leader
    Thread.sleep(2000);
    sendTestTaskAndWait(node);
    assertEquals(0, fsm.getSaveSnapshotTimes());
    // do snapshot but returns error
    CountDownLatch latch = new CountDownLatch(1);
    node.snapshot(new ExpectClosure(RaftError.EINVAL, "Snapshot is not supported", latch));
    waitLatch(latch);
    assertEquals(0, fsm.getSaveSnapshotTimes());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 17 with NodeOptions

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

the class ItNodeTest method testSingleNode.

@Test
public void testSingleNode() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    PeerId peer = new PeerId(addr, 0);
    NodeOptions nodeOptions = createNodeOptions();
    MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    RaftGroupService service = createService("unittest", peer, nodeOptions);
    Node node = service.start();
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) ;
    sendTestTaskAndWait(node);
    assertEquals(10, fsm.getLogs().size());
    int i = 0;
    for (ByteBuffer data : fsm.getLogs()) {
        assertEquals("hello" + i++, stringFromBytes(data.array()));
    }
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 18 with NodeOptions

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

the class ItNodeTest method testRollbackStateMachineWithReadIndex_Issue317.

/**
 * Test rollback stateMachine with readIndex for issue 317: https://github.com/sofastack/sofa-jraft/issues/317
 */
@Test
public void testRollbackStateMachineWithReadIndex_Issue317() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    PeerId peer = new PeerId(addr, 0);
    NodeOptions nodeOptions = createNodeOptions();
    CountDownLatch applyCompleteLatch = new CountDownLatch(1);
    CountDownLatch applyLatch = new CountDownLatch(1);
    CountDownLatch readIndexLatch = new CountDownLatch(1);
    AtomicInteger currentValue = new AtomicInteger(-1);
    String errorMsg = testInfo.getDisplayName();
    StateMachine fsm = new StateMachineAdapter() {

        @Override
        public void onApply(Iterator iter) {
            // Notify that the #onApply is preparing to go.
            readIndexLatch.countDown();
            // Wait for submitting a read-index request
            try {
                applyLatch.await();
            } catch (InterruptedException e) {
                fail();
            }
            int i = 0;
            while (iter.hasNext()) {
                byte[] data = iter.next().array();
                int v = Bits.getInt(data, 0);
                assertEquals(i++, v);
                currentValue.set(v);
            }
            if (i > 0) {
                // rollback
                currentValue.set(i - 1);
                iter.setErrorAndRollback(1, new Status(-1, errorMsg));
                applyCompleteLatch.countDown();
            }
        }
    };
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    RaftGroupService service = createService("unittest", peer, nodeOptions);
    Node node = service.start();
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) ;
    int n = 5;
    {
        // apply tasks
        for (int i = 0; i < n; i++) {
            byte[] b = new byte[4];
            Bits.putInt(b, 0, i);
            node.apply(new Task(ByteBuffer.wrap(b), null));
        }
    }
    AtomicInteger readIndexSuccesses = new AtomicInteger(0);
    {
        // Submit a read-index, wait for #onApply
        readIndexLatch.await();
        CountDownLatch latch = new CountDownLatch(1);
        node.readIndex(null, new ReadIndexClosure() {

            @Override
            public void run(Status status, long index, byte[] reqCtx) {
                try {
                    if (status.isOk())
                        readIndexSuccesses.incrementAndGet();
                    else {
                        assertTrue(status.getErrorMsg().contains(errorMsg) || status.getRaftError() == RaftError.ETIMEDOUT || status.getErrorMsg().contains("Invalid state for readIndex: STATE_ERROR"), "Unexpected status: " + status);
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
        // We have already submit a read-index request,
        // notify #onApply can go right now
        applyLatch.countDown();
        // The state machine is in error state, the node should step down.
        waitForCondition(() -> !node.isLeader(), 5_000);
        latch.await();
        applyCompleteLatch.await();
    }
    // No read-index request succeed.
    assertEquals(0, readIndexSuccesses.get());
    assertTrue(n - 1 >= currentValue.get());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Task(org.apache.ignite.raft.jraft.entity.Task) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StateMachine(org.apache.ignite.raft.jraft.StateMachine) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(org.apache.ignite.raft.jraft.Iterator) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 19 with NodeOptions

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

the class ItNodeTest method testInitShutdown.

@Test
public void testInitShutdown() {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    NodeOptions nodeOptions = createNodeOptions();
    nodeOptions.setFsm(new MockStateMachine(addr));
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    RaftGroupService service = createService("unittest", new PeerId(addr, 0), nodeOptions);
    service.start();
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 20 with NodeOptions

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

the class ItNodeTest method testSingleNodeWithLearner.

@Test
public void testSingleNodeWithLearner() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    PeerId peer = new PeerId(addr, 0);
    Endpoint learnerAddr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + 1);
    PeerId learnerPeer = new PeerId(learnerAddr, 0);
    final int cnt = 10;
    MockStateMachine learnerFsm;
    RaftGroupService learnerServer;
    {
        // Start learner
        NodeOptions nodeOptions = createNodeOptions();
        learnerFsm = new MockStateMachine(learnerAddr);
        nodeOptions.setFsm(learnerFsm);
        nodeOptions.setLogUri(dataPath + File.separator + "log1");
        nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta1");
        nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot1");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        learnerServer = createService("unittest", new PeerId(learnerAddr, 0), nodeOptions);
        learnerServer.start();
    }
    {
        // Start leader
        NodeOptions nodeOptions = createNodeOptions();
        MockStateMachine fsm = new MockStateMachine(addr);
        nodeOptions.setFsm(fsm);
        nodeOptions.setLogUri(dataPath + File.separator + "log");
        nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
        nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        RaftGroupService server = createService("unittest", new PeerId(addr, 0), nodeOptions);
        Node node = server.start();
        assertEquals(1, node.listPeers().size());
        assertTrue(node.listPeers().contains(peer));
        assertTrue(waitForCondition(() -> node.isLeader(), 1_000));
        sendTestTaskAndWait(node, cnt);
        assertEquals(cnt, fsm.getLogs().size());
        int i = 0;
        for (ByteBuffer data : fsm.getLogs()) assertEquals("hello" + i++, stringFromBytes(data.array()));
        // wait for entries to be replicated to learner.
        Thread.sleep(1000);
        server.shutdown();
    }
    {
        // assert learner fsm
        assertEquals(cnt, learnerFsm.getLogs().size());
        int i = 0;
        for (ByteBuffer data : learnerFsm.getLogs()) assertEquals("hello" + i++, stringFromBytes(data.array()));
        learnerServer.shutdown();
    }
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)34 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)17 Test (org.junit.jupiter.api.Test)14 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)13 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 Node (org.apache.ignite.raft.jraft.Node)10 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)8 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)8 ExecutorService (java.util.concurrent.ExecutorService)5 NodeId (org.apache.ignite.raft.jraft.entity.NodeId)4 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterService (org.apache.ignite.network.ClusterService)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)3 BootstrapOptions (org.apache.ignite.raft.jraft.option.BootstrapOptions)3 File (java.io.File)2 Path (java.nio.file.Path)2