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.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.thrift.generated.Hbase;
31  
32  
33  /**
34   * Converts a Hbase.Iface using InvocationHandler so that it reports process
35   * time of each call to ThriftMetrics.
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  }