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  package org.apache.hadoop.hbase.ipc;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.client.HConnection;
24  import org.apache.hadoop.hbase.client.ServerCallable;
25  import org.apache.hadoop.hbase.client.coprocessor.Exec;
26  import org.apache.hadoop.hbase.client.coprocessor.ExecResult;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  import java.lang.reflect.InvocationHandler;
30  import java.lang.reflect.Method;
31  
32  /**
33   * Backs a {@link org.apache.hadoop.hbase.ipc.CoprocessorProtocol} subclass proxy and forwards method
34   * invocations for server execution.  Note that internally this will issue a
35   * separate RPC call for each method invocation.
36   */
37  public class MasterExecRPCInvoker implements InvocationHandler {
38    // LOG is NOT in hbase subpackage intentionally so that the default HBase
39    // DEBUG log level does NOT emit RPC-level logging.
40    private static final Log LOG = LogFactory.getLog("org.apache.hadoop.ipc.MasterExecRPCInvoker");
41  
42    private Configuration conf;
43    private final HConnection connection;
44    private Class<? extends CoprocessorProtocol> protocol;
45  
46    public MasterExecRPCInvoker(Configuration conf,
47                                HConnection connection,
48                                Class<? extends CoprocessorProtocol> protocol) {
49      this.conf = conf;
50      this.connection = connection;
51      this.protocol = protocol;
52    }
53  
54    @Override
55    public Object invoke(Object instance, final Method method, final Object[] args)
56        throws Throwable {
57      if (LOG.isDebugEnabled()) {
58        LOG.debug("Call: "+method.getName()+", "+(args != null ? args.length : 0));
59      }
60      Exec exec = new Exec(conf, protocol, method, args);
61      ExecResult result = connection.getMaster().execCoprocessor(exec);
62      LOG.debug("Master Result is value="+result.getValue());
63      return result.getValue();
64    }
65  }