1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift;
20
21 import java.lang.reflect.InvocationHandler;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.thrift.generated.Hbase;
31
32
33
34
35
36
37 @InterfaceAudience.Private
38 public class HbaseHandlerMetricsProxy implements InvocationHandler {
39
40 private static final Log LOG = LogFactory.getLog(
41 HbaseHandlerMetricsProxy.class);
42
43 private final Hbase.Iface handler;
44 private final ThriftMetrics metrics;
45
46 public static Hbase.Iface newInstance(Hbase.Iface handler,
47 ThriftMetrics metrics,
48 Configuration conf) {
49 return (Hbase.Iface) Proxy.newProxyInstance(
50 handler.getClass().getClassLoader(),
51 new Class[]{Hbase.Iface.class},
52 new HbaseHandlerMetricsProxy(handler, metrics, conf));
53 }
54
55 private HbaseHandlerMetricsProxy(
56 Hbase.Iface handler, ThriftMetrics metrics, Configuration conf) {
57 this.handler = handler;
58 this.metrics = metrics;
59 }
60
61 @Override
62 public Object invoke(Object proxy, Method m, Object[] args)
63 throws Throwable {
64 Object result;
65 try {
66 long start = now();
67 result = m.invoke(handler, args);
68 long processTime = now() - start;
69 metrics.incMethodTime(m.getName(), processTime);
70 } catch (InvocationTargetException e) {
71 throw e.getTargetException();
72 } catch (Exception e) {
73 throw new RuntimeException(
74 "unexpected invocation exception: " + e.getMessage());
75 }
76 return result;
77 }
78
79 private static long now() {
80 return System.nanoTime();
81 }
82 }