Search in sources :

Example 6 with Message

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

the class CopySessionTest method testOnRpcReturnedOK.

@Test
public void testOnRpcReturnedOK() {
    assertNull(this.session.getRpcCall());
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
    this.session.setDestBuf(bufRef);
    final CompletableFuture<Message> future = new CompletableFuture<>();
    final RpcRequests.GetFileRequest rb = raftOpts.getRaftMessagesFactory().getFileRequest().readerId(99).filename("data").count(Integer.MAX_VALUE).offset(100).readPartly(true).build();
    Mockito.when(this.rpcService.getFile(this.address, rb, this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
    this.session.onRpcReturned(Status.OK(), raftOpts.getRaftMessagesFactory().getFileResponse().readSize(100).eof(false).data(new ByteString(new byte[100])).build());
    assertEquals(100, bufRef.capacity());
    assertEquals(100, bufRef.getBuffer().position());
    assertNotNull(this.session.getRpcCall());
    // send next request
    assertSame(future, this.session.getRpcCall());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) Message(org.apache.ignite.raft.jraft.rpc.Message) ByteString(org.apache.ignite.raft.jraft.util.ByteString) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) Test(org.junit.jupiter.api.Test)

Example 7 with Message

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

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

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

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

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