HDFS-16242. JournalMetrics should add JournalId MetricTag. (#3494)

Contributed by Max Xie
This commit is contained in:
Neil 2021-10-01 15:35:14 +08:00 committed by GitHub
parent 0c498f21de
commit bf9106c812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 2 deletions

View File

@ -401,4 +401,29 @@ public static void assertQuantileGauges(String prefix,
geq(0l)); geq(0l));
} }
} }
/**
* Assert a tag of metric as expected.
* @param name of the metric tag
* @param expected value of the metric tag
* @param rb the record builder mock used to getMetrics
*/
public static void assertTag(String name, String expected,
MetricsRecordBuilder rb) {
Assert.assertEquals("Bad Tag for metric " + name,
expected, getStringTag(name, rb));
}
/**
* get the value tag for the metric.
* @param name of the metric tag
* @param rb value of the metric tag
* @return the value tag for the metric
*/
public static String getStringTag(String name, MetricsRecordBuilder rb) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(rb).tag(eqName(info(name, "")), captor.capture());
checkCaptured(captor, name);
return captor.getValue();
}
} }

View File

@ -20,6 +20,7 @@
import java.io.IOException; import java.io.IOException;
import org.apache.hadoop.metrics2.annotation.Metric; import org.apache.hadoop.metrics2.annotation.Metric;
import org.apache.hadoop.metrics2.annotation.Metric.Type;
import org.apache.hadoop.metrics2.annotation.Metrics; import org.apache.hadoop.metrics2.annotation.Metrics;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import org.apache.hadoop.metrics2.lib.MetricsRegistry; import org.apache.hadoop.metrics2.lib.MetricsRegistry;
@ -99,6 +100,11 @@ String getName() {
return "Journal-" + journal.getJournalId(); return "Journal-" + journal.getJournalId();
} }
@Metric(value={"JournalId", "Current JournalId"}, type=Type.TAG)
public String getJournalId() {
return journal.getJournalId();
}
@Metric("Current writer's epoch") @Metric("Current writer's epoch")
public long getLastWriterEpoch() { public long getLastWriterEpoch() {
try { try {

View File

@ -97,7 +97,8 @@ public void setup() throws Exception {
conf.set(DFSConfigKeys.DFS_JOURNALNODE_EDITS_DIR_KEY, conf.set(DFSConfigKeys.DFS_JOURNALNODE_EDITS_DIR_KEY,
editsDir.getAbsolutePath()); editsDir.getAbsolutePath());
} else if (testName.getMethodName().equals( } else if (testName.getMethodName().equals(
"testJournalDefaultDirForOneNameSpace")) { "testJournalDefaultDirForOneNameSpace") ||
testName.getMethodName().equals("testJournalMetricTags")) {
FileUtil.fullyDelete(new File(DFSConfigKeys FileUtil.fullyDelete(new File(DFSConfigKeys
.DFS_JOURNALNODE_EDITS_DIR_DEFAULT)); .DFS_JOURNALNODE_EDITS_DIR_DEFAULT));
setFederationConf(); setFederationConf();
@ -151,7 +152,8 @@ public void setup() throws Exception {
testName.getMethodName().equals( testName.getMethodName().equals(
"testJournalCommonDirAcrossNameSpace") || "testJournalCommonDirAcrossNameSpace") ||
testName.getMethodName().equals( testName.getMethodName().equals(
"testJournalDefaultDirForOneNameSpace")) { "testJournalDefaultDirForOneNameSpace") ||
testName.getMethodName().equals("testJournalMetricTags")) {
Collection<String> nameServiceIds = DFSUtilClient.getNameServiceIds(conf); Collection<String> nameServiceIds = DFSUtilClient.getNameServiceIds(conf);
for(String nsId: nameServiceIds) { for(String nsId: nameServiceIds) {
journalId = "test-journalid-" + nsId; journalId = "test-journalid-" + nsId;
@ -240,6 +242,23 @@ public void testJournalDefaultDirForOneNameSpace() {
File.separator + jid); File.separator + jid);
assertEquals(editsDir.toString(), journalStorage.getRoot().toString()); assertEquals(editsDir.toString(), journalStorage.getRoot().toString());
} }
@Test(timeout=100000)
public void testJournalMetricTags() {
setupStaticHostResolution(2, "journalnode");
String jid = "test-journalid-ns1";
Journal nsJournal = jn.getJournal(jid);
MetricsRecordBuilder metrics = MetricsAsserts.getMetrics(
nsJournal.getMetrics().getName());
MetricsAsserts.assertTag("JournalId", jid, metrics);
jid = "test-journalid-ns2";
nsJournal = jn.getJournal(jid);
metrics = MetricsAsserts.getMetrics(
nsJournal.getMetrics().getName());
MetricsAsserts.assertTag("JournalId", jid, metrics);
}
@Test(timeout=100000) @Test(timeout=100000)
public void testJournal() throws Exception { public void testJournal() throws Exception {
MetricsRecordBuilder metrics = MetricsAsserts.getMetrics( MetricsRecordBuilder metrics = MetricsAsserts.getMetrics(