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.util.Pair;
27  import org.apache.hadoop.security.UserGroupInformation;
28  import org.apache.hadoop.util.StringUtils;
29  import org.cloudera.htrace.Trace;
30  import org.cloudera.htrace.TraceScope;
31  
32  import com.google.protobuf.Message;
33  
34  /**
35   * The request processing logic, which is usually executed in thread pools provided by an
36   * {@link RpcScheduler}.  Call {@link #run()} to actually execute the contained
37   * {@link RpcServer.Call}
38   */
39  @InterfaceAudience.Private
40  public class CallRunner {
41    private Call call;
42    private RpcServerInterface rpcServer;
43    private MonitoredRPCHandler status;
44  
45    /**
46     * On construction, adds the size of this call to the running count of outstanding call sizes.
47     * Presumption is that we are put on a queue while we wait on an executor to run us.  During this
48     * time we occupy heap.
49     */
50    // The constructor is shutdown so only RpcServer in this class can make one of these.
51    CallRunner(final RpcServerInterface rpcServer, final Call call) {
52      this.call = call;
53      this.rpcServer = rpcServer;
54      // Add size of the call to queue size.
55      this.rpcServer.addCallSize(call.getSize());
56    }
57  
58    public Call getCall() {
59      return call;
60    }
61  
62    public void setStatus(MonitoredRPCHandler status) {
63      this.status = status;
64    }
65  
66    /**
67     * Cleanup after ourselves... let go of references.
68     */
69    private void cleanup() {
70      this.call = null;
71      this.rpcServer = null;
72    }
73  
74    public void run() {
75      try {
76        if (!call.connection.channel.isOpen()) {
77          if (RpcServer.LOG.isDebugEnabled()) {
78            RpcServer.LOG.debug(Thread.currentThread().getName() + ": skipped " + call);
79          }
80          return;
81        }
82        this.status.setStatus("Setting up call");
83        this.status.setConnection(call.connection.getHostAddress(), call.connection.getRemotePort());
84        if (RpcServer.LOG.isTraceEnabled()) {
85          UserGroupInformation remoteUser = call.connection.ugi;
86          RpcServer.LOG.trace(call.toShortString() + " executing as " +
87              ((remoteUser == null) ? "NULL principal" : remoteUser.getUserName()));
88        }
89        Throwable errorThrowable = null;
90        String error = null;
91        Pair<Message, CellScanner> resultPair = null;
92        RpcServer.CurCall.set(call);
93        TraceScope traceScope = null;
94        try {
95          if (!this.rpcServer.isStarted()) {
96            InetSocketAddress address = rpcServer.getListenerAddress();
97            throw new ServerNotRunningYetException("Server " +
98                (address != null ? address : "(channel closed)") + " is not running yet");
99          }
100         if (call.tinfo != null) {
101           traceScope = Trace.startSpan(call.toTraceString(), call.tinfo);
102         }
103         // make the call
104         resultPair = this.rpcServer.call(call.service, call.md, call.param, call.cellScanner,
105           call.timestamp, this.status);
106       } catch (Throwable e) {
107         RpcServer.LOG.debug(Thread.currentThread().getName() + ": " + call.toShortString(), e);
108         errorThrowable = e;
109         error = StringUtils.stringifyException(e);
110         if (e instanceof Error) {
111           throw (Error)e;
112         } 
113       } finally {
114         if (traceScope != null) {
115           traceScope.close();
116         }
117       }
118       RpcServer.CurCall.set(null);
119       // Set the response for undelayed calls and delayed calls with
120       // undelayed responses.
121       if (!call.isDelayed() || !call.isReturnValueDelayed()) {
122         Message param = resultPair != null ? resultPair.getFirst() : null;
123         CellScanner cells = resultPair != null ? resultPair.getSecond() : null;
124         call.setResponse(param, cells, errorThrowable, error);
125       }
126       call.sendResponseIfReady();
127       this.status.markComplete("Sent response");
128       this.status.pause("Waiting for a call");
129     } catch (OutOfMemoryError e) {
130       if (this.rpcServer.getErrorHandler() != null) {
131         if (this.rpcServer.getErrorHandler().checkOOME(e)) {
132           RpcServer.LOG.info(Thread.currentThread().getName() + ": exiting on OutOfMemoryError");
133           return;
134         }
135       } else {
136         // rethrow if no handler
137         throw e;
138       }
139     } catch (ClosedChannelException cce) {
140       InetSocketAddress address = rpcServer.getListenerAddress();
141       RpcServer.LOG.warn(Thread.currentThread().getName() + ": caught a ClosedChannelException, " +
142           "this means that the server " + (address != null ? address : "(channel closed)") +
143           " was processing a request but the client went away. The error message was: " +
144           cce.getMessage());
145     } catch (Exception e) {
146       RpcServer.LOG.warn(Thread.currentThread().getName()
147           + ": caught: " + StringUtils.stringifyException(e));
148     } finally {
149       // regardless if succesful or not we need to reset the callQueueSize
150       this.rpcServer.addCallSize(call.getSize() * -1);
151       cleanup();
152     }
153   }
154 }