View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Converts a Hbase.Iface using InvocationHandler so that it reports process
34   * time of each call to ThriftMetrics.
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  }