1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.classification.InterfaceAudience;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.IpcProtocol;
28 import org.apache.hadoop.hbase.client.RetriesExhaustedException;
29
30 import java.io.IOException;
31 import java.io.InterruptedIOException;
32 import java.net.ConnectException;
33 import java.net.InetSocketAddress;
34 import java.net.SocketTimeoutException;
35
36
37
38
39 @InterfaceAudience.Private
40 public class HBaseClientRPC {
41 protected static final Log LOG =
42 LogFactory.getLog("org.apache.hadoop.ipc.HBaseClientRPC");
43
44
45 private static ThreadLocal<Integer> rpcTimeout = new ThreadLocal<Integer>() {
46 @Override
47 protected Integer initialValue() {
48 return HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT;
49 }
50 };
51
52
53
54
55
56
57
58
59
60
61
62 public static <T extends IpcProtocol> T waitForProxy(RpcClientEngine engine,
63 Class<T> protocol,
64 InetSocketAddress addr,
65 Configuration conf,
66 int maxAttempts,
67 int rpcTimeout,
68 long timeout)
69 throws IOException {
70
71 long startTime = System.currentTimeMillis();
72 IOException ioe;
73 int reconnectAttempts = 0;
74 while (true) {
75 try {
76 return engine.getProxy(protocol, addr, conf, rpcTimeout);
77 } catch (SocketTimeoutException te) {
78 LOG.info("Problem connecting to server: " + addr);
79 ioe = te;
80 } catch (IOException ioex) {
81
82 ConnectException ce = null;
83 if (ioex instanceof ConnectException) {
84 ce = (ConnectException) ioex;
85 ioe = ce;
86 } else if (ioex.getCause() != null
87 && ioex.getCause() instanceof ConnectException) {
88 ce = (ConnectException) ioex.getCause();
89 ioe = ce;
90 } else if (ioex.getMessage().toLowerCase()
91 .contains("connection refused")) {
92 ce = new ConnectException(ioex.getMessage());
93 ioe = ce;
94 } else {
95
96 ioe = ioex;
97 }
98 if (ce != null) {
99 handleConnectionException(++reconnectAttempts, maxAttempts, protocol,
100 addr, ce);
101 }
102 }
103
104 if (System.currentTimeMillis() - timeout >= startTime) {
105 throw ioe;
106 }
107
108
109 try {
110 Thread.sleep(1000);
111 } catch (InterruptedException ie) {
112 Thread.interrupted();
113 throw new InterruptedIOException();
114 }
115 }
116 }
117
118
119
120
121
122
123
124
125
126
127 private static void handleConnectionException(int retries,
128 int maxAttmpts,
129 Class<?> protocol,
130 InetSocketAddress addr,
131 ConnectException ce)
132 throws RetriesExhaustedException {
133 if (maxAttmpts >= 0 && retries >= maxAttmpts) {
134 LOG.info("Server at " + addr + " could not be reached after "
135 + maxAttmpts + " tries, giving up.");
136 throw new RetriesExhaustedException("Failed setting up proxy " + protocol
137 + " to " + addr.toString() + " after attempts=" + maxAttmpts, ce);
138 }
139 }
140
141 public static void setRpcTimeout(int t) {
142 rpcTimeout.set(t);
143 }
144
145 public static int getRpcTimeout() {
146 return rpcTimeout.get();
147 }
148
149 public static void resetRpcTimeout() {
150 rpcTimeout.remove();
151 }
152 }