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