Search in sources :

Example 26 with Message

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

the class SnapshotExecutorTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() throws Exception {
    RaftMessagesFactory msgFactory = raftOptions.getRaftMessagesFactory();
    final RpcRequests.InstallSnapshotRequest irb = msgFactory.installSnapshotRequest().groupId("test").peerId(addr.toString()).serverId("localhost:8080").uri("remote://localhost:8080/99").term(0).meta(msgFactory.snapshotMeta().lastIncludedIndex(1).lastIncludedTerm(2).build()).build();
    Mockito.when(raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
    final CompletableFuture<Message> fut = new CompletableFuture<>();
    final GetFileRequestBuilder rb = msgFactory.getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true);
    // Mock get metadata
    ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(fut);
    Future<?> snapFut = Utils.runInThread(ForkJoinPool.commonPool(), () -> executor.installSnapshot(irb, msgFactory.installSnapshotResponse(), new RpcRequestClosure(asyncCtx, msgFactory)));
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    final ByteBuffer metaBuf = table.saveToByteBufferAsRemote();
    closure.setResponse(msgFactory.getFileResponse().readSize(metaBuf.remaining()).eof(true).data(new ByteString(metaBuf)).build());
    // Mock get file
    argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    rb.filename("testFile");
    rb.count(raftOptions.getMaxByteCountPerRpc());
    Mockito.when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(fut);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    closure = argument.getValue();
    closure.setResponse(msgFactory.getFileResponse().readSize(100).eof(true).data(new ByteString(new byte[100])).build());
    ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
    Mockito.when(fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(loadSnapshotArg, 5_000));
    final LoadSnapshotClosure done = loadSnapshotArg.getValue();
    final SnapshotReader reader = done.start();
    assertNotNull(reader);
    assertEquals(1, reader.listFiles().size());
    assertTrue(reader.listFiles().contains("testFile"));
    done.run(Status.OK());
    executor.join();
    assertTrue(snapFut.isDone());
    assertEquals(2, executor.getLastSnapshotTerm());
    assertEquals(1, executor.getLastSnapshotIndex());
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) ByteString(org.apache.ignite.raft.jraft.util.ByteString) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteBuffer(java.nio.ByteBuffer) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) LoadSnapshotClosure(org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure) CompletableFuture(java.util.concurrent.CompletableFuture) GetFileRequestBuilder(org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) LocalSnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.local.LocalSnapshotReader) Test(org.junit.jupiter.api.Test)

Example 27 with Message

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

the class SnapshotExecutorTest method testInterruptInstalling.

@Test
public void testInterruptInstalling() throws Exception {
    RaftMessagesFactory msgFactory = raftOptions.getRaftMessagesFactory();
    final RpcRequests.InstallSnapshotRequest irb = msgFactory.installSnapshotRequest().groupId("test").peerId(addr.toString()).serverId("localhost:8080").uri("remote://localhost:8080/99").term(0).meta(msgFactory.snapshotMeta().lastIncludedIndex(1).lastIncludedTerm(1).build()).build();
    Mockito.lenient().when(raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
    final CompletableFuture<Message> future = new CompletableFuture<>();
    final RpcRequests.GetFileRequest rb = msgFactory.getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true).build();
    // Mock get metadata
    final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.lenient().when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    executorService = singleThreadExecutor;
    Utils.runInThread(singleThreadExecutor, () -> executor.installSnapshot(irb, msgFactory.installSnapshotResponse(), new RpcRequestClosure(asyncCtx, msgFactory)));
    executor.interruptDownloadingSnapshots(1);
    executor.join();
    assertEquals(0, executor.getLastSnapshotTerm());
    assertEquals(0, executor.getLastSnapshotIndex());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) ExecutorService(java.util.concurrent.ExecutorService) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) Test(org.junit.jupiter.api.Test)

Example 28 with Message

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

the class LocalSnapshotCopierTest method testInterrupt.

@Test
@Disabled("https://issues.apache.org/jira/browse/IGNITE-16620")
public void testInterrupt() throws Exception {
    final CompletableFuture<Message> future = new CompletableFuture<>();
    final RpcRequests.GetFileRequest rb = raftOptions.getRaftMessagesFactory().getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true).build();
    // mock get metadata
    ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    this.copier.start();
    Utils.runInThread(ForkJoinPool.commonPool(), () -> LocalSnapshotCopierTest.this.copier.cancel());
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.ignite.raft.jraft.rpc.Message) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 29 with Message

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

the class ActionRequestProcessor method sendRaftError.

/**
 * @param ctx    The context.
 * @param status The status.
 * @param node   Raft node.
 */
private void sendRaftError(RpcContext ctx, Status status, Node node) {
    RaftError raftError = status.getRaftError();
    Message response;
    if (raftError == RaftError.EPERM && node.getLeaderId() != null)
        response = RaftRpcFactory.DEFAULT.newResponse(node.getLeaderId().toString(), factory, RaftError.EPERM, status.getErrorMsg());
    else
        response = RaftRpcFactory.DEFAULT.newResponse(factory, raftError, status.getErrorMsg());
    ctx.sendResponse(response);
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) RaftError(org.apache.ignite.raft.jraft.error.RaftError)

Example 30 with Message

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

the class IgniteRpcClient method invokeAsync.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Message> invokeAsync(Endpoint endpoint, Object request, InvokeContext ctx, InvokeCallback callback, long timeoutMs) {
    CompletableFuture<Message> fut = new CompletableFuture<>();
    fut.orTimeout(timeoutMs, TimeUnit.MILLISECONDS).whenComplete((res, err) -> {
        assert !(res == null && err == null) : res + " " + err;
        if (err == null && recordPred != null && recordPred.test(res, this.toString()))
            recordedMsgs.add(new Object[] { res, this.toString(), fut.hashCode(), System.currentTimeMillis(), null });
        if (err instanceof ExecutionException)
            err = new RemotingException(err);
        else if (// Translate timeout exception.
        err instanceof TimeoutException)
            err = new InvokeTimeoutException();
        Throwable finalErr = err;
        // Avoid deadlocks if a closure has completed in the same thread.
        Utils.runInThread(callback.executor(), () -> callback.complete(res, finalErr));
    });
    // Future hashcode used as corellation id.
    if (recordPred != null && recordPred.test(request, endpoint.toString()))
        recordedMsgs.add(new Object[] { request, endpoint.toString(), fut.hashCode(), System.currentTimeMillis(), null });
    synchronized (this) {
        if (blockPred != null && blockPred.test(request, endpoint.toString())) {
            Object[] msgData = { request, endpoint.toString(), fut.hashCode(), System.currentTimeMillis(), (Runnable) () -> send(endpoint, request, fut, timeoutMs) };
            blockedMsgs.add(msgData);
            LOG.info("Blocked message to={} id={} msg={}", endpoint.toString(), msgData[2], S.toString(request));
            return fut;
        }
    }
    send(endpoint, request, fut, timeoutMs);
    return fut;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InvokeTimeoutException(org.apache.ignite.raft.jraft.error.InvokeTimeoutException) NetworkMessage(org.apache.ignite.network.NetworkMessage) Message(org.apache.ignite.raft.jraft.rpc.Message) RemotingException(org.apache.ignite.raft.jraft.error.RemotingException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) InvokeTimeoutException(org.apache.ignite.raft.jraft.error.InvokeTimeoutException)

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