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.client;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.CoprocessorEnvironment;
26  import org.apache.hadoop.hbase.ServerName;
27  import org.apache.hadoop.hbase.client.ConnectionManager.HConnectionImplementation;
28  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
29  import org.apache.hadoop.hbase.regionserver.HRegionServer;
30  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
31  import org.apache.hadoop.hbase.security.UserProvider;
32  
33  /**
34   * Connection to an HTable from within a Coprocessor. We can do some nice tricks since we know we
35   * are on a regionserver, for instance skipping the full serialization/deserialization of objects
36   * when talking to the server.
37   * <p>
38   * You should not use this class from any client - its an internal class meant for use by the
39   * coprocessor framework.
40   */
41  @InterfaceAudience.Private
42  @InterfaceStability.Evolving
43  public class CoprocessorHConnection extends HConnectionImplementation {
44    private static final NonceGenerator NO_NONCE_GEN = new ConnectionManager.NoNonceGenerator();
45  
46    /**
47     * Create an unmanaged {@link HConnection} based on the environment in which we are running the
48     * coprocessor. The {@link HConnection} must be externally cleaned up (we bypass the usual HTable
49     * cleanup mechanisms since we own everything).
50     * @param env environment hosting the {@link HConnection}
51     * @return an unmanaged {@link HConnection}.
52     * @throws IOException if we cannot create the connection
53     */
54    public static ClusterConnection getConnectionForEnvironment(CoprocessorEnvironment env)
55        throws IOException {
56      // this bit is a little hacky - just trying to get it going for the moment
57      if (env instanceof RegionCoprocessorEnvironment) {
58        RegionCoprocessorEnvironment e = (RegionCoprocessorEnvironment) env;
59        RegionServerServices services = e.getRegionServerServices();
60        if (services instanceof HRegionServer) {
61          return new CoprocessorHConnection((HRegionServer) services);
62        }
63      }
64      return ConnectionManager.createConnectionInternal(env.getConfiguration());
65    }
66  
67    private final ServerName serverName;
68    private final HRegionServer server;
69  
70    /**
71     * Legacy constructor
72     * @param delegate
73     * @param server
74     * @throws IOException if we cannot create the connection
75     * @deprecated delegate is not used
76     */
77    @Deprecated
78    public CoprocessorHConnection(ClusterConnection delegate, HRegionServer server)
79        throws IOException {
80      this(server);
81    }
82  
83    /**
84     * Constructor that uses server configuration
85     * @param server
86     * @throws IOException if we cannot create the connection
87     */
88    public CoprocessorHConnection(HRegionServer server) throws IOException {
89      this(server.getConfiguration(), server);
90    }
91  
92    /**
93     * Constructor that accepts custom configuration
94     * @param conf
95     * @param server
96     * @throws IOException if we cannot create the connection
97     */
98    public CoprocessorHConnection(Configuration conf, HRegionServer server) throws IOException {
99      super(conf, false, null, UserProvider.instantiate(conf).getCurrent(), server.getClusterId());
100     this.server = server;
101     this.serverName = server.getServerName();
102   }
103 
104   @Override
105   public org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService.BlockingInterface
106       getClient(ServerName serverName) throws IOException {
107     // client is trying to reach off-server, so we can't do anything special
108     if (!this.serverName.equals(serverName)) {
109       return super.getClient(serverName);
110     }
111     // the client is attempting to write to the same regionserver, we can short-circuit to our
112     // local regionserver
113     return server.getRSRpcServices();
114   }
115 
116   @Override
117   public NonceGenerator getNonceGenerator() {
118     return NO_NONCE_GEN; // don't use nonces for coprocessor connection
119   }
120 }