Search in sources :

Example 31 with Message

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);
    }
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) Node(org.apache.ignite.raft.jraft.Node)

Example 32 with Message

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;
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.ignite.raft.jraft.rpc.Message) RemotingException(org.apache.ignite.raft.jraft.error.RemotingException)

Example 33 with Message

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);
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteString(org.apache.ignite.raft.jraft.util.ByteString) Test(org.junit.jupiter.api.Test)

Example 34 with Message

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);
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) ByteString(org.apache.ignite.raft.jraft.util.ByteString) ArrayList(java.util.ArrayList) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) AppendEntriesRequestBuilder(org.apache.ignite.raft.jraft.rpc.AppendEntriesRequestBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 35 with Message

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());
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

Message (org.apache.ignite.raft.jraft.rpc.Message)35 Status (org.apache.ignite.raft.jraft.Status)18 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)16 Test (org.junit.jupiter.api.Test)15 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)12 JRaftException (org.apache.ignite.raft.jraft.error.JRaftException)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 ByteString (org.apache.ignite.raft.jraft.util.ByteString)7 RpcRequestClosure (org.apache.ignite.raft.jraft.rpc.RpcRequestClosure)6 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)5 SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)5 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)5 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)3 RemotingException (org.apache.ignite.raft.jraft.error.RemotingException)3 AppendEntriesRequestBuilder (org.apache.ignite.raft.jraft.rpc.AppendEntriesRequestBuilder)3 RpcContext (org.apache.ignite.raft.jraft.rpc.RpcContext)3 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)3 ByteBufferCollector (org.apache.ignite.raft.jraft.util.ByteBufferCollector)3