7272import org .apache .hadoop .hbase .util .EnvironmentEdgeManager ;
7373import org .apache .hadoop .hbase .util .HasThread ;
7474import org .apache .hadoop .hbase .util .IdReadWriteLock ;
75- import org .apache .hadoop .hbase .util .IdReadWriteLock .ReferenceType ;
75+ import org .apache .hadoop .hbase .util .IdReadWriteLockStrongRef ;
76+ import org .apache .hadoop .hbase .util .IdReadWriteLockWithObjectPool ;
77+ import org .apache .hadoop .hbase .util .IdReadWriteLockWithObjectPool .ReferenceType ;
7678import org .apache .hadoop .util .StringUtils ;
7779import org .apache .yetus .audience .InterfaceAudience ;
7880import org .slf4j .Logger ;
@@ -113,6 +115,10 @@ public class BucketCache implements BlockCache, HeapSize {
113115 static final String ACCEPT_FACTOR_CONFIG_NAME = "hbase.bucketcache.acceptfactor" ;
114116 static final String MIN_FACTOR_CONFIG_NAME = "hbase.bucketcache.minfactor" ;
115117
118+ /** Use strong reference for offsetLock or not */
119+ private static final String STRONG_REF_KEY = "hbase.bucketcache.offsetlock.usestrongref" ;
120+ private static final boolean STRONG_REF_DEFAULT = false ;
121+
116122 /** Priority buckets */
117123 @ VisibleForTesting
118124 static final float DEFAULT_SINGLE_FACTOR = 0.25f ;
@@ -199,10 +205,9 @@ public class BucketCache implements BlockCache, HeapSize {
199205 * A ReentrantReadWriteLock to lock on a particular block identified by offset.
200206 * The purpose of this is to avoid freeing the block which is being read.
201207 * <p>
202- * Key set of offsets in BucketCache is limited so soft reference is the best choice here.
203208 */
204209 @ VisibleForTesting
205- transient final IdReadWriteLock <Long > offsetLock = new IdReadWriteLock <>( ReferenceType . SOFT ) ;
210+ transient final IdReadWriteLock <Long > offsetLock ;
206211
207212 private final NavigableSet <BlockCacheKey > blocksByHFile = new ConcurrentSkipListSet <>((a , b ) -> {
208213 int nameComparison = a .getHfileName ().compareTo (b .getHfileName ());
@@ -257,6 +262,12 @@ public BucketCache(String ioEngineName, long capacity, int blockSize, int[] buck
257262 public BucketCache (String ioEngineName , long capacity , int blockSize , int [] bucketSizes ,
258263 int writerThreadNum , int writerQLen , String persistencePath , int ioErrorsTolerationDuration ,
259264 Configuration conf ) throws IOException {
265+ boolean useStrongRef = conf .getBoolean (STRONG_REF_KEY , STRONG_REF_DEFAULT );
266+ if (useStrongRef ) {
267+ this .offsetLock = new IdReadWriteLockStrongRef <>();
268+ } else {
269+ this .offsetLock = new IdReadWriteLockWithObjectPool <>(ReferenceType .SOFT );
270+ }
260271 this .algorithm = conf .get (FILE_VERIFY_ALGORITHM , DEFAULT_FILE_VERIFY_ALGORITHM );
261272 this .ioEngine = getIOEngineFromName (ioEngineName , capacity , persistencePath );
262273 this .writerThreads = new WriterThread [writerThreadNum ];
@@ -277,7 +288,7 @@ public BucketCache(String ioEngineName, long capacity, int blockSize, int[] buck
277288
278289 LOG .info ("Instantiating BucketCache with acceptableFactor: " + acceptableFactor + ", minFactor: " + minFactor +
279290 ", extraFreeFactor: " + extraFreeFactor + ", singleFactor: " + singleFactor + ", multiFactor: " + multiFactor +
280- ", memoryFactor: " + memoryFactor );
291+ ", memoryFactor: " + memoryFactor + ", useStrongRef: " + useStrongRef );
281292
282293 this .cacheCapacity = capacity ;
283294 this .persistencePath = persistencePath ;
0 commit comments