Search in sources :

Example 41 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class TestPersistentBinaryDeque method testOfferThenPoll.

@Test
public void testOfferThenPoll() throws Exception {
    System.out.println("Running testOfferThanPoll");
    BinaryDequeReader reader = m_pbd.openForRead(CURSOR_ID);
    assertNull(reader.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY));
    //Make sure a single file with the appropriate data is created
    m_pbd.offer(defaultContainer());
    File[] files = TEST_DIR.listFiles();
    assertEquals(1, files.length);
    assertTrue("pbd_nonce.0.pbd".equals(files[0].getName()));
    //Now make sure the current write file is stolen and a new write file created
    BBContainer retval = reader.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
    retval.discard();
}
Also used : BinaryDequeReader(org.voltdb.utils.BinaryDeque.BinaryDequeReader) BBContainer(org.voltcore.utils.DBBPool.BBContainer) File(java.io.File) Test(org.junit.Test)

Example 42 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class TestPBDMultipleReaders method testSegmentClosingWriterMultipleReaders.

@Test
public void testSegmentClosingWriterMultipleReaders() throws Exception {
    assertEquals(1, m_pbd.numOpenSegments());
    int numSegments = 5;
    for (int i = 0; i < numSegments; i++) {
        for (int j = 0; j < s_segmentFillCount; j++) {
            m_pbd.offer(DBBPool.wrapBB(TestPersistentBinaryDeque.getFilledBuffer(j)));
        }
        assertEquals(1, m_pbd.numOpenSegments());
    }
    BinaryDequeReader reader0 = m_pbd.openForRead("reader0");
    BinaryDequeReader reader1 = m_pbd.openForRead("reader1");
    // Position first reader0 on penultimate segment and reader1 on first segment
    for (int i = 0; i < numSegments - 1; i++) {
        for (int j = 0; j < 46; j++) {
            BBContainer bbC = reader0.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
            bbC.discard();
            if (i == 0) {
                bbC = reader1.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
                bbC.discard();
            }
        }
        BBContainer bbC = reader0.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
        bbC.discard();
        if (i == 0) {
            assertEquals(2, m_pbd.numOpenSegments());
        } else {
            assertEquals(3, m_pbd.numOpenSegments());
        }
    }
    BBContainer bbC = reader1.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
    bbC.discard();
    // Both readers finished reading first segment, so that is closed and deleted,
    // which reduces the # of open segments by 1
    assertEquals(2, m_pbd.numOpenSegments());
    // reader0 at penultimate. Move reader1 through segments and check open segments
    for (int i = 1; i < numSegments - 1; i++) {
        for (int j = 0; j < 46; j++) {
            bbC = reader1.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
            bbC.discard();
        }
        int expected = (i == numSegments - 2) ? 2 : 3;
        assertEquals(expected, m_pbd.numOpenSegments());
        bbC = reader1.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
        bbC.discard();
        expected = (i == numSegments - 2) ? 1 : 2;
        assertEquals(expected, m_pbd.numOpenSegments());
    }
    // read the last segment
    for (int j = 0; j < s_segmentFillCount; j++) {
        bbC = reader0.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
        bbC.discard();
        bbC = reader1.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
        bbC.discard();
    }
    assertEquals(1, m_pbd.numOpenSegments());
}
Also used : BinaryDequeReader(org.voltdb.utils.BinaryDeque.BinaryDequeReader) BBContainer(org.voltcore.utils.DBBPool.BBContainer) Test(org.junit.Test)

Example 43 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class PersistentBinaryDeque method push.

@Override
public synchronized void push(BBContainer[] objects) throws IOException {
    assertions();
    if (m_closed) {
        throw new IOException("Cannot push(): PBD has been Closed");
    }
    ArrayDeque<ArrayDeque<BBContainer>> segments = new ArrayDeque<ArrayDeque<BBContainer>>();
    ArrayDeque<BBContainer> currentSegment = new ArrayDeque<BBContainer>();
    //Take the objects that were provided and separate them into deques of objects
    //that will fit in a single write segment
    int available = PBDSegment.CHUNK_SIZE - 4;
    for (BBContainer object : objects) {
        int needed = PBDSegment.OBJECT_HEADER_BYTES + object.b().remaining();
        if (available - needed < 0) {
            if (needed > PBDSegment.CHUNK_SIZE - 4) {
                throw new IOException("Maximum object size is " + (PBDSegment.CHUNK_SIZE - 4));
            }
            segments.offer(currentSegment);
            currentSegment = new ArrayDeque<BBContainer>();
            available = PBDSegment.CHUNK_SIZE - 4;
        }
        available -= needed;
        currentSegment.add(object);
    }
    segments.add(currentSegment);
    assert (segments.size() > 0);
    //Calculate the index for the first segment to push at the front
    //This will be the index before the first segment available for read or
    //before the write segment if there are no finished segments
    Long nextIndex = 0L;
    if (m_segments.size() > 0) {
        nextIndex = peekFirstSegment().segmentId() - 1;
    }
    while (segments.peek() != null) {
        ArrayDeque<BBContainer> currentSegmentContents = segments.poll();
        PBDSegment writeSegment = newSegment(nextIndex, new VoltFile(m_path, m_nonce + "." + nextIndex + ".pbd"));
        writeSegment.openForWrite(true);
        nextIndex--;
        if (m_usageSpecificLog.isDebugEnabled()) {
            m_usageSpecificLog.debug("Segment " + writeSegment.file() + " has been created because of a push");
        }
        while (currentSegmentContents.peek() != null) {
            writeSegment.offer(currentSegmentContents.pollFirst(), false);
            m_numObjects++;
        }
        // Don't close the last one, it'll be used for writes
        if (!m_segments.isEmpty()) {
            writeSegment.close();
        }
        m_segments.put(writeSegment.segmentId(), writeSegment);
    }
    // Because we inserted at the beginning, cursors need to be rewound to the beginning
    rewindCursors();
    assertions();
}
Also used : BBContainer(org.voltcore.utils.DBBPool.BBContainer) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque)

Example 44 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class TestPBDMultipleReaders method testSegmentClosingWriterReader.

@Test
public void testSegmentClosingWriterReader() throws Exception {
    assertEquals(1, m_pbd.numOpenSegments());
    int numSegments = 3;
    for (int i = 0; i < numSegments; i++) {
        for (int j = 0; j < s_segmentFillCount; j++) {
            m_pbd.offer(DBBPool.wrapBB(TestPersistentBinaryDeque.getFilledBuffer(j)));
        }
        assertEquals(1, m_pbd.numOpenSegments());
    }
    BinaryDequeReader reader = m_pbd.openForRead("reader0");
    for (int i = 0; i < numSegments; i++) {
        for (int j = 0; j < 46; j++) {
            BBContainer bbC = reader.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
            bbC.discard();
        }
        int expected = (i == numSegments - 1) ? 1 : 2;
        assertEquals(expected, m_pbd.numOpenSegments());
        BBContainer bbC = reader.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
        bbC.discard();
        // there should only be 1 open because last discard closes and deletes
        assertEquals(1, m_pbd.numOpenSegments());
    }
}
Also used : BinaryDequeReader(org.voltdb.utils.BinaryDeque.BinaryDequeReader) BBContainer(org.voltcore.utils.DBBPool.BBContainer) Test(org.junit.Test)

Example 45 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class TestPersistentBinaryDeque method testNonceWithDots.

@Test
public void testNonceWithDots() throws Exception {
    System.out.println("Running testNonceWithDots");
    PersistentBinaryDeque pbd = new PersistentBinaryDeque("ha.ha", TEST_DIR, logger);
    pbd.offer(defaultContainer());
    pbd.close();
    pbd = new PersistentBinaryDeque("ha.ha", TEST_DIR, logger);
    BinaryDequeReader reader = pbd.openForRead(CURSOR_ID);
    BBContainer bb = reader.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
    try {
        ByteBuffer defaultBuffer = defaultBuffer();
        defaultBuffer.clear();
        assertEquals(defaultBuffer, bb.b());
        pbd.close();
    } finally {
        bb.discard();
    }
}
Also used : BinaryDequeReader(org.voltdb.utils.BinaryDeque.BinaryDequeReader) BBContainer(org.voltcore.utils.DBBPool.BBContainer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

BBContainer (org.voltcore.utils.DBBPool.BBContainer)57 Test (org.junit.Test)22 ByteBuffer (java.nio.ByteBuffer)21 BinaryDequeReader (org.voltdb.utils.BinaryDeque.BinaryDequeReader)18 IOException (java.io.IOException)15 File (java.io.File)10 JSONObject (org.json_voltpatches.JSONObject)4 BinaryDequeTruncator (org.voltdb.utils.BinaryDeque.BinaryDequeTruncator)4 TruncatorResponse (org.voltdb.utils.BinaryDeque.TruncatorResponse)4 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 FileInputStream (java.io.FileInputStream)2 Collection (java.util.Collection)2 Random (java.util.Random)2 Callable (java.util.concurrent.Callable)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 PureJavaCrc32C (org.apache.hadoop_voltpatches.util.PureJavaCrc32C)2 JSONException (org.json_voltpatches.JSONException)2 JSONString (org.json_voltpatches.JSONString)2 HashinatorConfig (org.voltdb.TheHashinator.HashinatorConfig)2