Search in sources :

Example 1 with ReadIndexRequest

use of com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest in project sofa-jraft by sofastack.

the class ReadOnlyServiceTest method testAddRequestOnResponseSuccess.

@Test
public void testAddRequestOnResponseSuccess() 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) {
            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());
    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 2 with ReadIndexRequest

use of com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest 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 3 with ReadIndexRequest

use of com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest 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 4 with ReadIndexRequest

use of com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest 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 5 with ReadIndexRequest

use of com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest 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

ReadIndexRequest (com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest)7 Status (com.alipay.sofa.jraft.Status)6 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)5 ReadIndexStatus (com.alipay.sofa.jraft.entity.ReadIndexStatus)5 Test (org.junit.Test)5 ArgumentMatcher (org.mockito.ArgumentMatcher)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ReadIndexState (com.alipay.sofa.jraft.entity.ReadIndexState)1 ArrayList (java.util.ArrayList)1