diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerWrapperImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerWrapperImpl.java index 22f7af7e2be..29e42f5c699 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerWrapperImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerWrapperImpl.java @@ -25,6 +25,7 @@ import org.apache.hadoop.hbase.CompatibilitySingletonFactory; import org.apache.hadoop.hbase.HDFSBlocksDistribution; import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.io.hfile.BlockCache; +import org.apache.hadoop.hbase.io.hfile.CacheConfig; import org.apache.hadoop.hbase.io.hfile.CacheStats; import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; @@ -46,7 +47,8 @@ class MetricsRegionServerWrapperImpl public static final int PERIOD = 15; private final HRegionServer regionServer; - private final BlockCache blockCache; + + private BlockCache blockCache; private volatile long numStores = 0; private volatile long numStoreFiles = 0; @@ -70,13 +72,29 @@ class MetricsRegionServerWrapperImpl public MetricsRegionServerWrapperImpl(final HRegionServer regionServer) { this.regionServer = regionServer; - this.blockCache = this.regionServer.cacheConfig.getBlockCache(); - this.cacheStats = blockCache.getStats(); + initBlockCache(); + this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor(); this.runnable = new RegionServerMetricsWrapperRunnable(); this.executor.scheduleWithFixedDelay(this.runnable, PERIOD, PERIOD, TimeUnit.SECONDS); } + /** + * It's possible that due to threading the block cache could not be initialized + * yet (testing multiple region servers in one jvm). So we need to try and initialize + * the blockCache and cacheStats reference multiple times until we succeed. + */ + private synchronized void initBlockCache() { + CacheConfig cacheConfig = this.regionServer.cacheConfig; + if (cacheConfig != null && this.blockCache == null) { + this.blockCache = cacheConfig.getBlockCache(); + } + + if (this.blockCache != null && this.cacheStats == null) { + this.cacheStats = blockCache.getStats(); + } + } + @Override public String getClusterId() { return regionServer.getClusterId(); @@ -309,7 +327,7 @@ class MetricsRegionServerWrapperImpl @Override synchronized public void run() { - + initBlockCache(); cacheStats = blockCache.getStats(); HDFSBlocksDistribution hdfsBlocksDistribution = diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperImpl.java index 64d570dcac4..ea013565eed 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperImpl.java @@ -19,8 +19,11 @@ package org.apache.hadoop.hbase.regionserver; import org.apache.hadoop.hbase.CompatibilitySingletonFactory; +import org.apache.hadoop.hbase.HRegionInfo; +import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.metrics2.MetricsExecutor; +import java.util.Map; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -44,17 +47,29 @@ public class MetricsRegionWrapperImpl implements MetricsRegionWrapper { @Override public String getTableName() { - return this.region.getTableDesc().getNameAsString(); + HTableDescriptor tableDesc = this.region.getTableDesc(); + if (tableDesc == null) { + return ""; + } + return tableDesc.getNameAsString(); } @Override public String getRegionName() { - return this.region.getRegionInfo().getEncodedName(); + HRegionInfo regionInfo = this.region.getRegionInfo(); + if (regionInfo == null) { + return ""; + } + return regionInfo.getEncodedName(); } @Override public long getNumStores() { - return this.region.stores.size(); + Map stores = this.region.stores; + if (stores == null) { + return 0; + } + return stores.size(); } @Override @@ -90,10 +105,12 @@ public class MetricsRegionWrapperImpl implements MetricsRegionWrapper { long tempMemstoreSize = 0; long tempStoreFileSize = 0; - for (Store store : region.stores.values()) { - tempNumStoreFiles += store.getStorefilesCount(); - tempMemstoreSize += store.getMemStoreSize(); - tempStoreFileSize += store.getStorefilesSize(); + if (region.stores != null) { + for (Store store : region.stores.values()) { + tempNumStoreFiles += store.getStorefilesCount(); + tempMemstoreSize += store.getMemStoreSize(); + tempStoreFileSize += store.getStorefilesSize(); + } } numStoreFiles = tempNumStoreFiles;