Search in sources :

Example 6 with ReadIndexClosure

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

the class ReadOnlyServiceTest method testAddRequestOnResponsePending.

@Test
public void testAddRequestOnResponsePending() throws Exception {
    final byte[] requestContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertTrue(status.isOk());
            assertEquals(index, 1);
            assertArrayEquals(reqCtx, requestContext);
            latch.countDown();
        }
    });
    this.readOnlyServiceImpl.flush();
    final ArgumentCaptor<RpcResponseClosure> closureCaptor = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.verify(this.node).handleReadIndexRequest(Mockito.argThat(new ArgumentMatcher<ReadIndexRequest>() {

        @Override
        public boolean matches(ReadIndexRequest argument) {
            if (argument != null) {
                final ReadIndexRequest req = (ReadIndexRequest) argument;
                return "test".equals(req.groupId()) && "localhost:8081:0".equals(req.serverId()) && Utils.size(req.entriesList()) == 1 && Arrays.equals(requestContext, req.entriesList().get(0).toByteArray());
            }
            return false;
        }
    }), closureCaptor.capture());
    final RpcResponseClosure closure = closureCaptor.getValue();
    assertNotNull(closure);
    closure.setResponse(msgFactory.readIndexResponse().index(1).success(true).build());
    assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
    closure.run(Status.OK());
    assertEquals(this.readOnlyServiceImpl.getPendingNotifyStatus().size(), 1);
    this.readOnlyServiceImpl.onApplied(2);
    latch.await();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ArgumentMatcher(org.mockito.ArgumentMatcher) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with ReadIndexClosure

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

the class ReadOnlyServiceTest method testAddRequest.

@Test
public void testAddRequest() throws Exception {
    final byte[] requestContext = TestUtils.getRandomBytes();
    this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
        }
    });
    this.readOnlyServiceImpl.flush();
    Mockito.verify(this.node).handleReadIndexRequest(Mockito.argThat(new ArgumentMatcher<ReadIndexRequest>() {

        @Override
        public boolean matches(ReadIndexRequest argument) {
            if (argument != null) {
                final ReadIndexRequest req = (ReadIndexRequest) argument;
                return "test".equals(req.groupId()) && "localhost:8081:0".equals(req.serverId()) && Utils.size(req.entriesList()) == 1 && Arrays.equals(requestContext, req.entriesList().get(0).toByteArray());
            }
            return false;
        }
    }), Mockito.any());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ArgumentMatcher(org.mockito.ArgumentMatcher) Test(org.junit.jupiter.api.Test)

Example 8 with ReadIndexClosure

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

the class ReadOnlyServiceTest method testAddRequestOnResponseFailure.

@Test
public void testAddRequestOnResponseFailure() throws Exception {
    Mockito.lenient().when(this.fsmCaller.getLastAppliedIndex()).thenReturn(2L);
    final byte[] requestContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertFalse(status.isOk());
            assertEquals(index, -1);
            assertArrayEquals(reqCtx, requestContext);
            latch.countDown();
        }
    });
    this.readOnlyServiceImpl.flush();
    final ArgumentCaptor<RpcResponseClosure> closureCaptor = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.verify(this.node).handleReadIndexRequest(Mockito.argThat(new ArgumentMatcher<ReadIndexRequest>() {

        @Override
        public boolean matches(ReadIndexRequest argument) {
            if (argument != null) {
                final ReadIndexRequest req = (ReadIndexRequest) argument;
                return "test".equals(req.groupId()) && "localhost:8081:0".equals(req.serverId()) && Utils.size(req.entriesList()) == 1 && Arrays.equals(requestContext, req.entriesList().get(0).toByteArray());
            }
            return false;
        }
    }), closureCaptor.capture());
    final RpcResponseClosure closure = closureCaptor.getValue();
    assertNotNull(closure);
    closure.setResponse(msgFactory.readIndexResponse().index(1).success(true).build());
    closure.run(new Status(-1, "test"));
    latch.await();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ArgumentMatcher(org.mockito.ArgumentMatcher) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 9 with ReadIndexClosure

use of org.apache.ignite.raft.jraft.closure.ReadIndexClosure 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 10 with ReadIndexClosure

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

the class ItNodeTest method testReadIndexChaos.

@Test
public void testReadIndexChaos() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unittest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), false, 300, true));
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    CountDownLatch latch = new CountDownLatch(10);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    executors.add(executor);
    for (int i = 0; i < 10; i++) {
        executor.submit(new Runnable() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 100; i++) {
                        try {
                            sendTestTaskAndWait(leader);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                        readIndexRandom(cluster);
                    }
                } finally {
                    latch.countDown();
                }
            }

            private void readIndexRandom(TestCluster cluster) {
                CountDownLatch readLatch = new CountDownLatch(1);
                byte[] requestContext = TestUtils.getRandomBytes();
                cluster.getNodes().get(ThreadLocalRandom.current().nextInt(3)).readIndex(requestContext, new ReadIndexClosure() {

                    @Override
                    public void run(Status status, long index, byte[] reqCtx) {
                        if (status.isOk()) {
                            assertTrue(status.isOk(), status.toString());
                            assertTrue(index > 0);
                            assertArrayEquals(requestContext, reqCtx);
                        }
                        readLatch.countDown();
                    }
                });
                try {
                    readLatch.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
    }
    latch.await();
    cluster.ensureSame();
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(10000, fsm.getLogs().size());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Node(org.apache.ignite.raft.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ExecutorService(java.util.concurrent.ExecutorService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)14 Status (org.apache.ignite.raft.jraft.Status)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.jupiter.api.Test)10 ReadIndexStatus (org.apache.ignite.raft.jraft.entity.ReadIndexStatus)6 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)5 ReadIndexRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest)5 Node (org.apache.ignite.raft.jraft.Node)4 ReadIndexState (org.apache.ignite.raft.jraft.entity.ReadIndexState)4 ArgumentMatcher (org.mockito.ArgumentMatcher)4 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)2 Bytes (org.apache.ignite.raft.jraft.util.Bytes)2 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)2 EventFactory (com.lmax.disruptor.EventFactory)1 EventHandler (com.lmax.disruptor.EventHandler)1 EventTranslator (com.lmax.disruptor.EventTranslator)1 RingBuffer (com.lmax.disruptor.RingBuffer)1