HADOOP-9467. Metrics2 record filter should check name as well as tags. (Ganeshan Iyler via llu)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1466377 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luke Lu 2013-04-10 08:25:37 +00:00
parent 2b19054c92
commit 4edf48c7ad
3 changed files with 67 additions and 1 deletions

View File

@ -629,6 +629,9 @@ Release 2.0.4-alpha - UNRELEASED
BUG FIXES BUG FIXES
HADOOP-9467. Metrics2 record filter should check name as well as tags.
(Ganeshan Iyler via llu)
HADOOP-9406. hadoop-client leaks dependency on JDK tools jar. (tucu) HADOOP-9406. hadoop-client leaks dependency on JDK tools jar. (tucu)
HADOOP-9301. hadoop client servlet/jsp/jetty/tomcat JARs creating HADOOP-9301. hadoop client servlet/jsp/jetty/tomcat JARs creating

View File

@ -57,7 +57,7 @@ public abstract class MetricsFilter implements MetricsPlugin {
* @return true to accept; false otherwise. * @return true to accept; false otherwise.
*/ */
public boolean accepts(MetricsRecord record) { public boolean accepts(MetricsRecord record) {
return accepts(record.tags()); return accepts(record.name()) && accepts(record.tags());
} }
} }

View File

@ -24,7 +24,9 @@ import java.util.List;
import org.apache.commons.configuration.SubsetConfiguration; import org.apache.commons.configuration.SubsetConfiguration;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.apache.hadoop.metrics2.MetricsRecord;
import org.apache.hadoop.metrics2.MetricsTag; import org.apache.hadoop.metrics2.MetricsTag;
import org.apache.hadoop.metrics2.impl.ConfigBuilder; import org.apache.hadoop.metrics2.impl.ConfigBuilder;
import static org.apache.hadoop.metrics2.lib.Interns.*; import static org.apache.hadoop.metrics2.lib.Interns.*;
@ -38,6 +40,8 @@ public class TestPatternFilter {
SubsetConfiguration empty = new ConfigBuilder().subset(""); SubsetConfiguration empty = new ConfigBuilder().subset("");
shouldAccept(empty, "anything"); shouldAccept(empty, "anything");
shouldAccept(empty, Arrays.asList(tag("key", "desc", "value"))); shouldAccept(empty, Arrays.asList(tag("key", "desc", "value")));
shouldAccept(empty, mockMetricsRecord("anything", Arrays.asList(
tag("key", "desc", "value"))));
} }
/** /**
@ -50,9 +54,15 @@ public class TestPatternFilter {
shouldAccept(wl, "foo"); shouldAccept(wl, "foo");
shouldAccept(wl, Arrays.asList(tag("bar", "", ""), shouldAccept(wl, Arrays.asList(tag("bar", "", ""),
tag("foo", "", "f"))); tag("foo", "", "f")));
shouldAccept(wl, mockMetricsRecord("foo", Arrays.asList(
tag("bar", "", ""), tag("foo", "", "f"))));
shouldReject(wl, "bar"); shouldReject(wl, "bar");
shouldReject(wl, Arrays.asList(tag("bar", "", ""))); shouldReject(wl, Arrays.asList(tag("bar", "", "")));
shouldReject(wl, Arrays.asList(tag("foo", "", "boo"))); shouldReject(wl, Arrays.asList(tag("foo", "", "boo")));
shouldReject(wl, mockMetricsRecord("bar", Arrays.asList(
tag("foo", "", "f"))));
shouldReject(wl, mockMetricsRecord("foo", Arrays.asList(
tag("bar", "", ""))));
} }
/** /**
@ -64,9 +74,15 @@ public class TestPatternFilter {
.add("p.exclude.tags", "foo:f").subset("p"); .add("p.exclude.tags", "foo:f").subset("p");
shouldAccept(bl, "bar"); shouldAccept(bl, "bar");
shouldAccept(bl, Arrays.asList(tag("bar", "", ""))); shouldAccept(bl, Arrays.asList(tag("bar", "", "")));
shouldAccept(bl, mockMetricsRecord("bar", Arrays.asList(
tag("bar", "", ""))));
shouldReject(bl, "foo"); shouldReject(bl, "foo");
shouldReject(bl, Arrays.asList(tag("bar", "", ""), shouldReject(bl, Arrays.asList(tag("bar", "", ""),
tag("foo", "", "f"))); tag("foo", "", "f")));
shouldReject(bl, mockMetricsRecord("foo", Arrays.asList(
tag("bar", "", ""))));
shouldReject(bl, mockMetricsRecord("bar", Arrays.asList(
tag("bar", "", ""), tag("foo", "", "f"))));
} }
/** /**
@ -81,10 +97,18 @@ public class TestPatternFilter {
.add("p.exclude.tags", "bar:b").subset("p"); .add("p.exclude.tags", "bar:b").subset("p");
shouldAccept(c, "foo"); shouldAccept(c, "foo");
shouldAccept(c, Arrays.asList(tag("foo", "", "f"))); shouldAccept(c, Arrays.asList(tag("foo", "", "f")));
shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
tag("foo", "", "f"))));
shouldReject(c, "bar"); shouldReject(c, "bar");
shouldReject(c, Arrays.asList(tag("bar", "", "b"))); shouldReject(c, Arrays.asList(tag("bar", "", "b")));
shouldReject(c, mockMetricsRecord("bar", Arrays.asList(
tag("foo", "", "f"))));
shouldReject(c, mockMetricsRecord("foo", Arrays.asList(
tag("bar", "", "b"))));
shouldAccept(c, "foobar"); shouldAccept(c, "foobar");
shouldAccept(c, Arrays.asList(tag("foobar", "", ""))); shouldAccept(c, Arrays.asList(tag("foobar", "", "")));
shouldAccept(c, mockMetricsRecord("foobar", Arrays.asList(
tag("foobar", "", ""))));
} }
/** /**
@ -98,6 +122,8 @@ public class TestPatternFilter {
.add("p.exclude.tags", "foo:f").subset("p"); .add("p.exclude.tags", "foo:f").subset("p");
shouldAccept(c, "foo"); shouldAccept(c, "foo");
shouldAccept(c, Arrays.asList(tag("foo", "", "f"))); shouldAccept(c, Arrays.asList(tag("foo", "", "f")));
shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
tag("foo", "", "f"))));
} }
static void shouldAccept(SubsetConfiguration conf, String s) { static void shouldAccept(SubsetConfiguration conf, String s) {
@ -110,6 +136,17 @@ public class TestPatternFilter {
assertTrue("accepts "+ tags, newRegexFilter(conf).accepts(tags)); assertTrue("accepts "+ tags, newRegexFilter(conf).accepts(tags));
} }
/**
* Asserts that filters with the given configuration accept the given record.
*
* @param conf SubsetConfiguration containing filter configuration
* @param record MetricsRecord to check
*/
static void shouldAccept(SubsetConfiguration conf, MetricsRecord record) {
assertTrue("accepts " + record, newGlobFilter(conf).accepts(record));
assertTrue("accepts " + record, newRegexFilter(conf).accepts(record));
}
static void shouldReject(SubsetConfiguration conf, String s) { static void shouldReject(SubsetConfiguration conf, String s) {
assertTrue("rejects "+ s, !newGlobFilter(conf).accepts(s)); assertTrue("rejects "+ s, !newGlobFilter(conf).accepts(s));
assertTrue("rejects "+ s, !newRegexFilter(conf).accepts(s)); assertTrue("rejects "+ s, !newRegexFilter(conf).accepts(s));
@ -120,6 +157,17 @@ public class TestPatternFilter {
assertTrue("rejects "+ tags, !newRegexFilter(conf).accepts(tags)); assertTrue("rejects "+ tags, !newRegexFilter(conf).accepts(tags));
} }
/**
* Asserts that filters with the given configuration reject the given record.
*
* @param conf SubsetConfiguration containing filter configuration
* @param record MetricsRecord to check
*/
static void shouldReject(SubsetConfiguration conf, MetricsRecord record) {
assertTrue("rejects " + record, !newGlobFilter(conf).accepts(record));
assertTrue("rejects " + record, !newRegexFilter(conf).accepts(record));
}
/** /**
* Create a new glob filter with a config object * Create a new glob filter with a config object
* @param conf the config object * @param conf the config object
@ -141,4 +189,19 @@ public class TestPatternFilter {
f.init(conf); f.init(conf);
return f; return f;
} }
/**
* Creates a mock MetricsRecord with the given name and tags.
*
* @param name String name
* @param tags List<MetricsTag> tags
* @return MetricsRecord newly created mock
*/
private static MetricsRecord mockMetricsRecord(String name,
List<MetricsTag> tags) {
MetricsRecord record = mock(MetricsRecord.class);
when(record.name()).thenReturn(name);
when(record.tags()).thenReturn(tags);
return record;
}
} }