diff --git a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSource.java b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSource.java index b179b91d8a3..4a430cdc434 100644 --- a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSource.java +++ b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSource.java @@ -76,6 +76,8 @@ public interface MetricsWALSource extends BaseSource { "How many times a roll was requested due to file size roll threshold."; String WRITTEN_BYTES = "writtenBytes"; String WRITTEN_BYTES_DESC = "Size (in bytes) of the data written to the WAL."; + String SUCCESSFUL_LOG_ROLLS = "successfulLogRolls"; + String SUCCESSFUL_LOG_ROLLS_DESC = "Number of successful log rolls requests"; /** * Add the append size. @@ -115,4 +117,11 @@ public interface MetricsWALSource extends BaseSource { void incrementSizeLogRoll(); void incrementWrittenBytes(long val); + + /** + * Increment the number of successful log roll requests. + */ + void incrementSuccessfulLogRolls(); + + long getSuccessfulLogRolls(); } diff --git a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java index d308913f6e4..4f71681113c 100644 --- a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java +++ b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWALSourceImpl.java @@ -46,6 +46,7 @@ public class MetricsWALSourceImpl extends BaseSourceImpl implements MetricsWALSo private final MutableFastCounter slowSyncRollRequested; private final MutableFastCounter sizeRollRequested; private final MutableFastCounter writtenBytes; + private final MutableFastCounter successfulLogRolls; // Per table metrics. private final ConcurrentMap perTableAppendCount; private final ConcurrentMap perTableAppendSize; @@ -78,6 +79,8 @@ public class MetricsWALSourceImpl extends BaseSourceImpl implements MetricsWALSo sizeRollRequested = this.getMetricsRegistry() .newCounter(SIZE_ROLL_REQUESTED, SIZE_ROLL_REQUESTED_DESC, 0L); writtenBytes = this.getMetricsRegistry().newCounter(WRITTEN_BYTES, WRITTEN_BYTES_DESC, 0L); + successfulLogRolls = this.getMetricsRegistry() + .newCounter(SUCCESSFUL_LOG_ROLLS, SUCCESSFUL_LOG_ROLLS_DESC, 0L); perTableAppendCount = new ConcurrentHashMap<>(); perTableAppendSize = new ConcurrentHashMap<>(); } @@ -159,4 +162,14 @@ public class MetricsWALSourceImpl extends BaseSourceImpl implements MetricsWALSo public void incrementWrittenBytes(long val) { writtenBytes.incr(val); } + + @Override + public void incrementSuccessfulLogRolls() { + successfulLogRolls.incr(); + } + + @Override + public long getSuccessfulLogRolls() { + return successfulLogRolls.value(); + } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWAL.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWAL.java index ce82a6c2f55..4d0619c79bb 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWAL.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/MetricsWAL.java @@ -21,13 +21,14 @@ package org.apache.hadoop.hbase.regionserver.wal; import java.io.IOException; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.CompatibilitySingletonFactory; import org.apache.hadoop.hbase.TableName; import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.hadoop.hbase.wal.WALEdit; import org.apache.hadoop.hbase.wal.WALKey; -import org.apache.hadoop.hbase.CompatibilitySingletonFactory; import org.apache.hadoop.util.StringUtils; /** @@ -91,4 +92,13 @@ public class MetricsWAL implements WALActionsListener { break; } } + + @Override + public void postLogRoll(Path oldPath, Path newPath) { + // oldPath can be null if this is the first time we created a wal + // Also newPath can be equal to oldPath if AbstractFSWAL#replaceWriter fails + if (newPath != oldPath) { + source.incrementSuccessfulLogRolls(); + } + } } 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 14d0a88a3a3..574330241f1 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 @@ -24,6 +24,7 @@ import static org.mockito.Mockito.verify; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.testclassification.MiscTests; @@ -32,11 +33,15 @@ import org.apache.hadoop.hbase.wal.WALKey; import org.apache.hadoop.hbase.wal.WALKeyImpl; import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; @Category({MiscTests.class, SmallTests.class}) public class TestMetricsWAL { + @Rule + public TestName name = new TestName(); @ClassRule public static final HBaseClassTestRule CLASS_RULE = @@ -74,7 +79,8 @@ public class TestMetricsWAL { @Test public void testSlowAppend() throws Exception { - MetricsWALSource source = new MetricsWALSourceImpl(); + String testName = name.getMethodName(); + MetricsWALSource source = new MetricsWALSourceImpl(testName, testName, testName, testName); MetricsWAL metricsWAL = new MetricsWAL(source); TableName tableName = TableName.valueOf("foo"); WALKey walKey = new WALKeyImpl(null, tableName, -1); @@ -129,4 +135,25 @@ public class TestMetricsWAL { assertEquals(i * numIters, tableAppendSize); } } + + @Test + public void testLogRolls() { + String testName = name.getMethodName(); + MetricsWALSource source = new MetricsWALSourceImpl(testName, testName, testName, testName); + MetricsWAL metricsWAL = new MetricsWAL(source); + Path path1 = new Path("path-1"); + int count = 1; + // oldPath is null but newPath is not null; + metricsWAL.postLogRoll(null, path1); + assertEquals(count, source.getSuccessfulLogRolls()); + + // Simulating a case where AbstractFSWAL#replaceWriter fails + metricsWAL.postLogRoll(path1, path1); + assertEquals(count, source.getSuccessfulLogRolls()); + + count++; + Path path2 = new Path("path-2"); + metricsWAL.postLogRoll(path1, path2); + assertEquals(count, source.getSuccessfulLogRolls()); + } }