1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.regionserver;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.ipc.HBaseRpcMetrics;
32 import org.apache.hadoop.metrics.ContextFactory;
33 import org.apache.hadoop.metrics.MetricsContext;
34 import org.apache.hadoop.metrics.MetricsUtil;
35 import org.apache.hadoop.metrics.spi.AbstractMetricsContext;
36 import org.apache.hadoop.metrics.spi.OutputRecord;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40
41 import static org.junit.Assert.*;
42
43 public class TestRpcMetrics {
44
45
46
47 public interface TestMetrics {
48 public void test();
49 }
50
51
52
53
54 public static class TestRegionServer extends HRegionServer {
55
56 public TestRegionServer(Configuration conf)
57 throws IOException, InterruptedException {
58 super(conf);
59
60
61 getRpcMetrics().createMetrics(new Class[]{TestMetrics.class}, true);
62 }
63
64 public void incTest(int amt) {
65 HBaseRpcMetrics metrics = getRpcMetrics();
66
67 metrics.inc(metrics.getMetricName(TestMetrics.class, "test"), amt);
68 }
69 }
70
71
72
73
74 public static class MockMetricsContext extends AbstractMetricsContext {
75
76 public MockMetricsContext() {
77
78 setPeriod(1);
79 }
80
81 @Override
82 protected void emitRecord(String contextName, String recordName,
83 OutputRecord outputRecord) throws IOException {
84 for (String name : outputRecord.getMetricNames()) {
85 Number val = outputRecord.getMetric(name);
86 if (val != null && val.intValue() > 0) {
87 METRICS.put(name, Boolean.TRUE);
88 LOG.debug("Set metric "+name+" to "+val);
89 }
90 }
91 }
92 }
93 private static Map<String,Boolean> METRICS = new HashMap<String,Boolean>();
94
95 private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
96 private static Log LOG = LogFactory.getLog(TestRpcMetrics.class);
97
98 @BeforeClass
99 public static void setupBeforeClass() throws Exception {
100
101 ContextFactory factory = ContextFactory.getFactory();
102 factory.setAttribute("rpc.class", MockMetricsContext.class.getName());
103
104 MetricsContext ctx = MetricsUtil.getContext("rpc");
105 assertTrue("Wrong MetricContext implementation class",
106 (ctx instanceof MockMetricsContext));
107
108 TEST_UTIL.startMiniZKCluster();
109 }
110
111 @AfterClass
112 public static void tearDownAfterClass() throws Exception {
113 TEST_UTIL.shutdownMiniZKCluster();
114 }
115
116 @Test
117 public void testCustomMetrics() throws Exception {
118
119 TestRegionServer rs = new TestRegionServer(TEST_UTIL.getConfiguration());
120 rs.incTest(5);
121
122
123 Thread.sleep(1000);
124
125 String metricName = HBaseRpcMetrics.getMetricName(TestMetrics.class, "test");
126 assertTrue("Metric should have set incremented for "+metricName,
127 wasSet(metricName + "_num_ops"));
128 }
129
130 public boolean wasSet(String name) {
131 return METRICS.get(name) != null ? METRICS.get(name) : false;
132 }
133 }