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