Search in sources :

Example 1 with CacheKey

use of org.teiid.common.buffer.CacheKey in project teiid by teiid.

the class BufferFrontedFileStoreCache method shouldPlaceInMemoryBuffer.

/**
 * Determine if an object should be in the memory buffer.
 * @param currentTime
 * @param info
 * @return
 */
private boolean shouldPlaceInMemoryBuffer(PhysicalInfo info) {
    if (info.evicting || info.inode != EMPTY_ADDRESS) {
        return false;
    }
    if (info.block == EMPTY_ADDRESS) {
        return true;
    }
    PhysicalInfo lowest = memoryBufferEntries.firstEntry(false);
    CacheKey key = info.getKey();
    return (blocksInuse.getTotalBits() - blocksInuse.getBitsSet()) > (cleaningThreshold + info.memoryBlockCount) || (lowest != null && lowest.block != EMPTY_ADDRESS && lowest.getKey().getOrderingValue() < key.getOrderingValue());
}
Also used : CacheKey(org.teiid.common.buffer.CacheKey)

Example 2 with CacheKey

use of org.teiid.common.buffer.CacheKey in project teiid by teiid.

the class BufferFrontedFileStoreCache method get.

@Override
public CacheEntry get(PhysicalInfo info, Long oid, WeakReference<? extends Serializer<?>> ref) throws TeiidComponentException {
    if (info == null) {
        return null;
    }
    Serializer<?> serializer = ref.get();
    if (serializer == null) {
        return null;
    }
    readAttempts.incrementAndGet();
    InputStream is = null;
    Lock lock = null;
    ExtensibleBufferedInputStream eis = null;
    int memoryBlocks = 0;
    try {
        synchronized (info) {
            // load should be locked
            assert !info.pinned && info.loading;
            // not necessary, but should make things safer
            info.await(true, false);
            if (info.inode != EMPTY_ADDRESS) {
                info.pinned = true;
                memoryBufferEntries.touch(info);
                if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.DETAIL)) {
                    // $NON-NLS-1$
                    LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "Getting object at inode", info.inode, serializer.getId(), oid);
                }
                BlockManager manager = getBlockManager(serializer.getId(), oid, info.inode);
                is = new BlockInputStream(manager, info.memoryBlockCount);
            } else if (info.block != EMPTY_ADDRESS) {
                info.pinned = true;
                memoryBufferEntries.recordAccess(info);
                storageReads.incrementAndGet();
                if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.DETAIL)) {
                    // $NON-NLS-1$
                    LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "Getting object at block", info.block, info.sizeIndex, serializer.getId(), oid);
                }
                BlockStore blockStore = sizeBasedStores[info.sizeIndex];
                int segment = info.block / blockStore.blocksInUse.getBitsPerSegment();
                FileStore fs = blockStore.stores[segment];
                long blockOffset = (info.block % blockStore.blocksInUse.getBitsPerSegment()) * blockStore.blockSize;
                eis = fs.createInputStream(blockOffset, info.memoryBlockCount << LOG_BLOCK_SIZE);
                lock = blockStore.locks[segment].writeLock();
                memoryBlocks = info.memoryBlockCount;
            } else {
                return null;
            }
        }
        if (lock != null) {
            is = readIntoMemory(info, eis, lock, memoryBlocks);
        }
        for (int i = 0; i < HEADER_BYTES; i++) {
            is.read();
        }
        ObjectInput dis = new ObjectInputStream(is);
        CacheEntry ce = new CacheEntry(new CacheKey(oid, 1, 1), info.sizeEstimate, serializer.deserialize(dis), ref, true);
        return ce;
    } catch (IOException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30048, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30048, info.gid, oid));
    } catch (ClassNotFoundException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30048, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30048, info.gid, oid));
    } catch (InterruptedException e) {
        throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30049, e);
    } finally {
        synchronized (info) {
            info.pinned = false;
            info.notifyAll();
        }
    }
}
Also used : ObjectInputStream(java.io.ObjectInputStream) ExtensibleBufferedInputStream(org.teiid.common.buffer.ExtensibleBufferedInputStream) InputStream(java.io.InputStream) ExtensibleBufferedInputStream(org.teiid.common.buffer.ExtensibleBufferedInputStream) IOException(java.io.IOException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) CacheEntry(org.teiid.common.buffer.CacheEntry) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) FileStore(org.teiid.common.buffer.FileStore) TeiidComponentException(org.teiid.core.TeiidComponentException) ObjectInput(java.io.ObjectInput) CacheKey(org.teiid.common.buffer.CacheKey) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with CacheKey

use of org.teiid.common.buffer.CacheKey in project teiid by teiid.

the class LrfuEvictionQueue method toString.

@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    // $NON-NLS-1$ //$NON-NLS-2$
    result.append("Size:").append(getSize()).append(" ");
    int max = 2000;
    for (CacheKey e : evictionQueue.keySet()) {
        // $NON-NLS-1$ //$NON-NLS-2$
        result.append("(").append(e.getOrderingValue()).append(", ").append(e.getLastAccess()).append(", ").append(// $NON-NLS-1$
        e.getId()).append(// $NON-NLS-1$
        ") ");
        if (--max == 0) {
            // $NON-NLS-1$
            result.append("...");
        }
    }
    return result.toString();
}
Also used : CacheKey(org.teiid.common.buffer.CacheKey)

Example 4 with CacheKey

use of org.teiid.common.buffer.CacheKey in project teiid by teiid.

the class LrfuEvictionQueue method recordAccess.

/**
 * Callers should be synchronized on value
 */
void recordAccess(V value) {
    CacheKey key = value.getKey();
    long lastAccess = key.getLastAccess();
    long currentClock = clock.get();
    long orderingValue = key.getOrderingValue();
    orderingValue = computeNextOrderingValue(currentClock, lastAccess, orderingValue);
    assert !this.evictionQueue.containsKey(value.getKey());
    value.setKey(new CacheKey(key.getId(), currentClock, orderingValue));
}
Also used : CacheKey(org.teiid.common.buffer.CacheKey)

Example 5 with CacheKey

use of org.teiid.common.buffer.CacheKey in project teiid by teiid.

the class TestBufferFrontedFileStoreCache method testMultipleAdds.

@Test
public void testMultipleAdds() throws Exception {
    cache = createLayeredCache(1 << 18, 1 << 18, true, true);
    Serializer<Integer> s = new SimpleSerializer() {

        @Override
        public void serialize(Integer obj, ObjectOutput oos) throws IOException {
            throw new IOException();
        }
    };
    CacheEntry ce = new CacheEntry(new CacheKey(31l, 0, 0), 1000000, null, null, false);
    cache.createCacheGroup(s.getId());
    Integer cacheObject = Integer.valueOf(50000);
    ce.setObject(cacheObject);
    cache.addToCacheGroup(s.getId(), ce.getId());
    assertTrue(cache.add(ce, s));
    s = new SimpleSerializer();
    assertTrue(cache.add(ce, s));
    assertNotNull(get(cache, ce.getId(), s));
}
Also used : ObjectOutput(java.io.ObjectOutput) IOException(java.io.IOException) CacheEntry(org.teiid.common.buffer.CacheEntry) CacheKey(org.teiid.common.buffer.CacheKey) Test(org.junit.Test)

Aggregations

CacheKey (org.teiid.common.buffer.CacheKey)7 Test (org.junit.Test)3 IOException (java.io.IOException)2 CacheEntry (org.teiid.common.buffer.CacheEntry)2 InputStream (java.io.InputStream)1 ObjectInput (java.io.ObjectInput)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutput (java.io.ObjectOutput)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 BaseCacheEntry (org.teiid.common.buffer.BaseCacheEntry)1 ExtensibleBufferedInputStream (org.teiid.common.buffer.ExtensibleBufferedInputStream)1 FileStore (org.teiid.common.buffer.FileStore)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1