View Javadoc

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