Package org.voltcore.utils.DBBPool

Examples of org.voltcore.utils.DBBPool.BBContainer


        }

        RestoreWork restoreWork = null;
        long hsId = msg.getFirst();
        long targetId = msg.getSecond().getFirst();
        BBContainer container = msg.getSecond().getSecond();
        try {
            ByteBuffer block = container.b();
            byte typeByte = block.get(StreamSnapshotDataTarget.typeOffset);
            final int blockIndex = block.getInt(StreamSnapshotDataTarget.blockIndexOffset);
            StreamSnapshotMessageType type = StreamSnapshotMessageType.values()[typeByte];
            if (type == StreamSnapshotMessageType.FAILURE) {
                VoltDB.crashLocalVoltDB("Rejoin source sent failure message.", false, null);

                // for test code only
                if (m_expectedEOFs.decrementAndGet() == 0) {
                    m_EOF = true;
                }
            }
            else if (type == StreamSnapshotMessageType.END) {
                if (rejoinLog.isTraceEnabled()) {
                    rejoinLog.trace("Got END message " + blockIndex);
                }

                // End of stream, no need to ack this buffer
                if (m_expectedEOFs.decrementAndGet() == 0) {
                    m_EOF = true;
                }
            }
            else if (type == StreamSnapshotMessageType.SCHEMA) {
                rejoinLog.trace("Got SCHEMA message");

                block.position(StreamSnapshotDataTarget.contentOffset);
                byte[] schemaBytes = new byte[block.remaining()];
                block.get(schemaBytes);
                m_schemas.put(block.getInt(StreamSnapshotDataTarget.tableIdOffset),
                              schemaBytes);
            }
            else if (type == StreamSnapshotMessageType.HASHINATOR) {
                block.position(StreamSnapshotDataTarget.contentOffset);
                long version = block.getLong();
                byte[] hashinatorConfig = new byte[block.remaining()];
                block.get(hashinatorConfig);

                restoreWork = new HashinatorRestoreWork(version, hashinatorConfig);
            }
            else {
                // It's normal snapshot data afterwards

                final int tableId = block.getInt(StreamSnapshotDataTarget.tableIdOffset);

                if (!m_schemas.containsKey(tableId)) {
                    VoltDB.crashLocalVoltDB("No schema for table with ID " + tableId,
                                            false, null);
                }

                // Get the byte buffer ready to be consumed
                block.position(StreamSnapshotDataTarget.contentOffset);
                ByteBuffer nextChunk = getNextChunk(m_schemas.get(tableId), block, resultBufferAllocator);
                m_bytesReceived += nextChunk.remaining();

                restoreWork = new TableRestoreWork(tableId, nextChunk);
            }

            // Queue ack to this block
            m_ack.ack(hsId, m_EOF, targetId, blockIndex);

            return restoreWork;
        } finally {
            container.discard();
        }
    }
View Full Code Here


                    System.out.println("Checksum " + calculatedChecksum + " didn't match " + checksum);
                    System.exit(-1);
                }
            }
        }
        final BBContainer cont = DBBPool.wrapBB(buf);
        DBBPool.registerUnsafeMemory(cont.address());
        cont.discard();
    }
View Full Code Here

        assertions();
        if (m_closed) {
            throw new IOException("Closed");
        }

        BBContainer retcont = null;
        PBDSegment segment = m_segments.peek();
        if (segment.hasMoreEntries()) {
            retcont = segment.poll(ocf);
        } else {
            for (PBDSegment s : m_segments) {
                if (s.hasMoreEntries()) {
                    segment = s;
                    retcont = segment.poll(ocf);
                    break;
                }
            }
        }
        if (retcont == null) {
            return null;
        }

        decrementNumObjects();
        assertions();
        assert (retcont.b() != null);
        return wrapRetCont(segment, retcont);
    }
View Full Code Here

        assert (retcont.b() != null);
        return wrapRetCont(segment, retcont);
    }

    private BBContainer wrapRetCont(final PBDSegment segment, final BBContainer retcont) {
        return new BBContainer(retcont.b()) {
            private boolean m_discarded = false;
            @Override
            public void discard() {
                checkDoubleFree();
                if (m_discarded) {
View Full Code Here

        /*
         * Iterator all the objects in all the segments and pass them to the truncator
         * When it finds the truncation point
         */
        Long lastSegmentIndex = null;
        BBContainer decompressionBuffer = DBBPool.allocateDirect(1024 * 512);
        try {
            for (PBDSegment segment : m_segments) {
                long segmentIndex = segment.m_index;

                File segmentFile = segment.m_file;
                RandomAccessFile ras = new RandomAccessFile(segmentFile, "rw");
                FileChannel fc = ras.getChannel();
                MBBContainer readBufferC = DBBPool.wrapMBB(fc.map(MapMode.READ_WRITE, 0, fc.size()));
                final ByteBuffer readBuffer = readBufferC.b();
                final long buffAddr = readBufferC.address();
                try {
                    //Get the number of objects and then iterator over them
                    int numObjects = readBuffer.getInt();
                    int size = readBuffer.getInt();
                    int objectsProcessed = 0;
                    exportLog.debug("PBD " + m_nonce + " has " + numObjects + " objects to parse and truncate");
                    for (int ii = 0; ii < numObjects; ii++) {
                        final int nextObjectLength = readBuffer.getInt();
                        final int nextObjectFlags = readBuffer.getInt();
                        final boolean compressed = nextObjectFlags == PBDSegment.FLAG_COMPRESSED;
                        final int uncompressedLength = compressed ? (int)Snappy.uncompressedLength(buffAddr + readBuffer.position(), nextObjectLength) : nextObjectLength;
                        objectsProcessed++;
                        //Copy the next object into a separate heap byte buffer
                        //do the old limit stashing trick to avoid buffer overflow
                        BBContainer nextObject = null;
                        if (compressed) {
                            decompressionBuffer.b().clear();
                            if (decompressionBuffer.b().remaining() < uncompressedLength ) {
                                decompressionBuffer.discard();
                                decompressionBuffer = DBBPool.allocateDirect(uncompressedLength);
                            }
                            nextObject = DBBPool.dummyWrapBB(decompressionBuffer.b());
                            final long sourceAddr = (buffAddr + readBuffer.position());
                            final long destAddr = nextObject.address();
                            Snappy.rawUncompress(sourceAddr, nextObjectLength, destAddr);
                            readBuffer.position(readBuffer.position() + nextObjectLength);
                        } else {
                            final int oldLimit = readBuffer.limit();
                            readBuffer.limit(readBuffer.position() + nextObjectLength);
                            nextObject = DBBPool.dummyWrapBB(readBuffer.slice());
                            readBuffer.position(readBuffer.limit());
                            readBuffer.limit(oldLimit);
                        }
                        try {
                            //Handoff the object to the truncator and await a decision
                            ByteBuffer retval = truncator.parse(nextObject.b());
                            if (retval == null) {
                                //Nothing to do, leave the object alone and move to the next
                                continue;
                            } else {
                                //If the returned bytebuffer is empty, remove the object and truncate the file
                                if (retval.remaining() == 0) {
                                    if (ii == 0) {
                                        /*
                                         * If truncation is occuring at the first object
                                         * Whammo! Delete the file. Do it by setting the lastSegmentIndex
                                         * to 1 previous. We may end up with an empty finished segment
                                         * set.
                                         */
                                        lastSegmentIndex = segmentIndex - 1;
                                    } else {
                                        addToNumObjects(-(numObjects - (objectsProcessed - 1)));
                                        //Don't forget to update the number of entries in the file
                                        ByteBuffer numObjectsBuffer = ByteBuffer.allocate(4);
                                        numObjectsBuffer.putInt(0, ii);
                                        fc.position(0);
                                        while (numObjectsBuffer.hasRemaining()) {
                                            fc.write(numObjectsBuffer);
                                        }
                                        fc.truncate(readBuffer.position() - (nextObjectLength + PBDSegment.m_objectHeaderBytes));
                                    }

                                } else {
                                    addToNumObjects(-(numObjects - objectsProcessed));
                                    //Partial object truncation
                                    ByteBuffer copy = ByteBuffer.allocate(retval.remaining());
                                    copy.put(retval);
                                    copy.flip();
                                    readBuffer.position(readBuffer.position() - (nextObjectLength + PBDSegment.m_objectHeaderBytes));
                                    readBuffer.putInt(copy.remaining());
                                    readBuffer.putInt(0);
                                    readBuffer.put(copy);

                                    readBuffer.putInt(0, ii + 1);

                                    /*
                                     * SHOULD REALLY make a copy of the original and then swap them with renaming
                                     */
                                    fc.truncate(readBuffer.position());
                                }
                                //Set last segment and break the loop over this segment
                                if (lastSegmentIndex == null) {
                                    lastSegmentIndex = segmentIndex;
                                }
                                break;
                            }
                        } finally {
                            nextObject.discard();
                        }
                    }

                    //If this is set the just processed segment was the last one
                    if (lastSegmentIndex != null) {
View Full Code Here

    private static final VoltLogger LOG = new VoltLogger("HOST");

    public static class UnsafeOutputContainerFactory implements OutputContainerFactory {
        @Override
        public BBContainer getContainer(int minimumSize) {
              final BBContainer origin = DBBPool.allocateUnsafeByteBuffer(minimumSize);
              final BBContainer retcont = new BBContainer(origin.b()) {
                  private boolean discarded = false;

                  @Override
                  public synchronized void discard() {
                      final ByteBuffer buf = checkDoubleFree();
View Full Code Here

        final int nextUncompressedLength = compressed ? (int)Snappy.uncompressedLength(mBufAddr + m_readBuf.position(), nextCompressedLength) : nextCompressedLength;
        m_bytesRead += nextUncompressedLength;

        if (compressed) {
            //Get storage for output
            final BBContainer retcont = factory.getContainer(nextUncompressedLength);
            final ByteBuffer retbuf = retcont.b();

            //Limit to appropriate uncompressed size
            retbuf.limit(nextUncompressedLength);

            //Uncompress to output buffer
            final long sourceAddr = mBufAddr + m_readBuf.position();
            final long destAddr = retcont.address();
            Snappy.rawUncompress(sourceAddr, nextCompressedLength, destAddr);
            m_readBuf.position(m_readBuf.position() + nextCompressedLength);
            return retcont;
        } else {
            //Return a slice
            final int oldLimit = m_readBuf.limit();
            m_readBuf.limit(m_readBuf.position() + nextUncompressedLength);
            ByteBuffer retbuf = m_readBuf.slice();
            m_readBuf.position(m_readBuf.limit());
            m_readBuf.limit(oldLimit);

            /*
             * For uncompressed data, touch all the pages to make 100% sure
             * they are available since they will be accessed directly.
             *
             * This code mimics MappedByteBuffer.load, but without the expensive
             * madvise call for data we are 99% sure was already madvised.
             *
             * This would only ever be an issue in the unlikely event that the page cache
             * is trashed at the wrong moment or we are very low on memory
             */
            final BBContainer dummyCont = DBBPool.dummyWrapBB(retbuf);
            Bits.readEveryPage(dummyCont);
            return dummyCont;
        }
    }
View Full Code Here

            @Override
            public BBContainer take() throws InterruptedException {
                final Semaphore permits = m_permits.get(bufLenInBytes);
                permits.acquire();
                final BBContainer origin = DBBPool.allocateDirectAndPool(bufLenInBytes);
                return new BBContainer(origin.b()) {
                    @Override
                    public void discard() {
                        checkDoubleFree();
                        permits.release();
                        origin.discard();
                    }
                };
            }

            @Override
View Full Code Here

                    FileInputStream fis = new FileInputStream(f);
                    FileOutputStream fos = new FileOutputStream(fInOtherSubroot);
                    FileChannel inputChannel = fis.getChannel();
                    FileChannel outputChannel = fos.getChannel();
                    BBContainer bufC = DBBPool.allocateDirect(8192);
                    ByteBuffer buf = bufC.b();

                    try {
                        while (inputChannel.read(buf) != -1) {
                            buf.flip();
                            outputChannel.write(buf);
                            buf.clear();
                        }
                        inputChannel.close();
                        outputChannel.close();
                    } finally {
                        bufC.discard();
                    }
                } else {
                    throw new IOException(fInOtherSubroot + " already exists");
                }
            }
View Full Code Here

     * a reference to the block to the in memory deque unless actuallyPoll is true.
     * @param actuallyPoll
     * @return
     */
    private StreamBlock pollPersistentDeque(boolean actuallyPoll) {
        BBContainer cont = null;
        try {
            cont = m_persistentDeque.poll(PersistentBinaryDeque.UNSAFE_CONTAINER_FACTORY);
        } catch (IOException e) {
            exportLog.error(e);
        }

        if (cont == null) {
            return null;
        } else {
            //If the container is not null, unpack it.
            final BBContainer fcont = cont;
            long uso = cont.b().getLong(0);
            //Pass the stream block a subset of the bytes, provide
            //a container that discards the original returned by the persistent deque
            StreamBlock block = new StreamBlock( fcont,
                uso,
View Full Code Here

TOP

Related Classes of org.voltcore.utils.DBBPool.BBContainer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.