Search in sources :

Example 6 with ByteBufferCollector

use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.

the class LocalFileReaderTest method testReadSmallInitBuffer.

@Test
public void testReadSmallInitBuffer() throws Exception {
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(2);
    final String data = writeData();
    assertReadResult(bufRef, data);
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 7 with ByteBufferCollector

use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.

the class LocalFileReaderTest method testReadBigFile.

@Test
public void testReadBigFile() throws Exception {
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(2);
    final File file = new File(this.path + File.separator + "data");
    String data = "";
    for (int i = 0; i < 4096; i++) {
        data += i % 10;
    }
    FileUtils.writeStringToFile(file, data);
    int read = this.fileReader.readFile(bufRef, "data", 0, 1024);
    assertEquals(1024, read);
    read = this.fileReader.readFile(bufRef, "data", 1024, 1024);
    assertEquals(1024, read);
    read = this.fileReader.readFile(bufRef, "data", 1024 + 1024, 1024);
    assertEquals(1024, read);
    read = this.fileReader.readFile(bufRef, "data", 1024 + 1024 + 1024, 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));
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 8 with ByteBufferCollector

use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.

the class AppendEntriesBenchmark method sendEntries3.

private static byte[] sendEntries3(final int entryCount, final int sizeOfEntry, AdaptiveBufAllocator.Handle allocator) {
    final AppendEntriesRequest.Builder rb = AppendEntriesRequest.newBuilder();
    fillCommonFields(rb);
    final ByteBufferCollector dataBuffer = allocator.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();
        allocator.record(remaining);
        rb.setData(ZeroByteStringHelper.wrap(buf));
        return rb.build().toByteArray();
    } finally {
        RecycleUtil.recycle(dataBuffer);
    }
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) AppendEntriesRequest(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest) ByteBuffer(java.nio.ByteBuffer)

Example 9 with ByteBufferCollector

use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.

the class AppendEntriesBenchmark method sendEntries1.

private static byte[] sendEntries1(final int entryCount, final int sizeOfEntry) {
    final AppendEntriesRequest.Builder rb = AppendEntriesRequest.newBuilder();
    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.setData(ZeroByteStringHelper.wrap(buf));
    return rb.build().toByteArray();
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) AppendEntriesRequest(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest) ByteBuffer(java.nio.ByteBuffer)

Example 10 with ByteBufferCollector

use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.

the class LocalSnapshotCopier method loadMetaTable.

private void loadMetaTable() throws InterruptedException {
    final ByteBufferCollector metaBuf = ByteBufferCollector.allocate(0);
    Session session = null;
    try {
        this.lock.lock();
        try {
            if (this.cancelled) {
                if (isOk()) {
                    setError(RaftError.ECANCELED, "ECANCELED");
                }
                return;
            }
            session = this.copier.startCopy2IoBuffer(Snapshot.JRAFT_SNAPSHOT_META_FILE, metaBuf, null);
            this.curSession = session;
        } finally {
            this.lock.unlock();
        }
        // join out of lock.
        session.join();
        this.lock.lock();
        try {
            this.curSession = null;
        } finally {
            this.lock.unlock();
        }
        if (!session.status().isOk() && isOk()) {
            LOG.warn("Fail to copy meta file: {}", session.status());
            setError(session.status().getCode(), session.status().getErrorMsg());
            return;
        }
        if (!this.remoteSnapshot.getMetaTable().loadFromIoBufferAsRemote(metaBuf.getBuffer())) {
            LOG.warn("Bad meta_table format");
            setError(-1, "Bad meta_table format from remote");
            return;
        }
        Requires.requireTrue(this.remoteSnapshot.getMetaTable().hasMeta(), "Invalid remote snapshot meta:%s", this.remoteSnapshot.getMetaTable().getMeta());
    } finally {
        if (session != null) {
            Utils.closeQuietly(session);
        }
    }
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Session(com.alipay.sofa.jraft.storage.snapshot.remote.Session)

Aggregations

ByteBufferCollector (com.alipay.sofa.jraft.util.ByteBufferCollector)14 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.Test)8 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)5 AppendEntriesRequest (com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest)4 Message (com.google.protobuf.Message)3 Status (com.alipay.sofa.jraft.Status)2 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)2 FileNotFoundException (java.io.FileNotFoundException)2 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)1 RetryAgainException (com.alipay.sofa.jraft.error.RetryAgainException)1 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)1 GetFileResponse (com.alipay.sofa.jraft.rpc.RpcRequests.GetFileResponse)1 RpcResponseClosureAdapter (com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter)1 FileReader (com.alipay.sofa.jraft.storage.io.FileReader)1 Session (com.alipay.sofa.jraft.storage.snapshot.remote.Session)1 Recyclable (com.alipay.sofa.jraft.util.Recyclable)1 RecyclableByteBufferList (com.alipay.sofa.jraft.util.RecyclableByteBufferList)1 File (java.io.File)1 IOException (java.io.IOException)1