001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.thrift;
020
021import java.lang.reflect.InvocationHandler;
022import java.lang.reflect.InvocationTargetException;
023import java.lang.reflect.Method;
024import java.lang.reflect.Proxy;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.thrift.generated.Hbase;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Converts a Hbase.Iface using InvocationHandler so that it reports process
034 * time of each call to ThriftMetrics.
035 */
036@InterfaceAudience.Private
037public class HbaseHandlerMetricsProxy implements InvocationHandler {
038
039  private static final Logger LOG = LoggerFactory.getLogger(
040      HbaseHandlerMetricsProxy.class);
041
042  private final Hbase.Iface handler;
043  private final ThriftMetrics metrics;
044
045  public static Hbase.Iface newInstance(Hbase.Iface handler,
046                                        ThriftMetrics metrics,
047                                        Configuration conf) {
048    return (Hbase.Iface) Proxy.newProxyInstance(
049        handler.getClass().getClassLoader(),
050        new Class[]{Hbase.Iface.class},
051        new HbaseHandlerMetricsProxy(handler, metrics, conf));
052  }
053
054  private HbaseHandlerMetricsProxy(
055      Hbase.Iface handler, ThriftMetrics metrics, Configuration conf) {
056    this.handler = handler;
057    this.metrics = metrics;
058  }
059
060  @Override
061  public Object invoke(Object proxy, Method m, Object[] args)
062      throws Throwable {
063    Object result;
064    long start = now();
065    try {
066      result = m.invoke(handler, args);
067    } catch (InvocationTargetException e) {
068      metrics.exception(e.getCause());
069      throw e.getTargetException();
070    } catch (Exception e) {
071      metrics.exception(e);
072      throw new RuntimeException(
073          "unexpected invocation exception: " + e.getMessage());
074    } finally {
075      long processTime = now() - start;
076      metrics.incMethodTime(m.getName(), processTime);
077    }
078    return result;
079  }
080  
081  private static long now() {
082    return System.nanoTime();
083  }
084}