Search in sources :

Example 1 with ByteString

use of org.apache.ignite.raft.jraft.util.ByteString 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 2 with ByteString

use of org.apache.ignite.raft.jraft.util.ByteString in project ignite-3 by apache.

the class AppendEntriesBenchmark method sendEntries2.

private byte[] sendEntries2() {
    final AppendEntriesRequestBuilder rb = msgFactory.appendEntriesRequest();
    fillCommonFields(rb);
    final ByteBufferCollector dataBuffer = ByteBufferCollector.allocateByRecyclers();
    try {
        for (int i = 0; i < entryCount; i++) {
            final byte[] bytes = new byte[sizeOfEntry];
            ThreadLocalRandom.current().nextBytes(bytes);
            final ByteBuffer buf = ByteBuffer.wrap(bytes);
            dataBuffer.put(buf.slice());
        }
        final ByteBuffer buf = dataBuffer.getBuffer();
        buf.flip();
        rb.data(new ByteString(buf));
        return Marshaller.DEFAULT.marshall(rb.build());
    } finally {
        RecycleUtil.recycle(dataBuffer);
    }
}
Also used : ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) ByteString(org.apache.ignite.raft.jraft.util.ByteString) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ByteString

use of org.apache.ignite.raft.jraft.util.ByteString in project ignite-3 by apache.

the class AppendEntriesBenchmark method sendEntries3.

private byte[] sendEntries3() {
    final AppendEntriesRequestBuilder rb = msgFactory.appendEntriesRequest();
    fillCommonFields(rb);
    final ByteBufferCollector dataBuffer = handleThreadLocal.get().allocateByRecyclers();
    try {
        for (int i = 0; i < entryCount; i++) {
            final byte[] bytes = new byte[sizeOfEntry];
            ThreadLocalRandom.current().nextBytes(bytes);
            final ByteBuffer buf = ByteBuffer.wrap(bytes);
            dataBuffer.put(buf.slice());
        }
        final ByteBuffer buf = dataBuffer.getBuffer();
        buf.flip();
        final int remaining = buf.remaining();
        handleThreadLocal.get().record(remaining);
        rb.data(new ByteString(buf));
        return Marshaller.DEFAULT.marshall(rb.build());
    } finally {
        RecycleUtil.recycle(dataBuffer);
    }
}
Also used : ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) ByteString(org.apache.ignite.raft.jraft.util.ByteString) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ByteString

use of org.apache.ignite.raft.jraft.util.ByteString in project ignite-3 by apache.

the class AppendEntriesBenchmark method sendEntries1.

private byte[] sendEntries1() {
    final AppendEntriesRequestBuilder rb = msgFactory.appendEntriesRequest();
    fillCommonFields(rb);
    final ByteBufferCollector dataBuffer = ByteBufferCollector.allocate();
    for (int i = 0; i < entryCount; i++) {
        final byte[] bytes = new byte[sizeOfEntry];
        ThreadLocalRandom.current().nextBytes(bytes);
        final ByteBuffer buf = ByteBuffer.wrap(bytes);
        dataBuffer.put(buf.slice());
    }
    final ByteBuffer buf = dataBuffer.getBuffer();
    buf.flip();
    rb.data(new ByteString(buf));
    return Marshaller.DEFAULT.marshall(rb.build());
}
Also used : ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) ByteString(org.apache.ignite.raft.jraft.util.ByteString) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ByteString

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

Aggregations

ByteString (org.apache.ignite.raft.jraft.util.ByteString)11 ByteBuffer (java.nio.ByteBuffer)7 ByteBufferCollector (org.apache.ignite.raft.jraft.util.ByteBufferCollector)7 Message (org.apache.ignite.raft.jraft.rpc.Message)5 Test (org.junit.jupiter.api.Test)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)4 ArrayList (java.util.ArrayList)3 AppendEntriesRequestBuilder (org.apache.ignite.raft.jraft.rpc.AppendEntriesRequestBuilder)2 GetFileRequestBuilder (org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder)2 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)2 SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)2 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 RaftMessagesFactory (org.apache.ignite.raft.jraft.RaftMessagesFactory)1 Status (org.apache.ignite.raft.jraft.Status)1 LoadSnapshotClosure (org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure)1