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.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   * 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 Call call;
43    private RpcServerInterface rpcServer;
44    private MonitoredRPCHandler status;
45  
46    /**
47     * On construction, adds the size of this call to the running count of outstanding call sizes.
48     * Presumption is that we are put on a queue while we wait on an executor to run us.  During this
49     * time we occupy heap.
50     */
51    // The constructor is shutdown so only RpcServer in this class can make one of these.
52    CallRunner(final RpcServerInterface rpcServer, final Call call) {
53      this.call = call;
54      this.rpcServer = rpcServer;
55      // Add size of the call to queue size.
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     * Cleanup after ourselves... let go of references.
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         // make the call
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       // Set the response for undelayed calls and delayed calls with
121       // undelayed responses.
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         // rethrow if no handler
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       // regardless if succesful or not we need to reset the callQueueSize
151       this.rpcServer.addCallSize(call.getSize() * -1);
152       cleanup();
153     }
154   }
155 }