use of org.apache.ignite.raft.jraft.rpc.Message in project ignite-3 by apache.
the class AppendEntriesRequestProcessor method processRequest0.
@Override
public Message processRequest0(final RaftServerService service, final AppendEntriesRequest request, final RpcRequestClosure done) {
final Node node = (Node) service;
if (node.getRaftOptions().isReplicatorPipeline()) {
final String groupId = request.groupId();
final PeerPair pair = pairOf(request.peerId(), request.serverId());
boolean isHeartbeat = isHeartbeatRequest(request);
int reqSequence = -1;
if (!isHeartbeat) {
reqSequence = getAndIncrementSequence(groupId, pair, done.getRpcCtx().getNodeManager());
}
final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, groupId, pair, reqSequence, isHeartbeat));
if (response != null) {
// heartbeat or probe request
if (isHeartbeat) {
done.getRpcCtx().sendResponse(response);
} else {
sendSequenceResponse(groupId, pair, reqSequence, done.getRpcCtx(), response);
}
}
return null;
} else {
return service.handleAppendEntriesRequest(request, done);
}
}
use of org.apache.ignite.raft.jraft.rpc.Message in project ignite-3 by apache.
the class DefaultRaftClientService method failedFuture.
/**
* @param executor The executor to run done closure.
* @param request The request.
* @param done The closure.
* @param endpoint The endpoint.
* @return The future.
*/
private Future<Message> failedFuture(Executor executor, Message request, RpcResponseClosure<?> done, Endpoint endpoint) {
// fail-fast when no connection
final CompletableFuture<Message> future = new CompletableFuture<>();
executor.execute(() -> {
if (done != null) {
try {
done.run(new Status(RaftError.EINTERNAL, "Check connection[%s] fail and try to create new one", endpoint));
} catch (final Throwable t) {
LOG.error("Fail to run RpcResponseClosure, the request is {}.", t, request);
}
}
future.completeExceptionally(new RemotingException("Check connection[" + endpoint.toString() + "] fail and try to create new one"));
});
return future;
}
use of org.apache.ignite.raft.jraft.rpc.Message in project ignite-3 by apache.
the class ReplicatorTest method testInstallSnapshot.
@Test
public void testInstallSnapshot() {
final Replicator r = getReplicator();
this.id.unlock();
final Future<Message> rpcInFly = r.getRpcInFly();
assertNotNull(rpcInFly);
final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
Mockito.when(this.snapshotStorage.open()).thenReturn(reader);
final String uri = "remote://localhost:8081/99";
Mockito.when(reader.generateURIForCopy()).thenReturn(uri);
final RaftOutter.SnapshotMeta meta = raftOptions.getRaftMessagesFactory().snapshotMeta().lastIncludedIndex(11).lastIncludedTerm(1).build();
Mockito.when(reader.load()).thenReturn(meta);
assertEquals(0, r.statInfo.lastLogIncluded);
assertEquals(0, r.statInfo.lastTermIncluded);
final RpcRequests.InstallSnapshotRequest req = raftOptions.getRaftMessagesFactory().installSnapshotRequest().term(this.opts.getTerm()).groupId(this.opts.getGroupId()).serverId(this.opts.getServerId().toString()).peerId(this.opts.getPeerId().toString()).meta(meta).uri(uri).build();
Mockito.when(this.rpcService.installSnapshot(eq(this.opts.getPeerId().getEndpoint()), eq(req), Mockito.any())).thenReturn(new CompletableFuture<>());
r.installSnapshot();
assertNotNull(r.getRpcInFly());
assertNotSame(r.getRpcInFly(), rpcInFly);
assertEquals(Replicator.RunningState.INSTALLING_SNAPSHOT, r.statInfo.runningState);
assertEquals(11, r.statInfo.lastLogIncluded);
assertEquals(1, r.statInfo.lastTermIncluded);
}
use of org.apache.ignite.raft.jraft.rpc.Message in project ignite-3 by apache.
the class ReplicatorTest method testContinueSendingEntries.
@Test
public void testContinueSendingEntries() throws Exception {
testOnRpcReturnedWaitMoreEntries();
final Replicator r = getReplicator();
this.id.unlock();
mockSendEmptyEntries();
final Future<Message> rpcInFly = r.getRpcInFly();
assertNotNull(rpcInFly);
final AppendEntriesRequestBuilder rb = raftOptions.getRaftMessagesFactory().appendEntriesRequest().groupId("test").serverId(new PeerId("localhost", 8082).toString()).peerId(this.peerId.toString()).term(1).prevLogIndex(10).prevLogTerm(1).committedIndex(0);
int totalDataLen = 0;
List<RaftOutter.EntryMeta> entries = new ArrayList<>();
for (int i = 0; i < 10; i++) {
totalDataLen += i;
final LogEntry value = new LogEntry();
value.setData(ByteBuffer.allocate(i));
value.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
value.setId(new LogId(11 + i, 1));
Mockito.when(this.logManager.getEntry(11 + i)).thenReturn(value);
entries.add(raftOptions.getRaftMessagesFactory().entryMeta().term(1).type(EnumOutter.EntryType.ENTRY_TYPE_DATA).dataLen(i).build());
}
rb.entriesList(entries);
rb.data(new ByteString(new byte[totalDataLen]));
final RpcRequests.AppendEntriesRequest request = rb.build();
Mockito.when(this.rpcService.appendEntries(eq(this.peerId.getEndpoint()), eq(request), eq(-1), Mockito.any())).thenAnswer(new Answer<Future>() {
@Override
public Future answer(InvocationOnMock invocation) throws Throwable {
return new CompletableFuture<>();
}
});
assertEquals(11, r.statInfo.firstLogIndex);
assertEquals(10, r.statInfo.lastLogIndex);
Mockito.when(this.logManager.getTerm(20)).thenReturn(1L);
assertTrue(Replicator.continueSending(this.id, 0));
assertNotNull(r.getRpcInFly());
assertNotSame(rpcInFly, r.getRpcInFly());
assertEquals(11, r.statInfo.firstLogIndex);
assertEquals(20, r.statInfo.lastLogIndex);
assertEquals(0, r.getWaitId());
assertEquals(Replicator.RunningState.IDLE, r.statInfo.runningState);
}
use of org.apache.ignite.raft.jraft.rpc.Message in project ignite-3 by apache.
the class ReplicatorTest method testOnRpcReturnedLessLogs.
@Test
public void testOnRpcReturnedLessLogs() {
final Replicator r = getReplicator();
assertEquals(11, r.getRealNextIndex());
final RpcRequests.AppendEntriesRequest request = createEmptyEntriesRequest();
final RpcRequests.AppendEntriesResponse response = raftOptions.getRaftMessagesFactory().appendEntriesResponse().success(false).lastLogIndex(8).term(1).build();
this.id.unlock();
final Future<Message> rpcInFly = r.getRpcInFly();
assertNotNull(rpcInFly);
Mockito.when(this.logManager.getTerm(8)).thenReturn(1L);
final RpcRequests.AppendEntriesRequest newReq = raftOptions.getRaftMessagesFactory().appendEntriesRequest().groupId("test").serverId(new PeerId("localhost", 8082).toString()).peerId(this.peerId.toString()).term(1).prevLogIndex(8).prevLogTerm(1).data(ByteString.EMPTY).committedIndex(0).build();
Mockito.when(this.rpcService.appendEntries(eq(this.peerId.getEndpoint()), eq(newReq), eq(-1), Mockito.any())).thenReturn(new CompletableFuture<>());
Replicator.onRpcReturned(this.id, Replicator.RequestType.AppendEntries, Status.OK(), request, response, 0, 0, Utils.monotonicMs());
assertNotNull(r.getRpcInFly());
assertNotSame(r.getRpcInFly(), rpcInFly);
assertEquals(Replicator.RunningState.APPENDING_ENTRIES, r.statInfo.runningState);
this.id.unlock();
assertEquals(0, Replicator.getNextIndex(this.id));
assertEquals(9, r.getRealNextIndex());
}
Aggregations