You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by wc...@apache.org on 2023/12/14 21:10:25 UTC
(hbase) branch master updated: HBASE-28246 Expose region cached size over JMX metrics and report in the RS UI (#5565)
This is an automated email from the ASF dual-hosted git repository.
wchevreuil pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hbase.git
The following commit(s) were added to refs/heads/master by this push:
new e8762dfea46 HBASE-28246 Expose region cached size over JMX metrics and report in the RS UI (#5565)
e8762dfea46 is described below
commit e8762dfea4692db600f50e1224bc9cb4f4e09078
Author: Wellington Ramos Chevreuil <wc...@apache.org>
AuthorDate: Thu Dec 14 21:10:18 2023 +0000
HBASE-28246 Expose region cached size over JMX metrics and report in the RS UI (#5565)
Signed-off-by: Peter Somogyi <ps...@apache.org>
---
.../regionserver/MetricsRegionServerSource.java | 2 +
.../regionserver/MetricsRegionSourceImpl.java | 4 +
.../hbase/regionserver/MetricsRegionWrapper.java | 5 ++
.../regionserver/TestMetricsRegionSourceImpl.java | 5 ++
.../hbase/tmpl/regionserver/RegionListTmpl.jamon | 3 +
.../apache/hadoop/hbase/io/hfile/BlockCache.java | 23 +++---
.../hadoop/hbase/io/hfile/BlockCacheKey.java | 15 ++++
.../hadoop/hbase/io/hfile/CombinedBlockCache.java | 11 ++-
.../hadoop/hbase/io/hfile/HFileReaderImpl.java | 2 +-
.../hadoop/hbase/io/hfile/bucket/BucketCache.java | 92 +++++++++++-----------
.../hadoop/hbase/regionserver/HRegionServer.java | 25 +++---
.../regionserver/MetricsRegionWrapperImpl.java | 17 +++-
.../io/hfile/TestPrefetchWithBucketCache.java | 27 ++++++-
.../regionserver/MetricsRegionWrapperStub.java | 5 ++
14 files changed, 164 insertions(+), 72 deletions(-)
diff --git a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSource.java b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSource.java
index a53899c476f..8b64e793bed 100644
--- a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSource.java
+++ b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSource.java
@@ -639,4 +639,6 @@ public interface MetricsRegionServerSource extends BaseSource, JvmPauseMonitorSo
String SCANNER_LEASE_EXPIRED_COUNT = "scannerLeaseExpiredCount";
String SCANNER_LEASE_EXPIRED_COUNT_DESC =
"Count of scanners which were expired due to scanner lease timeout";
+ String CURRENT_REGION_CACHE_RATIO = "currentRegionCacheRatio";
+ String CURRENT_REGION_CACHE_RATIO_DESC = "The percentage of caching completed for this region.";
}
diff --git a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionSourceImpl.java b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionSourceImpl.java
index 410d775d7d3..2f4fbb431ab 100644
--- a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionSourceImpl.java
+++ b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionSourceImpl.java
@@ -233,6 +233,10 @@ public class MetricsRegionSourceImpl implements MetricsRegionSource {
this.regionWrapper.getNumReferenceFiles());
mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
MetricsRegionServerSource.STOREFILE_SIZE_DESC), this.regionWrapper.getStoreFileSize());
+ mrb.addGauge(
+ Interns.info(regionNamePrefix + MetricsRegionServerSource.CURRENT_REGION_CACHE_RATIO,
+ MetricsRegionServerSource.CURRENT_REGION_CACHE_RATIO_DESC),
+ this.regionWrapper.getCurrentRegionCacheRatio());
mrb.addCounter(
Interns.info(regionNamePrefix + MetricsRegionSource.COMPACTIONS_COMPLETED_COUNT,
MetricsRegionSource.COMPACTIONS_COMPLETED_DESC),
diff --git a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapper.java b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapper.java
index 55ddc603979..3445faf7eaa 100644
--- a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapper.java
+++ b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapper.java
@@ -65,6 +65,11 @@ public interface MetricsRegionWrapper {
*/
long getStoreFileSize();
+ /**
+ * Gets the current cache % ratio for this region.
+ */
+ float getCurrentRegionCacheRatio();
+
/**
* Get the total number of read requests that have been issued against this region
*/
diff --git a/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionSourceImpl.java b/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionSourceImpl.java
index 46b6405eb46..28286cc1c6e 100644
--- a/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionSourceImpl.java
+++ b/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionSourceImpl.java
@@ -116,6 +116,11 @@ public class TestMetricsRegionSourceImpl {
return 0;
}
+ @Override
+ public float getCurrentRegionCacheRatio() {
+ return 0;
+ }
+
@Override
public long getReadRequestCount() {
return 0;
diff --git a/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/regionserver/RegionListTmpl.jamon b/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/regionserver/RegionListTmpl.jamon
index 0df4d2763b0..e77318437e0 100644
--- a/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/regionserver/RegionListTmpl.jamon
+++ b/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/regionserver/RegionListTmpl.jamon
@@ -38,6 +38,7 @@
org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos.RegionLoad;
org.apache.hadoop.hbase.client.RegionReplicaUtil;
org.apache.hadoop.hbase.regionserver.MetricsRegionWrapper;
+ org.apache.hadoop.util.StringUtils;
org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix;
</%import>
<%if (onlineRegions != null && onlineRegions.size() > 0) %>
@@ -172,6 +173,7 @@
<th>Bloom Size</th>
<th>Data Locality</th>
<th>Len Of Biggest Cell</th>
+ <th>% Cached</th>
</tr>
</thead>
@@ -237,6 +239,7 @@
<td><% bloomSizeStr %></td>
<td><% load.getDataLocality() %></td>
<td><% String.format("%,1d", lenOfBiggestCellInRegion) %></td>
+ <td><% StringUtils.formatPercent(load.getCurrentRegionCachedRatio(), 2) %></td>
</%if>
</tr>
</%for>
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java
index a62ca853ca6..bed0194b1fa 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java
@@ -169,21 +169,16 @@ public interface BlockCache extends Iterable<CachedBlock> {
/**
* Notifies the cache implementation that the given file has been fully cached (all its blocks
* made into the cache).
- * @param fileName the file that has been completely cached.
+ * @param fileName the file that has been completely cached.
+ * @param totalBlockCount the total of blocks cached for this file.
+ * @param dataBlockCount number of DATA block type cached.
+ * @param size the size, in bytes, cached.
*/
default void notifyFileCachingCompleted(Path fileName, int totalBlockCount, int dataBlockCount,
long size) {
// noop
}
- /**
- * Notifies the cache implementation that the given file had a block evicted
- * @param fileName the file had a block evicted.
- */
- default void notifyFileBlockEvicted(String fileName) {
- // noop
- }
-
/**
* Checks whether there's enough space left in the cache to accommodate the passed block. This
* method may not be overridden by all implementing classes. In such cases, the returned Optional
@@ -245,4 +240,14 @@ public interface BlockCache extends Iterable<CachedBlock> {
default Optional<Map<String, Pair<String, Long>>> getFullyCachedFiles() {
return Optional.empty();
}
+
+ /**
+ * Returns an Optional containing a map of regions and the percentage of how much of it has been
+ * cached so far.
+ * @return empty optional if this method is not supported, otherwise the returned optional
+ * contains a map of current regions caching percentage.
+ */
+ default Optional<Map<String, Long>> getRegionCachedInfo() {
+ return Optional.empty();
+ }
}
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheKey.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheKey.java
index 1cfdc5868be..bf22d38e373 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheKey.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheKey.java
@@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hbase.io.hfile;
+import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.io.HeapSize;
import org.apache.hadoop.hbase.util.ClassSize;
import org.apache.yetus.audience.InterfaceAudience;
@@ -31,6 +32,7 @@ public class BlockCacheKey implements HeapSize, java.io.Serializable {
private final long offset;
private BlockType blockType;
private final boolean isPrimaryReplicaBlock;
+ private Path filePath;
/**
* Construct a new BlockCacheKey
@@ -49,6 +51,14 @@ public class BlockCacheKey implements HeapSize, java.io.Serializable {
this.blockType = blockType;
}
+ public BlockCacheKey(Path hfilePath, long offset, boolean isPrimaryReplica, BlockType blockType) {
+ this.filePath = hfilePath;
+ this.isPrimaryReplicaBlock = isPrimaryReplica;
+ this.hfileName = hfilePath.getName();
+ this.offset = offset;
+ this.blockType = blockType;
+ }
+
@Override
public int hashCode() {
return hfileName.hashCode() * 127 + (int) (offset ^ (offset >>> 32));
@@ -102,4 +112,9 @@ public class BlockCacheKey implements HeapSize, java.io.Serializable {
public void setBlockType(BlockType blockType) {
this.blockType = blockType;
}
+
+ public Path getFilePath() {
+ return filePath;
+ }
+
}
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java
index 427f1771e66..d6692d2e2bf 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java
@@ -439,6 +439,11 @@ public class CombinedBlockCache implements ResizableBlockCache, HeapSize {
return this.l2Cache.getFullyCachedFiles();
}
+ @Override
+ public Optional<Map<String, Long>> getRegionCachedInfo() {
+ return l2Cache.getRegionCachedInfo();
+ }
+
@Override
public void setMaxSize(long size) {
this.l1Cache.setMaxSize(size);
@@ -467,12 +472,6 @@ public class CombinedBlockCache implements ResizableBlockCache, HeapSize {
}
- @Override
- public void notifyFileBlockEvicted(String fileName) {
- l1Cache.notifyFileBlockEvicted(fileName);
- l1Cache.notifyFileBlockEvicted(fileName);
- }
-
@Override
public Optional<Boolean> blockFitsIntoTheCache(HFileBlock block) {
if (isMetaBlock(block.getBlockType())) {
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java
index 9d431428f37..e0f27af7145 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java
@@ -1290,7 +1290,7 @@ public abstract class HFileReaderImpl implements HFile.Reader, Configurable {
// from doing).
BlockCacheKey cacheKey =
- new BlockCacheKey(name, dataBlockOffset, this.isPrimaryReplicaReader(), expectedBlockType);
+ new BlockCacheKey(path, dataBlockOffset, this.isPrimaryReplicaReader(), expectedBlockType);
Attributes attributes = Attributes.of(BLOCK_CACHE_KEY_KEY, cacheKey.toString());
boolean cacheable = cacheBlock && cacheIfCompactionsOff();
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCache.java
index 0d510457260..57f71b31894 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCache.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCache.java
@@ -165,7 +165,7 @@ public class BucketCache implements BlockCache, HeapSize {
* Map of region -> total size of the region prefetched on this region server. This is the total
* size of hFiles for this region prefetched on this region server
*/
- final Map<String, Long> regionCachedSizeMap = new ConcurrentHashMap<>();
+ final Map<String, Long> regionCachedSize = new ConcurrentHashMap<>();
private BucketCachePersister cachePersister;
@@ -348,7 +348,7 @@ public class BucketCache implements BlockCache, HeapSize {
fullyCachedFiles.clear();
backingMapValidated.set(true);
bucketAllocator = new BucketAllocator(capacity, bucketSizes);
- regionCachedSizeMap.clear();
+ regionCachedSize.clear();
}
} else {
bucketAllocator = new BucketAllocator(capacity, bucketSizes);
@@ -645,7 +645,7 @@ public class BucketCache implements BlockCache, HeapSize {
if (decrementBlockNumber) {
this.blockNumber.decrement();
if (ioEngine.isPersistent()) {
- removeFileFromPrefetch(cacheKey.getHfileName());
+ fileNotFullyCached(cacheKey.getHfileName());
}
}
if (evictedByEvictionProcess) {
@@ -656,6 +656,42 @@ public class BucketCache implements BlockCache, HeapSize {
}
}
+ private void fileNotFullyCached(String hfileName) {
+ // Update the regionPrefetchedSizeMap before removing the file from prefetchCompleted
+ if (fullyCachedFiles.containsKey(hfileName)) {
+ Pair<String, Long> regionEntry = fullyCachedFiles.get(hfileName);
+ String regionEncodedName = regionEntry.getFirst();
+ long filePrefetchSize = regionEntry.getSecond();
+ LOG.debug("Removing file {} for region {}", hfileName, regionEncodedName);
+ regionCachedSize.computeIfPresent(regionEncodedName, (rn, pf) -> pf - filePrefetchSize);
+ // If all the blocks for a region are evicted from the cache, remove the entry for that region
+ if (
+ regionCachedSize.containsKey(regionEncodedName)
+ && regionCachedSize.get(regionEncodedName) == 0
+ ) {
+ regionCachedSize.remove(regionEncodedName);
+ }
+ }
+ fullyCachedFiles.remove(hfileName);
+ }
+
+ public void fileCacheCompleted(Path filePath, long size) {
+ Pair<String, Long> pair = new Pair<>();
+ // sets the region name
+ String regionName = filePath.getParent().getParent().getName();
+ pair.setFirst(regionName);
+ pair.setSecond(size);
+ fullyCachedFiles.put(filePath.getName(), pair);
+ }
+
+ private void updateRegionCachedSize(Path filePath, long cachedSize) {
+ if (filePath != null) {
+ String regionName = filePath.getParent().getParent().getName();
+ regionCachedSize.merge(regionName, cachedSize,
+ (previousSize, newBlockSize) -> previousSize + newBlockSize);
+ }
+ }
+
/**
* Free the {{@link BucketEntry} actually,which could only be invoked when the
* {@link BucketEntry#refCnt} becoming 0.
@@ -1087,6 +1123,7 @@ public class BucketCache implements BlockCache, HeapSize {
protected void putIntoBackingMap(BlockCacheKey key, BucketEntry bucketEntry) {
BucketEntry previousEntry = backingMap.put(key, bucketEntry);
blocksByHFile.add(key);
+ updateRegionCachedSize(key.getFilePath(), bucketEntry.getLength());
if (previousEntry != null && previousEntry != bucketEntry) {
previousEntry.withWriteLock(offsetLock, () -> {
blockEvicted(key, previousEntry, false, false);
@@ -1308,8 +1345,9 @@ public class BucketCache implements BlockCache, HeapSize {
return ioEngine.isPersistent() && persistencePath != null;
}
- public Map<String, Long> getRegionCachedInfo() {
- return Collections.unmodifiableMap(regionCachedSizeMap);
+ @Override
+ public Optional<Map<String, Long>> getRegionCachedInfo() {
+ return Optional.of(Collections.unmodifiableMap(regionCachedSize));
}
/**
@@ -1350,17 +1388,17 @@ public class BucketCache implements BlockCache, HeapSize {
}
private void updateRegionSizeMapWhileRetrievingFromFile() {
- // Update the regionCachedSizeMap with the region size while restarting the region server
+ // Update the regionCachedSize with the region size while restarting the region server
if (LOG.isDebugEnabled()) {
LOG.debug("Updating region size map after retrieving cached file list");
dumpPrefetchList();
}
- regionCachedSizeMap.clear();
+ regionCachedSize.clear();
fullyCachedFiles.forEach((hFileName, hFileSize) -> {
// Get the region name for each file
String regionEncodedName = hFileSize.getFirst();
long cachedFileSize = hFileSize.getSecond();
- regionCachedSizeMap.merge(regionEncodedName, cachedFileSize,
+ regionCachedSize.merge(regionEncodedName, cachedFileSize,
(oldpf, fileSize) -> oldpf + fileSize);
});
}
@@ -1519,7 +1557,7 @@ public class BucketCache implements BlockCache, HeapSize {
this.backingMap.clear();
this.blocksByHFile.clear();
this.fullyCachedFiles.clear();
- this.regionCachedSizeMap.clear();
+ this.regionCachedSize.clear();
}
}
@@ -1619,7 +1657,7 @@ public class BucketCache implements BlockCache, HeapSize {
*/
@Override
public int evictBlocksByHfileName(String hfileName) {
- removeFileFromPrefetch(hfileName);
+ fileNotFullyCached(hfileName);
Set<BlockCacheKey> keySet = blocksByHFile.subSet(new BlockCacheKey(hfileName, Long.MIN_VALUE),
true, new BlockCacheKey(hfileName, Long.MAX_VALUE), true);
@@ -2028,35 +2066,6 @@ public class BucketCache implements BlockCache, HeapSize {
return Optional.empty();
}
- private void removeFileFromPrefetch(String hfileName) {
- // Update the regionPrefetchedSizeMap before removing the file from prefetchCompleted
- if (fullyCachedFiles.containsKey(hfileName)) {
- Pair<String, Long> regionEntry = fullyCachedFiles.get(hfileName);
- String regionEncodedName = regionEntry.getFirst();
- long filePrefetchSize = regionEntry.getSecond();
- LOG.debug("Removing file {} for region {}", hfileName, regionEncodedName);
- regionCachedSizeMap.computeIfPresent(regionEncodedName, (rn, pf) -> pf - filePrefetchSize);
- // If all the blocks for a region are evicted from the cache, remove the entry for that region
- if (
- regionCachedSizeMap.containsKey(regionEncodedName)
- && regionCachedSizeMap.get(regionEncodedName) == 0
- ) {
- regionCachedSizeMap.remove(regionEncodedName);
- }
- }
- fullyCachedFiles.remove(hfileName);
- }
-
- public void fileCacheCompleted(Path filePath, long size) {
- Pair<String, Long> pair = new Pair<>();
- // sets the region name
- String regionName = filePath.getParent().getParent().getName();
- pair.setFirst(regionName);
- pair.setSecond(size);
- fullyCachedFiles.put(filePath.getName(), pair);
- regionCachedSizeMap.merge(regionName, size, (oldpf, fileSize) -> oldpf + fileSize);
- }
-
@Override
public void notifyFileCachingCompleted(Path fileName, int totalBlockCount, int dataBlockCount,
long size) {
@@ -2111,11 +2120,6 @@ public class BucketCache implements BlockCache, HeapSize {
}
}
- @Override
- public void notifyFileBlockEvicted(String fileName) {
- fullyCachedFiles.remove(fileName);
- }
-
@Override
public Optional<Boolean> blockFitsIntoTheCache(HFileBlock block) {
long currentUsed = bucketAllocator.getUsedSize();
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 3042a2eae45..b6c573a953f 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
@@ -1218,11 +1218,15 @@ public class HRegionServer extends HBaseServerBase<RSRpcServices>
serverLoad.addCoprocessors(coprocessorBuilder.setName(coprocessor).build());
}
}
- computeIfPersistentBucketCache(bc -> {
- bc.getRegionCachedInfo().forEach((regionName, prefetchSize) -> {
- serverLoad.putRegionCachedInfo(regionName, roundSize(prefetchSize, unitMB));
+
+ getBlockCache().ifPresent(cache -> {
+ cache.getRegionCachedInfo().ifPresent(regionCachedInfo -> {
+ regionCachedInfo.forEach((regionName, prefetchSize) -> {
+ serverLoad.putRegionCachedInfo(regionName, roundSize(prefetchSize, unitMB));
+ });
});
});
+
serverLoad.setReportStartTime(reportStartTime);
serverLoad.setReportEndTime(reportEndTime);
if (this.infoServer != null) {
@@ -1587,13 +1591,14 @@ public class HRegionServer extends HBaseServerBase<RSRpcServices>
int totalStaticBloomSizeKB = roundSize(totalStaticBloomSize, unitKB);
int regionSizeMB = roundSize(totalRegionSize, unitMB);
final MutableFloat currentRegionCachedRatio = new MutableFloat(0.0f);
- computeIfPersistentBucketCache(bc -> {
- if (bc.getRegionCachedInfo().containsKey(regionEncodedName)) {
- currentRegionCachedRatio.setValue(regionSizeMB == 0
- ? 0.0f
- : (float) roundSize(bc.getRegionCachedInfo().get(regionEncodedName), unitMB)
- / regionSizeMB);
- }
+ getBlockCache().ifPresent(bc -> {
+ bc.getRegionCachedInfo().ifPresent(regionCachedInfo -> {
+ if (regionCachedInfo.containsKey(regionEncodedName)) {
+ currentRegionCachedRatio.setValue(regionSizeMB == 0
+ ? 0.0f
+ : (float) roundSize(regionCachedInfo.get(regionEncodedName), unitMB) / regionSizeMB);
+ }
+ });
});
HDFSBlocksDistribution hdfsBd = r.getHDFSBlocksDistribution();
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 c9595c12b5e..b3e2e93fc9e 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
@@ -26,6 +26,7 @@ import java.util.OptionalLong;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.mutable.MutableLong;
import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.TableDescriptor;
@@ -62,6 +63,8 @@ public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable
private ScheduledFuture<?> regionMetricsUpdateTask;
+ private float currentRegionCacheRatio;
+
public MetricsRegionWrapperImpl(HRegion region) {
this.region = region;
this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
@@ -121,6 +124,10 @@ public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable
return storeFileSize;
}
+ public float getCurrentRegionCacheRatio() {
+ return currentRegionCacheRatio;
+ }
+
@Override
public long getStoreRefCount() {
return storeRefCount;
@@ -315,7 +322,15 @@ public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable
readsOnlyFromMemstore.put(store.getColumnFamilyName(), tempVal);
}
}
-
+ MutableLong regionCachedAmount = new MutableLong(0);
+ region.getBlockCache().getRegionCachedInfo().ifPresent(regionCacheRatio -> regionCachedAmount
+ .addAndGet(regionCacheRatio.getOrDefault(region.getRegionInfo().getEncodedName(), 0L)));
+ if (tempStoreFileSize > 0) {
+ LOG.debug("Region {}, had cached {} bytes from a total of {}",
+ region.getRegionInfo().getEncodedName(), regionCachedAmount.getValue(),
+ tempStoreFileSize);
+ currentRegionCacheRatio = regionCachedAmount.floatValue() / tempStoreFileSize;
+ }
numStoreFiles = tempNumStoreFiles;
storeRefCount = tempStoreRefCount;
maxCompactedStoreFileRefCount = tempMaxCompactedStoreFileRefCount;
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestPrefetchWithBucketCache.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestPrefetchWithBucketCache.java
index 44629823547..2d0a85962ef 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestPrefetchWithBucketCache.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestPrefetchWithBucketCache.java
@@ -32,6 +32,7 @@ import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
+import org.apache.commons.lang3.mutable.MutableLong;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -185,6 +186,30 @@ public class TestPrefetchWithBucketCache {
assertTrue(bc.getStats().getEvictedCount() > 200);
}
+ @Test
+ public void testPrefetchMetricProgress() throws Exception {
+ conf.setLong(BUCKET_CACHE_SIZE_KEY, 200);
+ blockCache = BlockCacheFactory.createBlockCache(conf);
+ cacheConf = new CacheConfig(conf, blockCache);
+ Path storeFile = writeStoreFile("testPrefetchMetricsProgress", 100);
+ // Prefetches the file blocks
+ LOG.debug("First read should prefetch the blocks.");
+ readStoreFile(storeFile);
+ String regionName = storeFile.getParent().getParent().getName();
+ BucketCache bc = BucketCache.getBucketCacheFromCacheConfig(cacheConf).get();
+ MutableLong regionCachedSize = new MutableLong(0);
+ // Our file should have 6 DATA blocks. We should wait for all of them to be cached
+ long waitedTime = Waiter.waitFor(conf, 300, () -> {
+ if (bc.getBackingMap().size() > 0) {
+ long currentSize = bc.getRegionCachedInfo().get().get(regionName);
+ assertTrue(regionCachedSize.getValue() <= currentSize);
+ LOG.debug("Logging progress of region caching: {}", currentSize);
+ regionCachedSize.setValue(currentSize);
+ }
+ return bc.getBackingMap().size() == 6;
+ });
+ }
+
private void readStoreFile(Path storeFilePath) throws Exception {
readStoreFile(storeFilePath, (r, o) -> {
HFileBlock block = null;
@@ -216,6 +241,7 @@ public class TestPrefetchWithBucketCache {
Thread.sleep(1000);
}
long offset = 0;
+ long sizeForDataBlocks = 0;
while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) {
HFileBlock block = readFunction.apply(reader, offset);
BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), offset);
@@ -276,5 +302,4 @@ public class TestPrefetchWithBucketCache {
return keyType;
}
}
-
}
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperStub.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperStub.java
index 01187475944..c88e2deee4a 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperStub.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/MetricsRegionWrapperStub.java
@@ -82,6 +82,11 @@ public class MetricsRegionWrapperStub implements MetricsRegionWrapper {
return 104;
}
+ @Override
+ public float getCurrentRegionCacheRatio() {
+ return 0;
+ }
+
@Override
public long getReadRequestCount() {
return 105;