Search in sources :

Example 11 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class NodeTest 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 {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final CountDownLatch applyCompleteLatch = new CountDownLatch(1);
    final CountDownLatch applyLatch = new CountDownLatch(1);
    final CountDownLatch readIndexLatch = new CountDownLatch(1);
    final AtomicInteger currentValue = new AtomicInteger(-1);
    final String errorMsg = this.testName.getMethodName();
    final StateMachine fsm = new StateMachineAdapter() {

        @Override
        public void onApply(final 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(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    final Node node = new NodeImpl("unittest", peer);
    assertTrue(node.init(nodeOptions));
    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));
        }
    }
    final AtomicInteger readIndexSuccesses = new AtomicInteger(0);
    {
        // Submit a read-index, wait for #onApply
        readIndexLatch.await();
        final CountDownLatch latch = new CountDownLatch(1);
        node.readIndex(null, new ReadIndexClosure() {

            @Override
            public void run(final Status status, final long index, final byte[] reqCtx) {
                try {
                    if (status.isOk()) {
                        readIndexSuccesses.incrementAndGet();
                    } else {
                        assertTrue("Unexpected status: " + status, status.getErrorMsg().contains(errorMsg) || status.getRaftError() == RaftError.ETIMEDOUT || status.getErrorMsg().contains("Invalid state for readIndex: STATE_ERROR"));
                    }
                } 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.
        while (node.isLeader()) {
            Thread.sleep(10);
        }
        latch.await();
        applyCompleteLatch.await();
    }
    // No read-index request succeed.
    assertEquals(0, readIndexSuccesses.get());
    assertTrue(n - 1 >= currentValue.get());
    node.shutdown();
    node.join();
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) StateMachine(com.alipay.sofa.jraft.StateMachine) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(com.alipay.sofa.jraft.Iterator) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 12 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class NodeTest method testReadIndexTimeout.

@Test
public void testReadIndexTimeout() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), false, 300, true));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    // first call will fail-fast when no connection
    if (!assertReadIndex(leader, 11)) {
        assertTrue(assertReadIndex(leader, 11));
    }
    // read from follower
    for (final Node follower : cluster.getFollowers()) {
        assertNotNull(follower);
        assertReadIndex(follower, 11);
    }
    // read with null request context
    final CountDownLatch latch = new CountDownLatch(1);
    final long start = System.currentTimeMillis();
    leader.readIndex(null, new ReadIndexClosure(0) {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertNull(reqCtx);
            if (status.isOk()) {
                System.err.println("Read-index so fast: " + (System.currentTimeMillis() - start) + "ms");
            } else {
                assertEquals(status, new Status(RaftError.ETIMEDOUT, "read-index request timeout"));
                assertEquals(index, -1);
            }
            latch.countDown();
        }
    });
    latch.await();
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 13 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class NodeTest method assertReadIndex.

@SuppressWarnings({ "unused", "SameParameterValue" })
private boolean assertReadIndex(final Node node, final int index) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final byte[] requestContext = TestUtils.getRandomBytes();
    final AtomicBoolean success = new AtomicBoolean(false);
    node.readIndex(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long theIndex, final byte[] reqCtx) {
            if (status.isOk()) {
                assertEquals(index, theIndex);
                assertArrayEquals(requestContext, reqCtx);
                success.set(true);
            } else {
                assertTrue(status.getErrorMsg(), status.getErrorMsg().contains("RPC exception:Check connection["));
                assertTrue(status.getErrorMsg(), status.getErrorMsg().contains("] fail and try to create new one"));
            }
            latch.countDown();
        }
    });
    latch.await();
    return success.get();
}
Also used : Status(com.alipay.sofa.jraft.Status) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

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(final Object argument) {
            if (argument instanceof ReadIndexRequest) {
                final ReadIndexRequest req = (ReadIndexRequest) argument;
                return req.getGroupId().equals("test") && req.getServerId().equals("localhost:8081:0") && req.getEntriesCount() == 1 && Arrays.equals(requestContext, req.getEntries(0).toByteArray());
            }
            return false;
        }
    }), Mockito.any());
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ReadIndexRequest(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ArgumentMatcher(org.mockito.ArgumentMatcher) Test(org.junit.Test)

Example 15 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class ReadOnlyServiceTest method testAddRequestOnResponseFailure.

@Test
public void testAddRequestOnResponseFailure() throws Exception {
    Mockito.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(final Object argument) {
            if (argument instanceof ReadIndexRequest) {
                final ReadIndexRequest req = (ReadIndexRequest) argument;
                return req.getGroupId().equals("test") && req.getServerId().equals("localhost:8081:0") && req.getEntriesCount() == 1 && Arrays.equals(requestContext, req.getEntries(0).toByteArray());
            }
            return false;
        }
    }), closureCaptor.capture());
    final RpcResponseClosure closure = closureCaptor.getValue();
    assertNotNull(closure);
    closure.setResponse(ReadIndexResponse.newBuilder().setIndex(1).setSuccess(true).build());
    closure.run(new Status(-1, "test"));
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ReadIndexRequest(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ArgumentMatcher(org.mockito.ArgumentMatcher) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)20 Status (com.alipay.sofa.jraft.Status)17 CountDownLatch (java.util.concurrent.CountDownLatch)11 Test (org.junit.Test)10 ReadIndexStatus (com.alipay.sofa.jraft.entity.ReadIndexStatus)7 ReadIndexRequest (com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest)6 Node (com.alipay.sofa.jraft.Node)5 ArgumentMatcher (org.mockito.ArgumentMatcher)5 PeerId (com.alipay.sofa.jraft.entity.PeerId)4 ReadIndexState (com.alipay.sofa.jraft.entity.ReadIndexState)4 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)4 Bytes (com.alipay.sofa.jraft.util.Bytes)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)1 Response (com.alibaba.nacos.consistency.entity.Response)1 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 FSMCaller (com.alipay.sofa.jraft.FSMCaller)1 LastAppliedLogIndexListener (com.alipay.sofa.jraft.FSMCaller.LastAppliedLogIndexListener)1