Search in sources :

Example 1 with ReadIndexRequest

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest in project ignite-3 by apache.

the class NodeImpl method readFollower.

private void readFollower(final ReadIndexRequest request, final RpcResponseClosure<ReadIndexResponse> closure) {
    if (this.leaderId == null || this.leaderId.isEmpty()) {
        closure.run(new Status(RaftError.EPERM, "No leader at term %d.", this.currTerm));
        return;
    }
    // send request to leader.
    final ReadIndexRequest newRequest = raftOptions.getRaftMessagesFactory().readIndexRequest().groupId(request.groupId()).serverId(request.serverId()).peerId(request.peerId()).entriesList(request.entriesList()).peerId(this.leaderId.toString()).build();
    this.rpcClientService.readIndex(this.leaderId.getEndpoint(), newRequest, -1, closure);
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest)

Example 2 with ReadIndexRequest

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest 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 3 with ReadIndexRequest

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest 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 4 with ReadIndexRequest

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest 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 5 with ReadIndexRequest

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest in project ignite-3 by apache.

the class ReadOnlyServiceImpl method executeReadIndexEvents.

private void executeReadIndexEvents(final List<ReadIndexEvent> events) {
    if (events.isEmpty())
        return;
    ReadIndexRequestBuilder rb = raftOptions.getRaftMessagesFactory().readIndexRequest().groupId(this.node.getGroupId()).serverId(this.node.getServerId().toString());
    List<ReadIndexState> states = new ArrayList<>(events.size());
    List<ByteString> entries = new ArrayList<>(events.size());
    for (ReadIndexEvent event : events) {
        byte[] ctx = event.requestContext.get();
        entries.add(ctx == null ? ByteString.EMPTY : new ByteString(ctx));
        states.add(new ReadIndexState(event.requestContext, event.done, event.startTime));
    }
    ReadIndexRequest request = rb.entriesList(entries).build();
    this.node.handleReadIndexRequest(request, new ReadIndexResponseClosure(states, request));
}
Also used : ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) ByteString(org.apache.ignite.raft.jraft.util.ByteString) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState) ReadIndexRequestBuilder(org.apache.ignite.raft.jraft.rpc.ReadIndexRequestBuilder) ArrayList(java.util.ArrayList)

Aggregations

ReadIndexRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest)6 Status (org.apache.ignite.raft.jraft.Status)5 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)4 ReadIndexStatus (org.apache.ignite.raft.jraft.entity.ReadIndexStatus)4 Test (org.junit.jupiter.api.Test)4 ArgumentMatcher (org.mockito.ArgumentMatcher)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)3 ArrayList (java.util.ArrayList)1 ReadIndexState (org.apache.ignite.raft.jraft.entity.ReadIndexState)1 ReadIndexRequestBuilder (org.apache.ignite.raft.jraft.rpc.ReadIndexRequestBuilder)1 ByteString (org.apache.ignite.raft.jraft.util.ByteString)1