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.hadoop.hbase.thrift2.generated.THBaseService;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * Converts a Hbase.Iface using InvocationHandler so that it reports process
033 * time of each call to ThriftMetrics.
034 */
035@InterfaceAudience.Private
036public class HbaseHandlerMetricsProxy implements InvocationHandler {
037
038  private final Object handler;
039  private final ThriftMetrics metrics;
040
041  public static Hbase.Iface newInstance(Hbase.Iface handler,
042                                        ThriftMetrics metrics,
043                                        Configuration conf) {
044    return (Hbase.Iface) Proxy.newProxyInstance(
045        handler.getClass().getClassLoader(),
046        new Class[]{Hbase.Iface.class},
047        new HbaseHandlerMetricsProxy(handler, metrics, conf));
048  }
049
050  // for thrift 2
051  public static THBaseService.Iface newInstance(THBaseService.Iface handler,
052      ThriftMetrics metrics,
053      Configuration conf) {
054    return (THBaseService.Iface) Proxy.newProxyInstance(
055        handler.getClass().getClassLoader(),
056        new Class[]{THBaseService.Iface.class},
057        new HbaseHandlerMetricsProxy(handler, metrics, conf));
058  }
059
060  private HbaseHandlerMetricsProxy(
061      Object handler, ThriftMetrics metrics, Configuration conf) {
062    this.handler = handler;
063    this.metrics = metrics;
064  }
065
066  @Override
067  public Object invoke(Object proxy, Method m, Object[] args)
068      throws Throwable {
069    Object result;
070    long start = now();
071    try {
072      result = m.invoke(handler, args);
073    } catch (InvocationTargetException e) {
074      metrics.exception(e.getCause());
075      throw e.getTargetException();
076    } catch (Exception e) {
077      metrics.exception(e);
078      throw new RuntimeException(
079          "unexpected invocation exception: " + e.getMessage());
080    } finally {
081      long processTime = now() - start;
082      metrics.incMethodTime(m.getName(), processTime);
083    }
084    return result;
085  }
086  
087  private static long now() {
088    return System.nanoTime();
089  }
090}