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