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);
}
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();
}
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());
}
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();
}
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));
}
Aggregations