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.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   * The request processing logic, which is usually executed in thread pools provided by an
38   * {@link RpcScheduler}.  Call {@link #run()} to actually execute the contained
39   * RpcServer.Call
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     * On construction, adds the size of this call to the running count of outstanding call sizes.
52     * Presumption is that we are put on a queue while we wait on an executor to run us.  During this
53     * time we occupy heap.
54     */
55    // The constructor is shutdown so only RpcServer in this class can make one of these.
56    CallRunner(final RpcServerInterface rpcServer, final Call call) {
57      this.call = call;
58      this.rpcServer = rpcServer;
59      // Add size of the call to queue size.
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     * Cleanup after ourselves... let go of references.
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         // make the call
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       // Set the response
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         // rethrow if no handler
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 }