|
23 | 23 | import java.util.ArrayList;
|
24 | 24 | import java.util.List;
|
25 | 25 |
|
| 26 | +import org.apache.hadoop.metrics2.MetricsCollector; |
| 27 | +import org.apache.hadoop.metrics2.MetricsRecordBuilder; |
26 | 28 | import org.apache.hadoop.metrics2.MetricsSource;
|
27 | 29 | import org.apache.hadoop.metrics2.MetricsTag;
|
28 | 30 | import org.apache.hadoop.metrics2.annotation.Metric;
|
|
31 | 33 | import org.apache.hadoop.metrics2.lib.MetricsRegistry;
|
32 | 34 | import org.apache.hadoop.metrics2.lib.MetricsSourceBuilder;
|
33 | 35 | import org.apache.hadoop.metrics2.lib.MutableCounterLong;
|
| 36 | +import static org.apache.hadoop.metrics2.lib.Interns.info; |
| 37 | +import static org.junit.Assert.assertEquals; |
| 38 | + |
34 | 39 | import org.junit.Test;
|
35 | 40 |
|
| 41 | +import javax.management.MBeanAttributeInfo; |
| 42 | +import javax.management.MBeanInfo; |
| 43 | + |
36 | 44 | public class TestMetricsSourceAdapter {
|
37 | 45 |
|
| 46 | + |
| 47 | + @Test |
| 48 | + public void testPurgeOldMetrics() throws Exception { |
| 49 | + // create test source with a single metric counter of value 1 |
| 50 | + PurgableSource source = new PurgableSource(); |
| 51 | + MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source); |
| 52 | + final MetricsSource s = sb.build(); |
| 53 | + |
| 54 | + List<MetricsTag> injectedTags = new ArrayList<MetricsTag>(); |
| 55 | + MetricsSourceAdapter sa = new MetricsSourceAdapter( |
| 56 | + "tst", "tst", "testdesc", s, injectedTags, null, null, 1, false); |
| 57 | + |
| 58 | + MBeanInfo info = sa.getMBeanInfo(); |
| 59 | + boolean sawIt = false; |
| 60 | + for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) { |
| 61 | + sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName); |
| 62 | + }; |
| 63 | + assertTrue("The last generated metric is not exported to jmx", sawIt); |
| 64 | + |
| 65 | + Thread.sleep(1000); // skip JMX cache TTL |
| 66 | + |
| 67 | + info = sa.getMBeanInfo(); |
| 68 | + sawIt = false; |
| 69 | + for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) { |
| 70 | + sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName); |
| 71 | + }; |
| 72 | + assertTrue("The last generated metric is not exported to jmx", sawIt); |
| 73 | + } |
| 74 | + |
| 75 | + //generate a new key per each call |
| 76 | + class PurgableSource implements MetricsSource { |
| 77 | + int nextKey = 0; |
| 78 | + String lastKeyName = null; |
| 79 | + @Override |
| 80 | + public void getMetrics(MetricsCollector collector, boolean all) { |
| 81 | + MetricsRecordBuilder rb = |
| 82 | + collector.addRecord("purgablesource") |
| 83 | + .setContext("test"); |
| 84 | + lastKeyName = "key" + nextKey++; |
| 85 | + rb.addGauge(info(lastKeyName, "desc"), 1); |
| 86 | + } |
| 87 | + } |
| 88 | + |
38 | 89 | @Test
|
39 | 90 | public void testGetMetricsAndJmx() throws Exception {
|
40 | 91 | // create test source with a single metric counter of value 0
|
|
0 commit comments