View Javadoc

1   package org.apache.hadoop.hbase.ipc;
2   /**
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * The request processing logic, which is usually executed in thread pools provided by an
37   * {@link RpcScheduler}.  Call {@link #run()} to actually execute the contained
38   * {@link RpcServer.Call}
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     * On construction, adds the size of this call to the running count of outstanding call sizes.
49     * Presumption is that we are put on a queue while we wait on an executor to run us.  During this
50     * time we occupy heap.
51     * @param call The call to run.
52     * @param rpcServer
53     */
54    // The constructor is shutdown so only RpcServer in this class can make one of these.
55    CallRunner(final RpcServerInterface rpcServer, final Call call, UserProvider userProvider) {
56      this.call = call;
57      this.rpcServer = rpcServer;
58      // Add size of the call to queue size.
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          // make the call
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         // Must always clear the request context to avoid leaking
103         // credentials between requests.
104         RequestContext.clear();
105       }
106       RpcServer.CurCall.set(null);
107       this.rpcServer.addCallSize(call.getSize() * -1);
108       // Set the response for undelayed calls and delayed calls with
109       // undelayed responses.
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         // rethrow if no handler
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     // It is ugly the way we park status up in RpcServer.  Let it be for now.  TODO.
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 }