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