2222import java .lang .ref .WeakReference ;
2323import java .util .EnumMap ;
2424import java .util .Iterator ;
25- import java .util .List ;
2625import java .util .Map ;
2726import java .util .PriorityQueue ;
28- import java .util .SortedSet ;
29- import java .util .TreeSet ;
3027import java .util .concurrent .ConcurrentHashMap ;
31- import java .util .concurrent .Executors ;
32- import java .util .concurrent .ScheduledExecutorService ;
33- import java .util .concurrent .TimeUnit ;
3428import java .util .concurrent .atomic .AtomicLong ;
3529import java .util .concurrent .atomic .LongAdder ;
3630import java .util .concurrent .locks .ReentrantLock ;
4640
4741import org .apache .hbase .thirdparty .com .google .common .base .MoreObjects ;
4842import org .apache .hbase .thirdparty .com .google .common .base .Objects ;
49- import org .apache .hbase .thirdparty .com .google .common .util .concurrent .ThreadFactoryBuilder ;
5043
5144/**
5245 * <b>This realisation improve performance of classical LRU
146139 * Find more information about improvement: https://issues.apache.org/jira/browse/HBASE-23887
147140 */
148141@ InterfaceAudience .Private
149- public class LruAdaptiveBlockCache implements FirstLevelBlockCache {
142+ public class LruAdaptiveBlockCache extends FirstLevelBlockCache {
150143
151144 private static final Logger LOG = LoggerFactory .getLogger (LruAdaptiveBlockCache .class );
152145
@@ -243,11 +236,6 @@ public class LruAdaptiveBlockCache implements FirstLevelBlockCache {
243236 /** Eviction thread */
244237 private transient final EvictionThread evictionThread ;
245238
246- /** Statistics thread schedule pool (for heavy debugging, could remove) */
247- private transient final ScheduledExecutorService scheduleThreadPool =
248- Executors .newScheduledThreadPool (1 , new ThreadFactoryBuilder ()
249- .setNameFormat ("LruAdaptiveBlockCacheStatsExecutor" ).setDaemon (true ).build ());
250-
251239 /** Current size of cache */
252240 private final AtomicLong size ;
253241
@@ -345,7 +333,8 @@ public LruAdaptiveBlockCache(long maxSize, long blockSize, boolean evictionThrea
345333 DEFAULT_MAX_BLOCK_SIZE ,
346334 DEFAULT_LRU_CACHE_HEAVY_EVICTION_COUNT_LIMIT ,
347335 DEFAULT_LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT ,
348- DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT );
336+ DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT ,
337+ STAT_THREAD_ENABLE_DEFAULT );
349338 }
350339
351340 public LruAdaptiveBlockCache (long maxSize , long blockSize ,
@@ -368,7 +357,8 @@ public LruAdaptiveBlockCache(long maxSize, long blockSize,
368357 conf .getLong (LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT ,
369358 DEFAULT_LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT ),
370359 conf .getFloat (LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT ,
371- DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT ));
360+ DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT ),
361+ conf .getBoolean (STAT_THREAD_ENABLE_KEY , STAT_THREAD_ENABLE_DEFAULT ));
372362 }
373363
374364 public LruAdaptiveBlockCache (long maxSize , long blockSize , Configuration conf ) {
@@ -397,12 +387,13 @@ public LruAdaptiveBlockCache(long maxSize, long blockSize, Configuration conf) {
397387 * @param heavyEvictionOverheadCoefficient how aggressive AdaptiveLRU will reduce GC
398388 */
399389 public LruAdaptiveBlockCache (long maxSize , long blockSize , boolean evictionThread ,
400- int mapInitialSize , float mapLoadFactor , int mapConcurrencyLevel ,
401- float minFactor , float acceptableFactor , float singleFactor ,
402- float multiFactor , float memoryFactor , float hardLimitFactor ,
403- boolean forceInMemory , long maxBlockSize ,
404- int heavyEvictionCountLimit , long heavyEvictionMbSizeLimit ,
405- float heavyEvictionOverheadCoefficient ) {
390+ int mapInitialSize , float mapLoadFactor , int mapConcurrencyLevel ,
391+ float minFactor , float acceptableFactor , float singleFactor ,
392+ float multiFactor , float memoryFactor , float hardLimitFactor ,
393+ boolean forceInMemory , long maxBlockSize ,
394+ int heavyEvictionCountLimit , long heavyEvictionMbSizeLimit ,
395+ float heavyEvictionOverheadCoefficient , boolean statEnabled ) {
396+ super (statEnabled );
406397 this .maxBlockSize = maxBlockSize ;
407398 if (singleFactor + multiFactor + memoryFactor != 1 ||
408399 singleFactor < 0 || multiFactor < 0 || memoryFactor < 0 ) {
@@ -446,11 +437,6 @@ public LruAdaptiveBlockCache(long maxSize, long blockSize, boolean evictionThrea
446437 heavyEvictionOverheadCoefficient = Math .min (heavyEvictionOverheadCoefficient , 1.0f );
447438 heavyEvictionOverheadCoefficient = Math .max (heavyEvictionOverheadCoefficient , 0.001f );
448439 this .heavyEvictionOverheadCoefficient = heavyEvictionOverheadCoefficient ;
449-
450- // TODO: Add means of turning this off. Bit obnoxious running thread just to make a log
451- // every five minutes.
452- this .scheduleThreadPool .scheduleAtFixedRate (new StatisticsThread (this ), STAT_THREAD_PERIOD ,
453- STAT_THREAD_PERIOD , TimeUnit .SECONDS );
454440 }
455441
456442 @ Override
@@ -1198,26 +1184,8 @@ boolean isEnteringRun() {
11981184 }
11991185 }
12001186
1201- /*
1202- * Statistics thread. Periodically prints the cache statistics to the log.
1203- */
1204- static class StatisticsThread extends Thread {
1205-
1206- private final LruAdaptiveBlockCache lru ;
1207-
1208- public StatisticsThread (LruAdaptiveBlockCache lru ) {
1209- super ("LruAdaptiveBlockCacheStats" );
1210- setDaemon (true );
1211- this .lru = lru ;
1212- }
1213-
1214- @ Override
1215- public void run () {
1216- lru .logStats ();
1217- }
1218- }
1219-
1220- public void logStats () {
1187+ @ Override
1188+ protected void logStats () {
12211189 // Log size
12221190 long totalSize = heapSize ();
12231191 long freeSize = maxSize - totalSize ;
@@ -1375,26 +1343,10 @@ private long memorySize() {
13751343
13761344 @ Override
13771345 public void shutdown () {
1346+ super .shutdown ();
13781347 if (victimHandler != null ) {
13791348 victimHandler .shutdown ();
13801349 }
1381- this .scheduleThreadPool .shutdown ();
1382- for (int i = 0 ; i < 10 ; i ++) {
1383- if (!this .scheduleThreadPool .isShutdown ()) {
1384- try {
1385- Thread .sleep (10 );
1386- } catch (InterruptedException e ) {
1387- LOG .warn ("Interrupted while sleeping" );
1388- Thread .currentThread ().interrupt ();
1389- break ;
1390- }
1391- }
1392- }
1393-
1394- if (!this .scheduleThreadPool .isShutdown ()) {
1395- List <Runnable > runnables = this .scheduleThreadPool .shutdownNow ();
1396- LOG .debug ("Still running " + runnables );
1397- }
13981350 this .evictionThread .shutdown ();
13991351 }
14001352
0 commit comments