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.CellScanner;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.ipc.RpcServer.Call;
27 import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandler;
28 import org.apache.hadoop.hbase.util.Pair;
29 import org.apache.hadoop.security.UserGroupInformation;
30 import org.apache.hadoop.util.StringUtils;
31 import org.apache.htrace.Trace;
32 import org.apache.htrace.TraceScope;
33
34 import com.google.protobuf.Message;
35
36
37
38
39
40
41 @InterfaceAudience.Private
42 public class CallRunner {
43 private static final Log LOG = LogFactory.getLog(CallRunner.class);
44
45 private Call call;
46 private RpcServerInterface rpcServer;
47 private MonitoredRPCHandler status;
48 private volatile boolean sucessful;
49
50
51
52
53
54
55
56 CallRunner(final RpcServerInterface rpcServer, final Call call) {
57 this.call = call;
58 this.rpcServer = rpcServer;
59
60 this.rpcServer.addCallSize(call.getSize());
61 }
62
63 public Call getCall() {
64 return call;
65 }
66
67 public void setStatus(MonitoredRPCHandler status) {
68 this.status = status;
69 }
70
71
72
73
74 private void cleanup() {
75 this.call = null;
76 this.rpcServer = null;
77 }
78
79 public void run() {
80 try {
81 if (!call.connection.channel.isOpen()) {
82 if (RpcServer.LOG.isDebugEnabled()) {
83 RpcServer.LOG.debug(Thread.currentThread().getName() + ": skipped " + call);
84 }
85 return;
86 }
87 this.status.setStatus("Setting up call");
88 this.status.setConnection(call.connection.getHostAddress(), call.connection.getRemotePort());
89 if (RpcServer.LOG.isTraceEnabled()) {
90 UserGroupInformation remoteUser = call.connection.ugi;
91 RpcServer.LOG.trace(call.toShortString() + " executing as " +
92 ((remoteUser == null) ? "NULL principal" : remoteUser.getUserName()));
93 }
94 Throwable errorThrowable = null;
95 String error = null;
96 Pair<Message, CellScanner> resultPair = null;
97 RpcServer.CurCall.set(call);
98 TraceScope traceScope = null;
99 try {
100 if (!this.rpcServer.isStarted()) {
101 InetSocketAddress address = rpcServer.getListenerAddress();
102 throw new ServerNotRunningYetException("Server " +
103 (address != null ? address : "(channel closed)") + " is not running yet");
104 }
105 if (call.tinfo != null) {
106 traceScope = Trace.startSpan(call.toTraceString(), call.tinfo);
107 }
108
109 resultPair = this.rpcServer.call(call.service, call.md, call.param, call.cellScanner,
110 call.timestamp, this.status);
111 } catch (Throwable e) {
112 RpcServer.LOG.debug(Thread.currentThread().getName() + ": " + call.toShortString(), e);
113 errorThrowable = e;
114 error = StringUtils.stringifyException(e);
115 if (e instanceof Error) {
116 throw (Error)e;
117 }
118 } finally {
119 if (traceScope != null) {
120 traceScope.close();
121 }
122 RpcServer.CurCall.set(null);
123 if (resultPair != null) {
124 this.rpcServer.addCallSize(call.getSize() * -1);
125 sucessful = true;
126 }
127 }
128
129 Message param = resultPair != null ? resultPair.getFirst() : null;
130 CellScanner cells = resultPair != null ? resultPair.getSecond() : null;
131 call.setResponse(param, cells, errorThrowable, error);
132 call.sendResponseIfReady();
133 this.status.markComplete("Sent response");
134 this.status.pause("Waiting for a call");
135 } catch (OutOfMemoryError e) {
136 if (this.rpcServer.getErrorHandler() != null) {
137 if (this.rpcServer.getErrorHandler().checkOOME(e)) {
138 RpcServer.LOG.info(Thread.currentThread().getName() + ": exiting on OutOfMemoryError");
139 return;
140 }
141 } else {
142
143 throw e;
144 }
145 } catch (ClosedChannelException cce) {
146 InetSocketAddress address = rpcServer.getListenerAddress();
147 RpcServer.LOG.warn(Thread.currentThread().getName() + ": caught a ClosedChannelException, " +
148 "this means that the server " + (address != null ? address : "(channel closed)") +
149 " was processing a request but the client went away. The error message was: " +
150 cce.getMessage());
151 } catch (Exception e) {
152 RpcServer.LOG.warn(Thread.currentThread().getName()
153 + ": caught: " + StringUtils.stringifyException(e));
154 } finally {
155 if (!sucessful) {
156 this.rpcServer.addCallSize(call.getSize() * -1);
157 }
158 cleanup();
159 }
160 }
161 }