Search in sources :

Example 11 with ByteBufferCollector

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

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(org.apache.ignite.raft.jraft.util.ByteBufferCollector) Test(org.junit.jupiter.api.Test) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest)

Example 12 with ByteBufferCollector

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

the class SnapshotFileReaderTest method testReadMetaFile.

@Test
public void testReadMetaFile() throws Exception {
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(1024);
    final LocalFileMetaOutter.LocalFileMeta meta = addDataMeta();
    assertEquals(-1, this.reader.readFile(bufRef, Snapshot.JRAFT_SNAPSHOT_META_FILE, 0, Integer.MAX_VALUE));
    final ByteBuffer buf = bufRef.getBuffer();
    buf.flip();
    final LocalSnapshotMetaTable newTable = new LocalSnapshotMetaTable(new RaftOptions());
    newTable.loadFromIoBufferAsRemote(buf);
    assertEquals(meta, newTable.getFileMeta("data"));
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) LocalFileMetaOutter(org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter) ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest)

Example 13 with ByteBufferCollector

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

the class CopySessionTest method testOnRpcReturnedEOF.

@Test
public void testOnRpcReturnedEOF() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(() -> {
        try {
            // test join, should return
            session.join();
            latch.countDown();
        } catch (final InterruptedException e) {
        // No-op.
        }
    });
    try {
        t.start();
        assertNull(this.session.getRpcCall());
        final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
        this.session.setDestBuf(bufRef);
        this.session.onRpcReturned(Status.OK(), raftOpts.getRaftMessagesFactory().getFileResponse().readSize(100).eof(true).data(new ByteString(new byte[100])).build());
        assertEquals(100, bufRef.capacity());
        // should be flip
        assertEquals(0, bufRef.getBuffer().position());
        assertEquals(100, bufRef.getBuffer().remaining());
        assertNull(this.session.getRpcCall());
        latch.await();
    } finally {
        t.join();
    }
}
Also used : ByteBufferCollector(org.apache.ignite.raft.jraft.util.ByteBufferCollector) ByteString(org.apache.ignite.raft.jraft.util.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 14 with ByteBufferCollector

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

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(org.apache.ignite.raft.jraft.util.ByteBufferCollector) Session(org.apache.ignite.raft.jraft.storage.snapshot.remote.Session)

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