Search in sources :

Example 1 with Iterator

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

the class FSMCallerTest method testOnCommitted.

@Test
public void testOnCommitted() throws Exception {
    final LogEntry log = new LogEntry(EntryType.ENTRY_TYPE_DATA);
    log.getId().setIndex(11);
    log.getId().setTerm(1);
    Mockito.when(this.logManager.getTerm(11)).thenReturn(1L);
    Mockito.when(this.logManager.getEntry(11)).thenReturn(log);
    final ArgumentCaptor<Iterator> itArg = ArgumentCaptor.forClass(Iterator.class);
    assertTrue(this.fsmCaller.onCommitted(11));
    this.fsmCaller.flush();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 11);
    Mockito.verify(this.fsm).onApply(itArg.capture());
    final Iterator it = itArg.getValue();
    assertFalse(it.hasNext());
    assertEquals(it.getIndex(), 12);
    Mockito.verify(this.logManager).setAppliedId(new LogId(11, 1));
    assertTrue(this.fsmCaller.getError().getStatus().isOk());
}
Also used : Iterator(org.apache.ignite.raft.jraft.Iterator) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) Test(org.junit.jupiter.api.Test)

Example 2 with Iterator

use of org.apache.ignite.raft.jraft.Iterator 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)

Aggregations

Iterator (org.apache.ignite.raft.jraft.Iterator)2 Test (org.junit.jupiter.api.Test)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Node (org.apache.ignite.raft.jraft.Node)1 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)1 StateMachine (org.apache.ignite.raft.jraft.StateMachine)1 Status (org.apache.ignite.raft.jraft.Status)1 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)1 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)1 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)1 LogId (org.apache.ignite.raft.jraft.entity.LogId)1 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)1 Task (org.apache.ignite.raft.jraft.entity.Task)1 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)1 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)1