From 4bbf6479e5131ff3ee310311af7807198c546237 Mon Sep 17 00:00:00 2001 From: terrytlu Date: Wed, 18 Sep 2024 11:37:07 +0800 Subject: [PATCH] HBASE-28845 table level wal appendSize and replication source metrics not correctly shown in /jmx response xx --- .../main/java/org/apache/hadoop/hbase/TableName.java | 4 ++++ .../hbase/regionserver/wal/MetricsWALSourceImpl.java | 5 +++-- .../hbase/replication/regionserver/MetricsSource.java | 2 +- .../hadoop/hbase/regionserver/wal/TestMetricsWAL.java | 10 ++++++---- .../hbase/replication/TestReplicationEndpoint.java | 7 +++++-- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java index 83303a1c476c..16e77f24385a 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/TableName.java @@ -260,6 +260,10 @@ public String getQualifierAsString() { return qualifierAsString; } + public String getMetricPrefixTableName() { + return "Namespace_" + this.getNamespaceAsString() + "table_" + this.getQualifierAsString(); + } + /** Returns A pointer to TableName as String bytes. */ public byte[] toBytes() { return name; diff --git a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java index 9400eb7d22c7..307ff097c5cf 100644 --- a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java +++ b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java @@ -88,7 +88,7 @@ public void incrementAppendSize(TableName tableName, long size) { if (tableAppendSizeCounter == null) { // Ideally putIfAbsent is atomic and we don't need a branch check but we still do it to avoid // expensive string construction for every append. - String metricsKey = String.format("%s.%s", tableName, APPEND_SIZE); + String metricsKey = String.format("%s.%s", tableName.getMetricPrefixTableName(), APPEND_SIZE); perTableAppendSize.putIfAbsent(tableName, getMetricsRegistry().newCounter(metricsKey, APPEND_SIZE_DESC, 0L)); tableAppendSizeCounter = perTableAppendSize.get(tableName); @@ -106,7 +106,8 @@ public void incrementAppendCount(TableName tableName) { appendCount.incr(); MutableFastCounter tableAppendCounter = perTableAppendCount.get(tableName); if (tableAppendCounter == null) { - String metricsKey = String.format("%s.%s", tableName, APPEND_COUNT); + String metricsKey = + String.format("%s.%s", tableName.getMetricPrefixTableName(), APPEND_COUNT); perTableAppendCount.putIfAbsent(tableName, getMetricsRegistry().newCounter(metricsKey, APPEND_COUNT_DESC, 0L)); tableAppendCounter = perTableAppendCount.get(tableName); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/MetricsSource.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/MetricsSource.java index 00bbc24dc247..b18807390983 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/MetricsSource.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/MetricsSource.java @@ -99,7 +99,7 @@ public void updateTableLevelMetrics(List> walEntries) { for (Pair walEntryWithSize : walEntries) { Entry entry = walEntryWithSize.getFirst(); long entrySize = walEntryWithSize.getSecond(); - String tableName = entry.getKey().getTableName().getNameAsString(); + String tableName = entry.getKey().getTableName().getMetricPrefixTableName(); long writeTime = entry.getKey().getWriteTime(); long age = EnvironmentEdgeManager.currentTime() - writeTime; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestMetricsWAL.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestMetricsWAL.java index e430964afd1c..c1303742d0b8 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestMetricsWAL.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestMetricsWAL.java @@ -127,11 +127,13 @@ public void testPerTableWALMetrics() throws Exception { // Validate the metrics for (int i = 0; i < numThreads; i++) { TableName tableName = TableName.valueOf("tab_" + i); - long tableAppendCount = - registry.getCounter(tableName + "." + MetricsWALSource.APPEND_COUNT, -1).value(); + long tableAppendCount = registry + .getCounter(tableName.getMetricPrefixTableName() + "." + MetricsWALSource.APPEND_COUNT, -1) + .value(); assertEquals(numIters, tableAppendCount); - long tableAppendSize = - registry.getCounter(tableName + "." + MetricsWALSource.APPEND_SIZE, -1).value(); + long tableAppendSize = registry + .getCounter(tableName.getMetricPrefixTableName() + "." + MetricsWALSource.APPEND_SIZE, -1) + .value(); assertEquals(i * numIters, tableAppendSize); } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationEndpoint.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationEndpoint.java index 057a9f3567f5..7378dd448633 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationEndpoint.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/TestReplicationEndpoint.java @@ -390,9 +390,12 @@ public void testMetricsSourceBaseSourcePassThrough() { source.getSingleSourceSourceByTable().containsKey("RandomNewTable"); Assert.assertEquals(false, containsRandomNewTable); source.updateTableLevelMetrics(createWALEntriesWithSize("RandomNewTable")); - containsRandomNewTable = source.getSingleSourceSourceByTable().containsKey("RandomNewTable"); + TableName tableName1 = TableName.valueOf("RandomNewTable"); + containsRandomNewTable = + source.getSingleSourceSourceByTable().containsKey(tableName1.getMetricPrefixTableName()); Assert.assertEquals(true, containsRandomNewTable); - MetricsReplicationTableSource msr = source.getSingleSourceSourceByTable().get("RandomNewTable"); + MetricsReplicationTableSource msr = + source.getSingleSourceSourceByTable().get(tableName1.getMetricPrefixTableName()); // age should be greater than zero we created the entry with time in the past Assert.assertTrue(msr.getLastShippedAge() > 0);