1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.CoprocessorEnvironment;
25 import org.apache.hadoop.hbase.ServerName;
26 import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionImplementation;
27 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
28 import org.apache.hadoop.hbase.ipc.HRegionInterface;
29 import org.apache.hadoop.hbase.regionserver.HRegionServer;
30 import org.apache.hadoop.hbase.regionserver.RegionServerServices;
31
32
33
34
35
36
37
38
39 @SuppressWarnings("javadoc")
40 public class CoprocessorHConnection extends HConnectionManager.HConnectionImplementation {
41
42
43
44
45
46
47
48
49
50 @SuppressWarnings("resource")
51 public static HConnection getConnectionForEnvironment(CoprocessorEnvironment env)
52 throws IOException {
53 Configuration conf = env.getConfiguration();
54 HConnection connection = null;
55
56
57 if (env instanceof RegionCoprocessorEnvironment) {
58 RegionCoprocessorEnvironment e = (RegionCoprocessorEnvironment) env;
59 RegionServerServices services = e.getRegionServerServices();
60 if (services instanceof HRegionServer) {
61 connection = new CoprocessorHConnection(conf, (HRegionServer) services);
62 }
63 }
64
65
66 if (connection == null) {
67 connection = HConnectionManager.createConnection(conf);
68 }
69 return connection;
70 }
71
72 private ServerName serverName;
73 private HRegionServer server;
74
75 CoprocessorHConnection(Configuration conf, HRegionServer server) throws IOException {
76 super(conf, false, null);
77 this.server = server;
78 this.serverName = server.getServerName();
79 }
80
81 @Override
82 HRegionInterface getHRegionConnection(final String hostname, final int port,
83 final InetSocketAddress isa, final boolean master) throws IOException {
84
85
86 boolean isRemote = false;
87 if (isa != null) {
88 isRemote = checkRemote(isa.getHostName(), isa.getPort());
89 } else {
90 isRemote = checkRemote(hostname, port);
91 }
92
93 if (isRemote) {
94 return super.getHRegionConnection(hostname, port, isa, master);
95 }
96
97
98 return this.server;
99 }
100
101
102
103
104
105
106
107 private boolean checkRemote(String hostName, int port) {
108 return !(this.serverName.getHostname().equals(hostName) && this.serverName.getPort() == port);
109 }
110 }