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.conf.Configuration;
29 import org.apache.hadoop.hbase.thrift.generated.Hbase;
30
31
32
33
34
35
36 public class HbaseHandlerMetricsProxy implements InvocationHandler {
37
38 public static final Log LOG = LogFactory.getLog(
39 HbaseHandlerMetricsProxy.class);
40
41 private final Hbase.Iface handler;
42 private final ThriftMetrics metrics;
43
44 public static Hbase.Iface newInstance(Hbase.Iface handler,
45 ThriftMetrics metrics,
46 Configuration conf) {
47 return (Hbase.Iface) Proxy.newProxyInstance(
48 handler.getClass().getClassLoader(),
49 new Class[]{Hbase.Iface.class},
50 new HbaseHandlerMetricsProxy(handler, metrics, conf));
51 }
52
53 private HbaseHandlerMetricsProxy(
54 Hbase.Iface handler, ThriftMetrics metrics, Configuration conf) {
55 this.handler = handler;
56 this.metrics = metrics;
57 }
58
59 @Override
60 public Object invoke(Object proxy, Method m, Object[] args)
61 throws Throwable {
62 Object result;
63 try {
64 long start = now();
65 result = m.invoke(handler, args);
66 int processTime = (int)(now() - start);
67 metrics.incMethodTime(m.getName(), processTime);
68 } catch (InvocationTargetException e) {
69 throw e.getTargetException();
70 } catch (Exception e) {
71 throw new RuntimeException(
72 "unexpected invocation exception: " + e.getMessage());
73 }
74 return result;
75 }
76
77 private static long now() {
78 return System.nanoTime();
79 }
80 }