@Before public void setUp() throws Exception { BlockCacheKey cacheKey = new BlockCacheKey("name", 0); BlockCacheKey otherKey = new BlockCacheKey("name2", 1); Cacheable cacheable = Mockito.mock(Cacheable.class); Cacheable otheCacheable = Mockito.mock(Cacheable.class); block = new LruCachedBlock(cacheKey, cacheable, 0); blockEqual = new LruCachedBlock(otherKey, otheCacheable, 0); blockNotEqual = new LruCachedBlock(cacheKey, cacheable, 1); }
boolean found = map.remove(block.getCacheKey()) != null; if (!found) { return 0; assertCounterSanity(size, val); if (block.getBuffer().getBlockType().isData()) { dataBlockElements.decrement(); stats.evicted(block.getCachedTime(), block.getCacheKey().isPrimary()); if (victimHandler != null) { victimHandler.cacheBlock(block.getCacheKey(), block.getBuffer()); return block.heapSize();
/** * Attempt to add the specified cached block to this queue. * * <p>If the queue is smaller than the max size, or if the specified element * is ordered before the smallest element in the queue, the element will be * added to the queue. Otherwise, there is no side effect of this call. * @param cb block to try to add to the queue */ public void add(LruCachedBlock cb) { if(heapSize < maxSize) { queue.add(cb); heapSize += cb.heapSize(); } else { LruCachedBlock head = queue.peek(); if(cb.compareTo(head) > 0) { heapSize += cb.heapSize(); heapSize -= head.heapSize(); if(heapSize > maxSize) { queue.poll(); } else { heapSize += head.heapSize(); } queue.add(cb); } } }
/** * Helper function that updates the local size counter and also updates any * per-cf or per-blocktype metrics it can discern from given * {@link LruCachedBlock} */ private long updateSizeMetrics(LruCachedBlock cb, boolean evict) { long heapsize = cb.heapSize(); BlockType bt = cb.getBuffer().getBlockType(); if (evict) { heapsize *= -1; } if (bt != null && bt.isData()) { dataBlockSize.add(heapsize); } return size.addAndGet(heapsize); }
if (cb != null) { if (compare(buf, cb.getBuffer()) != 0) { throw new RuntimeException("Cached block contents differ, which should not have happened." + "cacheKey:" + cacheKey); String msg = "Cached an already cached block: " + cacheKey + " cb:" + cb.getCacheKey(); msg += ". This is harmless and can happen in rare cases (see HBASE-8547)"; LOG.warn(msg); return; cb = new LruCachedBlock(cacheKey, buf, count.incrementAndGet(), inMemory); long newSize = updateSizeMetrics(cb, false); map.put(cacheKey, cb);
@VisibleForTesting public Map<DataBlockEncoding, Integer> getEncodingCountsForTest() { Map<DataBlockEncoding, Integer> counts = new EnumMap<>(DataBlockEncoding.class); for (LruCachedBlock block : map.values()) { DataBlockEncoding encoding = ((HFileBlock) block.getBuffer()).getDataBlockEncoding(); Integer count = counts.get(encoding); counts.put(encoding, (count == null ? 0 : count) + 1); } return counts; }
assertEquals(queue.pollLast().getCacheKey().getHfileName(), "cb"+i);
public void add(LruCachedBlock block) { totalSize += block.heapSize(); queue.add(block); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } LruCachedBlock other = (LruCachedBlock) obj; return compareTo(other) == 0; }
switch (cachedBlock.getPriority()) { case SINGLE: { bucketSingle.add(cachedBlock);
@VisibleForTesting Map<BlockType, Integer> getBlockTypeCountsForTest() { Map<BlockType, Integer> counts = new EnumMap<>(BlockType.class); for (LruCachedBlock cb : map.values()) { BlockType blockType = cb.getBuffer().getBlockType(); Integer count = counts.get(blockType); counts.put(blockType, (count == null ? 0 : count) + 1); } return counts; }
assertEquals(queue.pollLast().getCacheKey().getHfileName(), "cb"+i);
/** * Attempt to add the specified cached block to this queue. * * <p>If the queue is smaller than the max size, or if the specified element * is ordered before the smallest element in the queue, the element will be * added to the queue. Otherwise, there is no side effect of this call. * @param cb block to try to add to the queue */ public void add(LruCachedBlock cb) { if(heapSize < maxSize) { queue.add(cb); heapSize += cb.heapSize(); } else { LruCachedBlock head = queue.peek(); if(cb.compareTo(head) > 0) { heapSize += cb.heapSize(); heapSize -= head.heapSize(); if(heapSize > maxSize) { queue.poll(); } else { heapSize += head.heapSize(); } queue.add(cb); } } }
/** * Helper function that updates the local size counter and also updates any * per-cf or per-blocktype metrics it can discern from given * {@link LruCachedBlock} * * @param cb * @param evict */ protected long updateSizeMetrics(LruCachedBlock cb, boolean evict) { long heapsize = cb.heapSize(); if (evict) { heapsize *= -1; } return size.addAndGet(heapsize); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } LruCachedBlock other = (LruCachedBlock) obj; return compareTo(other) == 0; }
switch(cachedBlock.getPriority()) { case SINGLE: { bucketSingle.add(cachedBlock);
/** * Evict the block, and it will be cached by the victim handler if exists && * block may be read again later * @param block * @param evictedByEvictionProcess true if the given block is evicted by * EvictionThread * @return the heap size of evicted block */ protected long evictBlock(LruCachedBlock block, boolean evictedByEvictionProcess) { map.remove(block.getCacheKey()); updateSizeMetrics(block, true); long val = elements.decrementAndGet(); if (LOG.isTraceEnabled()) { long size = map.size(); assertCounterSanity(size, val); } stats.evicted(block.getCachedTime(), block.getCacheKey().isPrimary()); if (evictedByEvictionProcess && victimHandler != null) { if (victimHandler instanceof BucketCache) { boolean wait = getCurrentSize() < acceptableSize(); boolean inMemory = block.getPriority() == BlockPriority.MEMORY; ((BucketCache)victimHandler).cacheBlockWithWait(block.getCacheKey(), block.getBuffer(), inMemory, wait); } else { victimHandler.cacheBlock(block.getCacheKey(), block.getBuffer()); } } return block.heapSize(); }