View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.ipc;
20  
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.Method;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.client.HConnection;
29  import org.apache.hadoop.hbase.client.ServerCallable;
30  import org.apache.hadoop.hbase.client.coprocessor.Exec;
31  import org.apache.hadoop.hbase.client.coprocessor.ExecResult;
32  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  /**
36   * Backs a {@link CoprocessorProtocol} subclass proxy and forwards method
37   * invocations for server execution.  Note that internally this will issue a
38   * separate RPC call for each method invocation (using a
39   * {@link org.apache.hadoop.hbase.client.ServerCallable} instance).
40   */
41  @InterfaceAudience.Private
42  @Deprecated
43  public class ExecRPCInvoker implements InvocationHandler {
44    // LOG is NOT in hbase subpackage intentionally so that the default HBase
45    // DEBUG log level does NOT emit RPC-level logging. 
46    private static final Log LOG = LogFactory.getLog("org.apache.hadoop.ipc.ExecRPCInvoker");
47  
48    private Configuration conf;
49    private final HConnection connection;
50    private Class<? extends CoprocessorProtocol> protocol;
51    private final byte[] table;
52    private final byte[] row;
53    private byte[] regionName;
54  
55    public ExecRPCInvoker(Configuration conf,
56        HConnection connection,
57        Class<? extends CoprocessorProtocol> protocol,
58        byte[] table,
59        byte[] row) {
60      this.conf = conf;
61      this.connection = connection;
62      this.protocol = protocol;
63      this.table = table;
64      this.row = row;
65    }
66  
67    @Override
68    public Object invoke(Object instance, final Method method, final Object[] args)
69        throws Throwable {
70      if (LOG.isDebugEnabled()) {
71        LOG.debug("Call: "+method.getName()+", "+(args != null ? args.length : 0));
72      }
73  
74      if (row != null) {
75        final Exec exec = new Exec(conf, row, protocol, method, args);
76        ServerCallable<ExecResult> callable =
77            new ServerCallable<ExecResult>(connection, table, row) {
78              public ExecResult call() throws Exception {
79                byte[] regionName = location.getRegionInfo().getRegionName();
80                return ProtobufUtil.execCoprocessor(server, exec, regionName);
81              }
82            };
83          ExecResult result = callable.withRetries();
84          this.regionName = result.getRegionName();
85          LOG.debug("Result is region="+ Bytes.toStringBinary(regionName) +
86              ", value="+result.getValue());
87          return result.getValue();
88      } else if (LOG.isDebugEnabled()) {
89        LOG.debug("Null row passed for call");
90      }
91  
92      return null;
93    }
94  
95    public byte[] getRegionName() {
96      return regionName;
97    }
98  }