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();
}
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());
}
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();
}
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());
}
}
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();
}
}
Aggregations