一、综述
在《HBase-1.2.4 Allow block cache to be external分析》一文的最后,讲解了如何实例化外部缓存MemcachedBlockCache。本文将对上文中提到的几种缓存中的InclusiveCombinedBlockCache和CombinedBlockCache做个综述。
上文中提到的缓存实现,有以下几种:
1、单独LruBlockCache
2、启用外部缓存:InclusiveCombinedBlockCache
3、启用混合缓存CombinedBlockCache
二、CombinedBlockCache
CombinedBlockCache是一个混合缓存,它是由LruBlockCache和另外一种BlockCache实现的。它的cacheBlock()方法,会根据参数inMemory和cacheDataInL1来确定缓存的地址是LruBlockCache还是另外一种BlockCache,如下:
@Override
public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory,
final boolean cacheDataInL1) {
boolean isMetaBlock = buf.getBlockType().getCategory() != BlockCategory.DATA;
if (isMetaBlock || cacheDataInL1) {
lruCache.cacheBlock(cacheKey, buf, inMemory, cacheDataInL1);
} else {
l2Cache.cacheBlock(cacheKey, buf, inMemory, false);
}
}
而获取缓存时,则是先从LruBlockCache中获取,获取不到时再从另外一种BlockCache中获取,如下:
@Override
public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching,
boolean repeat, boolean updateCacheMetrics) {
// TODO: is there a hole here, or just awkwardness since in the lruCache getBlock
// we end up calling l2Cache.getBlock.
if (lruCache.containsBlock(cacheKey)) {
return lruCache.getBlock(cacheKey, caching, repeat, updateCacheMetrics);
}
Cacheable result = l2Cache.getBlock(cacheKey, caching, repeat, updateCacheMetrics);
return result;
}
回收缓存亦是如此,如下:
@Override
public boolean evictBlock(BlockCacheKey cacheKey) {
return lruCache.evictBlock(cacheKey) || l2Cache.evictBlock(cacheKey);
}
三、InclusiveCombinedBlockCache
InclusiveCombinedBlockCache是外部缓存的集中体现,它继承自CombinedBlockCache,但是对于缓存及其获取做了特别的声明,如下:
1、获取缓存时从lruCache中获取,因为启用外部缓存时会同时写入lru和外部缓存,并且,lru有一种机制,即存在victimHandler且!repeat的情况下,会尝试从另外一种缓存中获取;
2、缓存时则是lruCache和l2Cache同时缓存。