From a81d9be876004a5d3654b72be31ffb719bab93e8 Mon Sep 17 00:00:00 2001 From: Guanghao Zhang Date: Thu, 11 Oct 2018 19:34:45 +0800 Subject: [PATCH] HBASE-21290 No need to instantiate BlockCache for master which not carry table --- .../hadoop/hbase/io/hfile/CacheConfig.java | 36 +++++++++++++++---- .../hadoop/hbase/mob/MobCacheConfig.java | 5 +++ .../hbase/regionserver/HRegionServer.java | 7 ++-- .../hadoop/hbase/master/TestMaster.java | 7 ++++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java index ac1af916174..147568e1957 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java @@ -48,10 +48,8 @@ public class CacheConfig { /** * Disabled cache configuration */ - public static final CacheConfig DISABLED = new CacheConfig(); - /** * Configuration key to cache data blocks on read. Bloom blocks and index blocks are always be * cached if the block cache is enabled. @@ -247,7 +245,11 @@ public class CacheConfig { * @param conf hbase configuration */ public CacheConfig(Configuration conf) { - this(CacheConfig.instantiateBlockCache(conf), + this(conf, true); + } + + public CacheConfig(Configuration conf, boolean enableBlockCache) { + this(conf, enableBlockCache, conf.getBoolean(CACHE_DATA_ON_READ_KEY, DEFAULT_CACHE_DATA_ON_READ), DEFAULT_IN_MEMORY, // This is a family-level setting so can't be set // strictly from conf @@ -257,11 +259,32 @@ public class CacheConfig { conf.getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE), conf.getBoolean(CACHE_DATA_BLOCKS_COMPRESSED_KEY, DEFAULT_CACHE_DATA_COMPRESSED), conf.getBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, DEFAULT_PREFETCH_ON_OPEN), - conf.getBoolean(DROP_BEHIND_CACHE_COMPACTION_KEY, DROP_BEHIND_CACHE_COMPACTION_DEFAULT) - ); + conf.getBoolean(DROP_BEHIND_CACHE_COMPACTION_KEY, DROP_BEHIND_CACHE_COMPACTION_DEFAULT)); LOG.info("Created cacheConfig: " + this); } + private CacheConfig(Configuration conf, boolean enableBlockCache, + final boolean cacheDataOnRead, final boolean inMemory, + final boolean cacheDataOnWrite, final boolean cacheIndexesOnWrite, + final boolean cacheBloomsOnWrite, final boolean evictOnClose, + final boolean cacheDataCompressed, final boolean prefetchOnOpen, + final boolean dropBehindCompaction) { + if (enableBlockCache) { + this.blockCache = CacheConfig.instantiateBlockCache(conf); + } else { + this.blockCache = null; + } + this.cacheDataOnRead = cacheDataOnRead; + this.inMemory = inMemory; + this.cacheDataOnWrite = cacheDataOnWrite; + this.cacheIndexesOnWrite = cacheIndexesOnWrite; + this.cacheBloomsOnWrite = cacheBloomsOnWrite; + this.evictOnClose = evictOnClose; + this.cacheDataCompressed = cacheDataCompressed; + this.prefetchOnOpen = prefetchOnOpen; + this.dropBehindCompaction = dropBehindCompaction; + } + /** * Create a block cache configuration with the specified cache and configuration parameters. * @param blockCache reference to block cache, null if completely disabled @@ -274,11 +297,10 @@ public class CacheConfig { * @param evictOnClose whether blocks should be evicted when HFile is closed * @param cacheDataCompressed whether to store blocks as compressed in the cache * @param prefetchOnOpen whether to prefetch blocks upon open - * @param cacheDataInL1 If more than one cache tier deployed, if true, cache this column families - * data blocks up in the L1 tier. * @param dropBehindCompaction indicate that we should set drop behind to true when open a store * file reader for compaction */ + @VisibleForTesting CacheConfig(final BlockCache blockCache, final boolean cacheDataOnRead, final boolean inMemory, final boolean cacheDataOnWrite, final boolean cacheIndexesOnWrite, diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/mob/MobCacheConfig.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/mob/MobCacheConfig.java index 2305ebac96f..971bb929630 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/mob/MobCacheConfig.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/mob/MobCacheConfig.java @@ -42,6 +42,11 @@ public class MobCacheConfig extends CacheConfig { instantiateMobFileCache(conf); } + public MobCacheConfig(Configuration conf, boolean needBlockCache) { + super(conf, needBlockCache); + instantiateMobFileCache(conf); + } + /** * Instantiates the MobFileCache. * @param conf The current configuration. diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java index 01d41954151..63a36af2f7c 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java @@ -587,8 +587,11 @@ public class HRegionServer extends HasThread implements Superusers.initialize(conf); regionServerAccounting = new RegionServerAccounting(conf); - cacheConfig = new CacheConfig(conf); - mobCacheConfig = new MobCacheConfig(conf); + boolean isMasterNotCarryTable = + this instanceof HMaster && !LoadBalancer.isTablesOnMaster(conf) && !LoadBalancer + .isSystemTablesOnlyOnMaster(conf); + cacheConfig = new CacheConfig(conf, !isMasterNotCarryTable); + mobCacheConfig = new MobCacheConfig(conf, !isMasterNotCarryTable); uncaughtExceptionHandler = new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java index 50a83d864c1..8423bce09f4 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMaster.java @@ -20,6 +20,7 @@ package org.apache.hadoop.hbase.master; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -233,5 +234,11 @@ public class TestMaster { // Assert lock gets put in place again. assertTrue(fs.exists(hbckLockPath)); } + + @Test + public void testMasterBlockCache() { + // Master not carry table in default, so no need to instantiate block cache, too. + assertNull(TEST_UTIL.getMiniHBaseCluster().getMaster().getCacheConfig().getBlockCache()); + } }