1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.ipc;
20
21 import com.google.common.annotations.VisibleForTesting;
22 import com.google.protobuf.BlockingRpcChannel;
23 import com.google.protobuf.Descriptors;
24 import com.google.protobuf.Message;
25 import com.google.protobuf.RpcController;
26 import com.google.protobuf.ServiceException;
27
28 import java.io.IOException;
29 import java.net.ConnectException;
30 import java.net.InetSocketAddress;
31 import java.net.SocketAddress;
32 import java.net.SocketTimeoutException;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.hbase.CellScanner;
38 import org.apache.hadoop.hbase.HConstants;
39 import org.apache.hadoop.hbase.ServerName;
40 import org.apache.hadoop.hbase.classification.InterfaceAudience;
41 import org.apache.hadoop.hbase.codec.Codec;
42 import org.apache.hadoop.hbase.codec.KeyValueCodec;
43 import org.apache.hadoop.hbase.exceptions.ConnectionClosingException;
44 import org.apache.hadoop.hbase.security.User;
45 import org.apache.hadoop.hbase.security.UserProvider;
46 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
47 import org.apache.hadoop.hbase.util.Pair;
48 import org.apache.hadoop.hbase.util.PoolMap;
49 import org.apache.hadoop.io.compress.CompressionCodec;
50
51
52
53
54 @InterfaceAudience.Private
55 public abstract class AbstractRpcClient implements RpcClient {
56 public static final Log LOG = LogFactory.getLog(AbstractRpcClient.class);
57
58 protected final Configuration conf;
59 protected String clusterId;
60 protected final SocketAddress localAddr;
61
62 protected UserProvider userProvider;
63 protected final IPCUtil ipcUtil;
64
65 protected final int minIdleTimeBeforeClose;
66
67 protected final int maxRetries;
68 protected final long failureSleep;
69 protected final boolean tcpNoDelay;
70 protected final boolean tcpKeepAlive;
71 protected final Codec codec;
72 protected final CompressionCodec compressor;
73 protected final boolean fallbackAllowed;
74
75 protected final int connectTO;
76 protected final int readTO;
77 protected final int writeTO;
78
79
80
81
82
83
84
85
86 public AbstractRpcClient(Configuration conf, String clusterId, SocketAddress localAddr) {
87 this.userProvider = UserProvider.instantiate(conf);
88 this.localAddr = localAddr;
89 this.tcpKeepAlive = conf.getBoolean("hbase.ipc.client.tcpkeepalive", true);
90 this.clusterId = clusterId != null ? clusterId : HConstants.CLUSTER_ID_DEFAULT;
91 this.failureSleep = conf.getLong(HConstants.HBASE_CLIENT_PAUSE,
92 HConstants.DEFAULT_HBASE_CLIENT_PAUSE);
93 this.maxRetries = conf.getInt("hbase.ipc.client.connect.max.retries", 0);
94 this.tcpNoDelay = conf.getBoolean("hbase.ipc.client.tcpnodelay", true);
95 this.ipcUtil = new IPCUtil(conf);
96
97 this.minIdleTimeBeforeClose = conf.getInt(IDLE_TIME, 120000);
98 this.conf = conf;
99 this.codec = getCodec();
100 this.compressor = getCompressor(conf);
101 this.fallbackAllowed = conf.getBoolean(IPC_CLIENT_FALLBACK_TO_SIMPLE_AUTH_ALLOWED_KEY,
102 IPC_CLIENT_FALLBACK_TO_SIMPLE_AUTH_ALLOWED_DEFAULT);
103 this.connectTO = conf.getInt(SOCKET_TIMEOUT_CONNECT, DEFAULT_SOCKET_TIMEOUT_CONNECT);
104 this.readTO = conf.getInt(SOCKET_TIMEOUT_READ, DEFAULT_SOCKET_TIMEOUT_READ);
105 this.writeTO = conf.getInt(SOCKET_TIMEOUT_WRITE, DEFAULT_SOCKET_TIMEOUT_WRITE);
106
107
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("Codec=" + this.codec + ", compressor=" + this.compressor +
110 ", tcpKeepAlive=" + this.tcpKeepAlive +
111 ", tcpNoDelay=" + this.tcpNoDelay +
112 ", connectTO=" + this.connectTO +
113 ", readTO=" + this.readTO +
114 ", writeTO=" + this.writeTO +
115 ", minIdleTimeBeforeClose=" + this.minIdleTimeBeforeClose +
116 ", maxRetries=" + this.maxRetries +
117 ", fallbackAllowed=" + this.fallbackAllowed +
118 ", bind address=" + (this.localAddr != null ? this.localAddr : "null"));
119 }
120 }
121
122 @VisibleForTesting
123 public static String getDefaultCodec(final Configuration c) {
124
125
126
127 return c.get(DEFAULT_CODEC_CLASS, KeyValueCodec.class.getCanonicalName());
128 }
129
130
131
132
133
134 Codec getCodec() {
135
136
137 String className = conf.get(HConstants.RPC_CODEC_CONF_KEY, getDefaultCodec(this.conf));
138 if (className == null || className.length() == 0) return null;
139 try {
140 return (Codec)Class.forName(className).newInstance();
141 } catch (Exception e) {
142 throw new RuntimeException("Failed getting codec " + className, e);
143 }
144 }
145
146
147
148
149
150
151 private static CompressionCodec getCompressor(final Configuration conf) {
152 String className = conf.get("hbase.client.rpc.compressor", null);
153 if (className == null || className.isEmpty()) return null;
154 try {
155 return (CompressionCodec)Class.forName(className).newInstance();
156 } catch (Exception e) {
157 throw new RuntimeException("Failed getting compressor " + className, e);
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 protected static PoolMap.PoolType getPoolType(Configuration config) {
178 return PoolMap.PoolType
179 .valueOf(config.get(HConstants.HBASE_CLIENT_IPC_POOL_TYPE), PoolMap.PoolType.RoundRobin,
180 PoolMap.PoolType.ThreadLocal);
181 }
182
183
184
185
186
187
188
189
190 protected static int getPoolSize(Configuration config) {
191 return config.getInt(HConstants.HBASE_CLIENT_IPC_POOL_SIZE, 1);
192 }
193
194
195
196
197
198
199
200
201
202
203
204 Message callBlockingMethod(Descriptors.MethodDescriptor md, PayloadCarryingRpcController pcrc,
205 Message param, Message returnType, final User ticket, final InetSocketAddress isa)
206 throws ServiceException {
207 if (pcrc == null) {
208 pcrc = new PayloadCarryingRpcController();
209 }
210
211 long startTime = 0;
212 if (LOG.isTraceEnabled()) {
213 startTime = EnvironmentEdgeManager.currentTime();
214 }
215 Pair<Message, CellScanner> val;
216 try {
217 val = call(pcrc, md, param, returnType, ticket, isa);
218
219 pcrc.setCellScanner(val.getSecond());
220
221 if (LOG.isTraceEnabled()) {
222 long callTime = EnvironmentEdgeManager.currentTime() - startTime;
223 LOG.trace("Call: " + md.getName() + ", callTime: " + callTime + "ms");
224 }
225 return val.getFirst();
226 } catch (Throwable e) {
227 throw new ServiceException(e);
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 protected abstract Pair<Message, CellScanner> call(PayloadCarryingRpcController pcrc,
247 Descriptors.MethodDescriptor md, Message param, Message returnType, User ticket,
248 InetSocketAddress isa) throws IOException, InterruptedException;
249
250 @Override
251 public BlockingRpcChannel createBlockingRpcChannel(final ServerName sn, final User ticket,
252 int defaultOperationTimeout) {
253 return new BlockingRpcChannelImplementation(this, sn, ticket, defaultOperationTimeout);
254 }
255
256
257
258
259
260
261
262
263
264
265
266 protected IOException wrapException(InetSocketAddress addr, Exception exception) {
267 if (exception instanceof ConnectException) {
268
269 return (ConnectException) new ConnectException("Call to " + addr
270 + " failed on connection exception: " + exception).initCause(exception);
271 } else if (exception instanceof SocketTimeoutException) {
272 return (SocketTimeoutException) new SocketTimeoutException("Call to " + addr
273 + " failed because " + exception).initCause(exception);
274 } else if (exception instanceof ConnectionClosingException) {
275 return (ConnectionClosingException) new ConnectionClosingException("Call to " + addr
276 + " failed on local exception: " + exception).initCause(exception);
277 } else {
278 return (IOException) new IOException("Call to " + addr + " failed on local exception: "
279 + exception).initCause(exception);
280 }
281 }
282
283
284
285
286 @VisibleForTesting
287 public static class BlockingRpcChannelImplementation implements BlockingRpcChannel {
288 private final InetSocketAddress isa;
289 private final AbstractRpcClient rpcClient;
290 private final User ticket;
291 private final int channelOperationTimeout;
292
293
294
295
296 protected BlockingRpcChannelImplementation(final AbstractRpcClient rpcClient,
297 final ServerName sn, final User ticket, int channelOperationTimeout) {
298 this.isa = new InetSocketAddress(sn.getHostname(), sn.getPort());
299 this.rpcClient = rpcClient;
300 this.ticket = ticket;
301 this.channelOperationTimeout = channelOperationTimeout;
302 }
303
304 @Override
305 public Message callBlockingMethod(Descriptors.MethodDescriptor md, RpcController controller,
306 Message param, Message returnType) throws ServiceException {
307 PayloadCarryingRpcController pcrc;
308 if (controller != null) {
309 pcrc = (PayloadCarryingRpcController) controller;
310 if (!pcrc.hasCallTimeout()) {
311 pcrc.setCallTimeout(channelOperationTimeout);
312 }
313 } else {
314 pcrc = new PayloadCarryingRpcController();
315 pcrc.setCallTimeout(channelOperationTimeout);
316 }
317
318 return this.rpcClient.callBlockingMethod(md, pcrc, param, returnType, this.ticket, this.isa);
319 }
320 }
321 }