Search in sources :

Example 1 with ByteBufferCollector

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

the class SnapshotFileReaderTest method testReadFile.

@Test
public void testReadFile() throws Exception {
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate();
    try {
        this.reader.readFile(bufRef, "unfound", 0, 1024);
        fail();
    } catch (final FileNotFoundException e) {
    // No-op.
    }
    final String data = writeData();
    addDataMeta();
    final int read = this.reader.readFile(bufRef, "data", 0, 1024);
    assertEquals(-1, read);
    final ByteBuffer buf = bufRef.getBuffer();
    buf.flip();
    assertEquals(data.length(), buf.remaining());
    final byte[] bs = new byte[data.length()];
    buf.get(bs);
    assertEquals(data, new String(bs, UTF_8));
}
Also used : ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) FileNotFoundException(java.io.FileNotFoundException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest)

Example 2 with ByteBufferCollector

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

the class CopySessionTest method testOnRpcReturnedRetry.

@Test
public void testOnRpcReturnedRetry() throws Exception {
    assertNull(this.session.getTimer());
    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(0).readPartly(true).build();
    Mockito.when(this.rpcService.getFile(this.address, rb, this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
    this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
    assertNotNull(this.session.getTimer());
    Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
    assertNotNull(this.session.getRpcCall());
    assertSame(future, this.session.getRpcCall());
    assertNull(this.session.getTimer());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) Test(org.junit.jupiter.api.Test)

Example 3 with ByteBufferCollector

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

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

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

Aggregations

ByteBufferCollector (org.apache.ignite.raft.jraft.util.ByteBufferCollector)14 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.jupiter.api.Test)8 ByteString (org.apache.ignite.raft.jraft.util.ByteString)7 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)5 Message (org.apache.ignite.raft.jraft.rpc.Message)3 FileNotFoundException (java.io.FileNotFoundException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Status (org.apache.ignite.raft.jraft.Status)2 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 EntryMetaBuilder (org.apache.ignite.raft.jraft.entity.EntryMetaBuilder)1 LocalFileMetaOutter (org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter)1 RetryAgainException (org.apache.ignite.raft.jraft.error.RetryAgainException)1 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)1 AppendEntriesRequestBuilder (org.apache.ignite.raft.jraft.rpc.AppendEntriesRequestBuilder)1 GetFileResponseBuilder (org.apache.ignite.raft.jraft.rpc.GetFileResponseBuilder)1