From 7f5fa6d7dc098a63fba85f3878f9abc2959c8daa Mon Sep 17 00:00:00 2001 From: shawkins Date: Mon, 17 Aug 2015 15:46:33 -0400 Subject: [PATCH] TEIID-3640 fixing initialization --- .../common/buffer/impl/BlockByteBuffer.java | 20 ++++++++++++------- .../impl/BufferFrontedFileStoreCache.java | 8 +++----- .../buffer/impl/TestBlockByteBuffer.java | 9 ++++++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/engine/src/main/java/org/teiid/common/buffer/impl/BlockByteBuffer.java b/engine/src/main/java/org/teiid/common/buffer/impl/BlockByteBuffer.java index 72331bc4ee..9ed30f9fdb 100644 --- a/engine/src/main/java/org/teiid/common/buffer/impl/BlockByteBuffer.java +++ b/engine/src/main/java/org/teiid/common/buffer/impl/BlockByteBuffer.java @@ -50,7 +50,7 @@ private static class BlockByteBufferData { * @param blockAddressBits * @param direct */ - public BlockByteBuffer(int segmentAddressBits, int blockCount, int blockAddressBits, boolean direct) { + public BlockByteBuffer(int segmentAddressBits, int blockCount, int blockAddressBits, boolean direct, boolean allocate) { this.data = new BlockByteBufferData(); this.data.segmentAddressBits = segmentAddressBits; this.data.blockAddressBits = blockAddressBits; @@ -58,7 +58,7 @@ public BlockByteBuffer(int segmentAddressBits, int blockCount, int blockAddressB this.data.segmentSize = 1 << this.data.segmentAddressBits; this.data.blockCount = blockCount; long size = ((long)blockCount)<>segmentAddressBits; + int fullSegments = (int)(size>>segmentAddressBits); int lastSegmentSize = (int) (size&(data.segmentSize-1)); int segments = fullSegments; if (lastSegmentSize > 0) { @@ -66,14 +66,20 @@ public BlockByteBuffer(int segmentAddressBits, int blockCount, int blockAddressB } origBuffers = new ByteBuffer[segments]; buffers = new ByteBuffer[segments]; - for (int i = 0; i < fullSegments; i++) { - origBuffers[i] = allocate(data.segmentSize, direct); - } - if (lastSegmentSize > 0) { - origBuffers[fullSegments] = allocate(lastSegmentSize, direct); + if (allocate) { + for (int i = 0; i < fullSegments; i++) { + origBuffers[i] = allocate(data.segmentSize, direct); + } + if (lastSegmentSize > 0) { + origBuffers[fullSegments] = allocate(lastSegmentSize, direct); + } } } + public ByteBuffer[] getBuffers() { + return buffers; + } + private BlockByteBuffer() { } diff --git a/engine/src/main/java/org/teiid/common/buffer/impl/BufferFrontedFileStoreCache.java b/engine/src/main/java/org/teiid/common/buffer/impl/BufferFrontedFileStoreCache.java index 46fa0a9cbf..60f46dd432 100644 --- a/engine/src/main/java/org/teiid/common/buffer/impl/BufferFrontedFileStoreCache.java +++ b/engine/src/main/java/org/teiid/common/buffer/impl/BufferFrontedFileStoreCache.java @@ -569,11 +569,9 @@ void initialize(boolean allocateMemory) throws TeiidComponentException { blocks = (int) Math.min(Integer.MAX_VALUE, (memoryBufferSpace>>LOG_BLOCK_SIZE)*ADDRESSES_PER_BLOCK/(ADDRESSES_PER_BLOCK+1)); inodesInuse = new ConcurrentBitSet(blocks+1, BufferManagerImpl.CONCURRENCY_LEVEL); blocksInuse = new ConcurrentBitSet(blocks, BufferManagerImpl.CONCURRENCY_LEVEL); - if (allocateMemory) { - this.blockByteBuffer = new BlockByteBuffer(30, blocks, LOG_BLOCK_SIZE, direct); - //ensure that we'll run out of blocks first - this.inodeByteBuffer = new BlockByteBuffer(30, blocks+1, LOG_INODE_SIZE, direct); - } + this.blockByteBuffer = new BlockByteBuffer(30, blocks, LOG_BLOCK_SIZE, direct, allocateMemory); + //ensure that we'll run out of blocks first + this.inodeByteBuffer = new BlockByteBuffer(30, blocks+1, LOG_INODE_SIZE, direct, allocateMemory); memoryWritePermits = new Semaphore(blocks); maxMemoryBlocks = Math.min(MAX_DOUBLE_INDIRECT, blocks); maxMemoryBlocks = Math.min(maxMemoryBlocks, (maxStorageObjectSize>>LOG_BLOCK_SIZE) + ((maxStorageObjectSize&BufferFrontedFileStoreCache.BLOCK_MASK)>0?1:0)); diff --git a/engine/src/test/java/org/teiid/common/buffer/impl/TestBlockByteBuffer.java b/engine/src/test/java/org/teiid/common/buffer/impl/TestBlockByteBuffer.java index a3107902f9..4fd2e62cef 100644 --- a/engine/src/test/java/org/teiid/common/buffer/impl/TestBlockByteBuffer.java +++ b/engine/src/test/java/org/teiid/common/buffer/impl/TestBlockByteBuffer.java @@ -22,13 +22,20 @@ package org.teiid.common.buffer.impl; +import static org.junit.Assert.*; + import org.junit.Test; public class TestBlockByteBuffer { @Test public void testBufferSizing() { - BlockByteBuffer bbb = new BlockByteBuffer(4, 100, 4, false); + BlockByteBuffer bbb = new BlockByteBuffer(4, 100, 4, false, true); bbb.getByteBuffer(1); } + @Test public void testLargeSizing() { + BlockByteBuffer bbb = new BlockByteBuffer(30, 1000000, 13, false, false); + assertEquals(8, bbb.getBuffers().length); + } + }