Search in sources :

Example 1 with RpcResponseClosure

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

the class LocalSnapshotCopierTest method testCancelByRemote.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCancelByRemote() 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();
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    final RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    closure.run(new Status(RaftError.ECANCELED, "test cancel"));
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    assertEquals("test cancel", this.copier.getErrorMsg());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) 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)

Example 2 with RpcResponseClosure

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

the class LocalSnapshotCopierTest method testStartJoinFinishOK.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testStartJoinFinishOK() throws Exception {
    final CompletableFuture<Message> future = new CompletableFuture<>();
    final GetFileRequestBuilder rb = raftOptions.getRaftMessagesFactory().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(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    this.copier.start();
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    final ByteBuffer metaBuf = this.table.saveToByteBufferAsRemote();
    RpcRequests.GetFileResponse response = raftOptions.getRaftMessagesFactory().getFileResponse().readSize(metaBuf.remaining()).eof(true).data(new ByteString(metaBuf)).build();
    closure.setResponse(response);
    // mock get file
    argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    rb.filename("testFile");
    rb.count(this.raftOptions.getMaxByteCountPerRpc());
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    closure = argument.getValue();
    response = raftOptions.getRaftMessagesFactory().getFileResponse().readSize(100).eof(true).data(new ByteString(new byte[100])).build();
    closure.setResponse(response);
    closure.run(Status.OK());
    this.copier.join();
    final SnapshotReader reader = this.copier.getReader();
    assertSame(this.reader, reader);
    assertEquals(1, this.writer.listFiles().size());
    assertTrue(this.writer.listFiles().contains("testFile"));
}
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) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteBuffer(java.nio.ByteBuffer) 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) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 3 with RpcResponseClosure

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

use of org.apache.ignite.raft.jraft.rpc.RpcResponseClosure 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 RpcResponseClosure

use of org.apache.ignite.raft.jraft.rpc.RpcResponseClosure 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)

Aggregations

RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)8 Test (org.junit.jupiter.api.Test)8 CompletableFuture (java.util.concurrent.CompletableFuture)5 Message (org.apache.ignite.raft.jraft.rpc.Message)5 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)5 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)5 Status (org.apache.ignite.raft.jraft.Status)4 SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)3 ReadIndexStatus (org.apache.ignite.raft.jraft.entity.ReadIndexStatus)3 ReadIndexRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest)3 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 ByteBuffer (java.nio.ByteBuffer)2 RaftMessagesFactory (org.apache.ignite.raft.jraft.RaftMessagesFactory)2 GetFileRequestBuilder (org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder)2 RpcRequestClosure (org.apache.ignite.raft.jraft.rpc.RpcRequestClosure)2 ByteString (org.apache.ignite.raft.jraft.util.ByteString)2 ExecutorService (java.util.concurrent.ExecutorService)1