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.ipc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.metrics.MetricsContext;
26 import org.apache.hadoop.metrics.MetricsRecord;
27 import org.apache.hadoop.metrics.MetricsUtil;
28 import org.apache.hadoop.metrics.Updater;
29 import org.apache.hadoop.metrics.util.MetricsRegistry;
30 import org.apache.hadoop.metrics.util.MetricsTimeVaryingRate;
31
32 import java.lang.reflect.Method;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class HBaseRpcMetrics implements Updater {
47 public static final String NAME_DELIM = "$";
48 private MetricsRecord metricsRecord;
49 private static Log LOG = LogFactory.getLog(HBaseRpcMetrics.class);
50 private final HBaseRPCStatistics rpcStatistics;
51
52 public HBaseRpcMetrics(String hostName, String port) {
53 MetricsContext context = MetricsUtil.getContext("rpc");
54 metricsRecord = MetricsUtil.createRecord(context, "metrics");
55
56 metricsRecord.setTag("port", port);
57
58 LOG.info("Initializing RPC Metrics with hostName="
59 + hostName + ", port=" + port);
60
61 context.registerUpdater(this);
62
63 initMethods(HMasterInterface.class);
64 initMethods(HMasterRegionInterface.class);
65 initMethods(HRegionInterface.class);
66 rpcStatistics = new HBaseRPCStatistics(this.registry, hostName, port);
67 }
68
69
70
71
72
73
74
75 public final MetricsRegistry registry = new MetricsRegistry();
76
77 public MetricsTimeVaryingRate rpcQueueTime = new MetricsTimeVaryingRate("RpcQueueTime", registry);
78 public MetricsTimeVaryingRate rpcProcessingTime = new MetricsTimeVaryingRate("RpcProcessingTime", registry);
79
80
81
82 private void initMethods(Class<? extends HBaseRPCProtocolVersion> protocol) {
83 for (Method m : protocol.getDeclaredMethods()) {
84 if (get(m.getName()) == null)
85 create(m.getName());
86 }
87 }
88
89 private MetricsTimeVaryingRate get(String key) {
90 return (MetricsTimeVaryingRate) registry.get(key);
91 }
92 private MetricsTimeVaryingRate create(String key) {
93 return new MetricsTimeVaryingRate(key, this.registry);
94 }
95
96 public void inc(String name, int amt) {
97 MetricsTimeVaryingRate m = get(name);
98 if (m == null) {
99 LOG.warn("Got inc() request for method that doesnt exist: " +
100 name);
101 return;
102 }
103 m.inc(amt);
104 }
105
106
107
108
109
110
111
112 public void createMetrics(Class<?>[] ifaces) {
113 createMetrics(ifaces, false);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public void createMetrics(Class<?>[] ifaces, boolean prefixWithClass) {
131 for (Class<?> iface : ifaces) {
132 Method[] methods = iface.getMethods();
133 for (Method method : methods) {
134 String attrName = prefixWithClass ?
135 getMetricName(iface, method.getName()) : method.getName();
136 if (get(attrName) == null)
137 create(attrName);
138 }
139 }
140 }
141
142 public static String getMetricName(Class<?> c, String method) {
143 return c.getSimpleName() + NAME_DELIM + method;
144 }
145
146
147
148
149
150 public void doUpdates(MetricsContext context) {
151 rpcQueueTime.pushMetric(metricsRecord);
152 rpcProcessingTime.pushMetric(metricsRecord);
153
154 synchronized (registry) {
155
156
157 for (String metricName : registry.getKeyList() ) {
158 MetricsTimeVaryingRate value = (MetricsTimeVaryingRate) registry.get(metricName);
159
160 value.pushMetric(metricsRecord);
161 }
162 }
163 metricsRecord.update();
164 }
165
166 public void shutdown() {
167 if (rpcStatistics != null)
168 rpcStatistics.shutdown();
169 }
170 }