Search in sources :

Example 41 with Status

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

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(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());
    assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
    closure.run(Status.OK());
    assertEquals(this.readOnlyServiceImpl.getPendingNotifyStatus().size(), 1);
    this.readOnlyServiceImpl.onApplied(2);
    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)

Example 42 with Status

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

the class ReadOnlyServiceTest method testOverMaxReadIndexLag.

@Test
public void testOverMaxReadIndexLag() throws Exception {
    Mockito.when(this.fsmCaller.getLastAppliedIndex()).thenReturn(1L);
    this.readOnlyServiceImpl.getRaftOptions().setMaxReadIndexLag(50);
    final byte[] requestContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    final String errMsg = "Fail to run ReadIndex task, the gap of current node's apply index between leader's commit index over maxReadIndexLag";
    this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertFalse(status.isOk());
            assertEquals(status.getErrorMsg(), errMsg);
            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(52).setSuccess(true).build());
    closure.run(Status.OK());
    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)

Example 43 with Status

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

the class NodeTest method testTripleNodesV1V2Codec.

@Test
public void testTripleNodesV1V2Codec() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (int i = 0; i < peers.size(); i++) {
        // Peer3 use codec v1
        if (i == 2) {
            cluster.setRaftServiceFactory(new V1JRaftServiceFactory());
        }
        assertTrue(cluster.start(peers.get(i).getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    {
        final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes());
        final Task task = new Task(data, null);
        leader.apply(task);
    }
    {
        // task with TaskClosure
        final ByteBuffer data = ByteBuffer.wrap("task closure".getBytes());
        final Vector<String> cbs = new Vector<>();
        final CountDownLatch latch = new CountDownLatch(1);
        final Task task = new Task(data, new TaskClosure() {

            @Override
            public void run(final Status status) {
                cbs.add("apply");
                latch.countDown();
            }

            @Override
            public void onCommitted() {
                cbs.add("commit");
            }
        });
        leader.apply(task);
        latch.await();
        assertEquals(2, cbs.size());
        assertEquals("commit", cbs.get(0));
        assertEquals("apply", cbs.get(1));
    }
    cluster.ensureSame(-1);
    assertEquals(2, cluster.getFollowers().size());
    // transfer the leader to v1 codec peer
    assertTrue(leader.transferLeadershipTo(peers.get(2)).isOk());
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(leader.getLeaderId(), peers.get(2));
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    cluster.stopAll();
    // start the cluster with v2 codec, should work
    final TestCluster newCluster = new TestCluster("unittest", this.dataPath, peers);
    for (int i = 0; i < peers.size(); i++) {
        assertTrue(newCluster.start(peers.get(i).getEndpoint()));
    }
    // elect leader
    newCluster.waitLeader();
    newCluster.ensureSame();
    leader = newCluster.getLeader();
    assertNotNull(leader);
    // apply new tasks
    this.sendTestTaskAndWait(leader);
    newCluster.ensureSame();
    newCluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Vector(java.util.Vector) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 44 with Status

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

the class ReplicatorTest method testOnHeartbeatReturnedRpcError.

@Test
public void testOnHeartbeatReturnedRpcError() {
    final Replicator r = getReplicator();
    this.id.unlock();
    final ScheduledFuture<?> timer = r.getHeartbeatTimer();
    assertNotNull(timer);
    Replicator.onHeartbeatReturned(this.id, new Status(-1, "test"), createEmptyEntriesRequest(), null, Utils.monotonicMs());
    assertNotNull(r.getHeartbeatTimer());
    assertNotSame(timer, r.getHeartbeatTimer());
}
Also used : Status(com.alipay.sofa.jraft.Status) Test(org.junit.Test)

Example 45 with Status

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

the class ReplicatorTest method testOnInstallSnapshotReturnedRpcError.

@Test
public void testOnInstallSnapshotReturnedRpcError() {
    final Replicator r = getReplicator();
    this.id.unlock();
    assertNull(r.getBlockTimer());
    final RpcRequests.InstallSnapshotRequest request = createInstallSnapshotRequest();
    final RpcRequests.InstallSnapshotResponse response = RpcRequests.InstallSnapshotResponse.newBuilder().setSuccess(true).setTerm(1).build();
    assertEquals(-1, r.getWaitId());
    Mockito.when(this.logManager.getTerm(11)).thenReturn(1L);
    Replicator.onRpcReturned(this.id, Replicator.RequestType.Snapshot, new Status(-1, "test"), request, response, 0, 0, -1);
    assertNotNull(r.getBlockTimer());
    assertEquals(-1, r.getWaitId());
}
Also used : Status(com.alipay.sofa.jraft.Status) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) Test(org.junit.Test)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11