HBASE-22447 Check refCount before free block in BucketCache

This commit is contained in:
binlijin 2019-05-22 16:19:08 +08:00
parent 2c55bd9344
commit e3c27d33a7
1 changed files with 10 additions and 34 deletions

View File

@ -562,37 +562,6 @@ public class BucketCache implements BlockCache, HeapSize {
return evictBlock(cacheKey, true);
}
// does not check for the ref count. Just tries to evict it if found in the
// bucket map
private boolean forceEvict(BlockCacheKey cacheKey) {
if (!cacheEnabled) {
return false;
}
RAMQueueEntry removedBlock = checkRamCache(cacheKey);
BucketEntry bucketEntry = backingMap.get(cacheKey);
if (bucketEntry == null) {
if (removedBlock != null) {
cacheStats.evicted(0, cacheKey.isPrimary());
return true;
} else {
return false;
}
}
ReentrantReadWriteLock lock = offsetLock.getLock(bucketEntry.offset());
try {
lock.writeLock().lock();
if (backingMap.remove(cacheKey, bucketEntry)) {
blockEvicted(cacheKey, bucketEntry, removedBlock == null);
} else {
return false;
}
} finally {
lock.writeLock().unlock();
}
cacheStats.evicted(bucketEntry.getCachedTime(), cacheKey.isPrimary());
return true;
}
private RAMQueueEntry checkRamCache(BlockCacheKey cacheKey) {
RAMQueueEntry removedBlock = ramCache.remove(cacheKey);
if (removedBlock != null) {
@ -1054,8 +1023,15 @@ public class BucketCache implements BlockCache, HeapSize {
ReentrantReadWriteLock lock = offsetLock.getLock(bucketEntries[i].offset());
try {
lock.writeLock().lock();
if (backingMap.remove(key, bucketEntries[i])) {
blockEvicted(key, bucketEntries[i], false);
int refCount = bucketEntries[i].getRefCount();
if (refCount == 0) {
if (backingMap.remove(key, bucketEntries[i])) {
blockEvicted(key, bucketEntries[i], false);
} else {
bucketEntries[i].markForEvict();
}
} else {
bucketEntries[i].markForEvict();
}
} finally {
lock.writeLock().unlock();
@ -1674,7 +1650,7 @@ public class BucketCache implements BlockCache, HeapSize {
if (bucketEntry != null) {
int refCount = bucketEntry.decrementRefCountAndGet();
if (refCount == 0 && bucketEntry.isMarkedForEvict()) {
forceEvict(cacheKey);
evictBlock(cacheKey);
}
}
}