1 package org.apache.hadoop.hbase.ipc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.net.InetSocketAddress;
20 import java.nio.channels.ClosedChannelException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.CallDroppedException;
25 import org.apache.hadoop.hbase.CellScanner;
26 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceStability;
29 import org.apache.hadoop.hbase.ipc.RpcServer.Call;
30 import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandler;
31 import org.apache.hadoop.hbase.util.Pair;
32 import org.apache.hadoop.security.UserGroupInformation;
33 import org.apache.hadoop.util.StringUtils;
34 import org.apache.htrace.Trace;
35 import org.apache.htrace.TraceScope;
36
37 import com.google.protobuf.Message;
38
39
40
41
42
43
44 @InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX})
45 @InterfaceStability.Evolving
46 public class CallRunner {
47 private static final Log LOG = LogFactory.getLog(CallRunner.class);
48
49 private static final CallDroppedException CALL_DROPPED_EXCEPTION
50 = new CallDroppedException();
51
52 private Call call;
53 private RpcServerInterface rpcServer;
54 private MonitoredRPCHandler status;
55 private volatile boolean sucessful;
56
57
58
59
60
61
62
63 CallRunner(final RpcServerInterface rpcServer, final Call call) {
64 this.call = call;
65 this.rpcServer = rpcServer;
66
67 if (call != null && rpcServer != null) {
68 this.rpcServer.addCallSize(call.getSize());
69 }
70 }
71
72 public Call getCall() {
73 return call;
74 }
75
76 public void setStatus(MonitoredRPCHandler status) {
77 this.status = status;
78 }
79
80
81
82
83 private void cleanup() {
84 this.call = null;
85 this.rpcServer = null;
86 }
87
88 public void run() {
89 try {
90 if (!call.connection.channel.isOpen()) {
91 if (RpcServer.LOG.isDebugEnabled()) {
92 RpcServer.LOG.debug(Thread.currentThread().getName() + ": skipped " + call);
93 }
94 return;
95 }
96 call.startTime = System.currentTimeMillis();
97 if (call.startTime > call.deadline) {
98 RpcServer.LOG.info("Drop timeout call: " + call);
99 return;
100 }
101 this.status.setStatus("Setting up call");
102 this.status.setConnection(call.connection.getHostAddress(), call.connection.getRemotePort());
103 if (RpcServer.LOG.isTraceEnabled()) {
104 UserGroupInformation remoteUser = call.connection.ugi;
105 RpcServer.LOG.trace(call.toShortString() + " executing as " +
106 ((remoteUser == null) ? "NULL principal" : remoteUser.getUserName()));
107 }
108 Throwable errorThrowable = null;
109 String error = null;
110 Pair<Message, CellScanner> resultPair = null;
111 RpcServer.CurCall.set(call);
112 TraceScope traceScope = null;
113 try {
114 if (!this.rpcServer.isStarted()) {
115 InetSocketAddress address = rpcServer.getListenerAddress();
116 throw new ServerNotRunningYetException("Server " +
117 (address != null ? address : "(channel closed)") + " is not running yet");
118 }
119 if (call.tinfo != null) {
120 traceScope = Trace.startSpan(call.toTraceString(), call.tinfo);
121 }
122
123 resultPair = this.rpcServer.call(call.service, call.md, call.param, call.cellScanner,
124 call.timestamp, this.status, call.startTime, call.timeout);
125 } catch (Throwable e) {
126 RpcServer.LOG.debug(Thread.currentThread().getName() + ": " + call.toShortString(), e);
127 errorThrowable = e;
128 error = StringUtils.stringifyException(e);
129 if (e instanceof Error) {
130 throw (Error)e;
131 }
132 } finally {
133 if (traceScope != null) {
134 traceScope.close();
135 }
136 RpcServer.CurCall.set(null);
137 if (resultPair != null) {
138 this.rpcServer.addCallSize(call.getSize() * -1);
139 sucessful = true;
140 }
141 }
142
143 Message param = resultPair != null ? resultPair.getFirst() : null;
144 CellScanner cells = resultPair != null ? resultPair.getSecond() : null;
145 call.setResponse(param, cells, errorThrowable, error);
146 call.sendResponseIfReady();
147 this.status.markComplete("Sent response");
148 this.status.pause("Waiting for a call");
149 } catch (OutOfMemoryError e) {
150 if (this.rpcServer.getErrorHandler() != null) {
151 if (this.rpcServer.getErrorHandler().checkOOME(e)) {
152 RpcServer.LOG.info(Thread.currentThread().getName() + ": exiting on OutOfMemoryError");
153 return;
154 }
155 } else {
156
157 throw e;
158 }
159 } catch (ClosedChannelException cce) {
160 InetSocketAddress address = rpcServer.getListenerAddress();
161 RpcServer.LOG.warn(Thread.currentThread().getName() + ": caught a ClosedChannelException, " +
162 "this means that the server " + (address != null ? address : "(channel closed)") +
163 " was processing a request but the client went away. The error message was: " +
164 cce.getMessage());
165 } catch (Exception e) {
166 RpcServer.LOG.warn(Thread.currentThread().getName()
167 + ": caught: " + StringUtils.stringifyException(e));
168 } finally {
169 if (!sucessful) {
170 this.rpcServer.addCallSize(call.getSize() * -1);
171 }
172 cleanup();
173 }
174 }
175
176
177
178
179 public void drop() {
180 try {
181 if (!call.connection.channel.isOpen()) {
182 if (RpcServer.LOG.isDebugEnabled()) {
183 RpcServer.LOG.debug(Thread.currentThread().getName() + ": skipped " + call);
184 }
185 return;
186 }
187
188
189 InetSocketAddress address = rpcServer.getListenerAddress();
190 call.setResponse(null, null, CALL_DROPPED_EXCEPTION, "Call dropped, server "
191 + (address != null ? address : "(channel closed)") + " is overloaded, please retry.");
192 call.sendResponseIfReady();
193 } catch (ClosedChannelException cce) {
194 InetSocketAddress address = rpcServer.getListenerAddress();
195 RpcServer.LOG.warn(Thread.currentThread().getName() + ": caught a ClosedChannelException, " +
196 "this means that the server " + (address != null ? address : "(channel closed)") +
197 " was processing a request but the client went away. The error message was: " +
198 cce.getMessage());
199 } catch (Exception e) {
200 RpcServer.LOG.warn(Thread.currentThread().getName()
201 + ": caught: " + StringUtils.stringifyException(e));
202 } finally {
203 if (!sucessful) {
204 this.rpcServer.addCallSize(call.getSize() * -1);
205 }
206 cleanup();
207 }
208 }
209 }