Search in sources :

Example 1 with RpcRequestClosure

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

the class FileServiceTest method testGetLargeFileData.

@Test
public void testGetLargeFileData() throws IOException {
    final String data = writeLargeData();
    final long readerId = FileService.getInstance().addReader(this.fileReader);
    int fileOffset = 0;
    while (true) {
        final RpcRequests.GetFileRequest request = msgFactory.getFileRequest().count(4096).filename("data").offset(fileOffset).readerId(readerId).build();
        final RpcContext asyncContext = Mockito.mock(RpcContext.class);
        final Message msg = // 
        FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext, msgFactory));
        assertTrue(msg instanceof RpcRequests.GetFileResponse);
        final RpcRequests.GetFileResponse response = (RpcRequests.GetFileResponse) msg;
        final byte[] sourceArray = data.getBytes(UTF_8);
        final byte[] respData = response.data().toByteArray();
        final int length = sourceArray.length;
        int offset = 0;
        while (offset + length <= respData.length) {
            final byte[] respArray = new byte[length];
            System.arraycopy(respData, offset, respArray, 0, length);
            try {
                assertArrayEquals(sourceArray, respArray, "Offset: " + fileOffset);
            } catch (AssertionError e) {
                LOG.error("arrayComparisonFailure", e);
            }
            offset += length;
        }
        fileOffset += offset;
        if (response.eof()) {
            break;
        }
    }
}
Also used : RpcContext(org.apache.ignite.raft.jraft.rpc.RpcContext) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) Test(org.junit.jupiter.api.Test)

Example 2 with RpcRequestClosure

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

the class FileServiceTest method testGetFileData.

@Test
public void testGetFileData() throws IOException {
    writeData();
    long readerId = FileService.getInstance().addReader(this.fileReader);
    RpcRequests.GetFileRequest request = msgFactory.getFileRequest().count(Integer.MAX_VALUE).filename("data").offset(0).readerId(readerId).build();
    RpcContext asyncContext = Mockito.mock(RpcContext.class);
    Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext, msgFactory));
    assertTrue(msg instanceof RpcRequests.GetFileResponse);
    RpcRequests.GetFileResponse response = (RpcRequests.GetFileResponse) msg;
    assertTrue(response.eof());
    assertEquals("jraft is great!", new String(response.data().toByteArray(), UTF_8));
    assertEquals(-1, response.readSize());
}
Also used : RpcContext(org.apache.ignite.raft.jraft.rpc.RpcContext) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) Test(org.junit.jupiter.api.Test)

Example 3 with RpcRequestClosure

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

the class FileServiceTest method testGetFileNotFoundReader.

@Test
public void testGetFileNotFoundReader() {
    RpcRequests.GetFileRequest request = msgFactory.getFileRequest().count(Integer.MAX_VALUE).filename("data").offset(0).readerId(1).build();
    RpcContext asyncContext = Mockito.mock(RpcContext.class);
    Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext, msgFactory));
    assertTrue(msg instanceof RpcRequests.ErrorResponse);
    RpcRequests.ErrorResponse response = (RpcRequests.ErrorResponse) msg;
    assertEquals(RaftError.ENOENT.getNumber(), response.errorCode());
    assertEquals("Fail to find reader=1", response.errorMsg());
}
Also used : RpcContext(org.apache.ignite.raft.jraft.rpc.RpcContext) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) Test(org.junit.jupiter.api.Test)

Example 4 with RpcRequestClosure

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

the class FileServiceTest method testGetFileNotFound.

@Test
public void testGetFileNotFound() {
    long readerId = FileService.getInstance().addReader(this.fileReader);
    RpcRequests.GetFileRequest request = msgFactory.getFileRequest().count(Integer.MAX_VALUE).filename("data").offset(0).readerId(readerId).build();
    RpcContext asyncContext = Mockito.mock(RpcContext.class);
    Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext, msgFactory));
    assertTrue(msg instanceof RpcRequests.ErrorResponse);
    RpcRequests.ErrorResponse response = (RpcRequests.ErrorResponse) msg;
    assertEquals(RaftError.EIO.getNumber(), response.errorCode());
    assertEquals(String.format("Fail to read from path=%s filename=data", this.path), response.errorMsg());
}
Also used : RpcContext(org.apache.ignite.raft.jraft.rpc.RpcContext) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) Test(org.junit.jupiter.api.Test)

Example 5 with RpcRequestClosure

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

Message (org.apache.ignite.raft.jraft.rpc.Message)6 RpcRequestClosure (org.apache.ignite.raft.jraft.rpc.RpcRequestClosure)6 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)6 Test (org.junit.jupiter.api.Test)6 RpcContext (org.apache.ignite.raft.jraft.rpc.RpcContext)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 RaftMessagesFactory (org.apache.ignite.raft.jraft.RaftMessagesFactory)2 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)2 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)2 ByteBuffer (java.nio.ByteBuffer)1 ExecutorService (java.util.concurrent.ExecutorService)1 LoadSnapshotClosure (org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure)1 GetFileRequestBuilder (org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder)1 SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)1 LocalSnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.local.LocalSnapshotReader)1 ByteString (org.apache.ignite.raft.jraft.util.ByteString)1