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.CallDroppedException;
25  import org.apache.hadoop.hbase.CellScanner;
26  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.apache.hadoop.hbase.ipc.RpcServer.Call;
30  import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandler;
31  import org.apache.hadoop.hbase.util.Pair;
32  import org.apache.hadoop.security.UserGroupInformation;
33  import org.apache.hadoop.util.StringUtils;
34  import org.apache.htrace.Trace;
35  import org.apache.htrace.TraceScope;
36  
37  import com.google.protobuf.Message;
38  
39  /**
40   * The request processing logic, which is usually executed in thread pools provided by an
41   * {@link RpcScheduler}.  Call {@link #run()} to actually execute the contained
42   * RpcServer.Call
43   */
44  @InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX})
45  @InterfaceStability.Evolving
46  public class CallRunner {
47    private static final Log LOG = LogFactory.getLog(CallRunner.class);
48  
49    private static final CallDroppedException CALL_DROPPED_EXCEPTION
50      = new CallDroppedException();
51  
52    private Call call;
53    private RpcServerInterface rpcServer;
54    private MonitoredRPCHandler status;
55    private volatile boolean sucessful;
56  
57    /**
58     * On construction, adds the size of this call to the running count of outstanding call sizes.
59     * Presumption is that we are put on a queue while we wait on an executor to run us.  During this
60     * time we occupy heap.
61     */
62    // The constructor is shutdown so only RpcServer in this class can make one of these.
63    CallRunner(final RpcServerInterface rpcServer, final Call call) {
64      this.call = call;
65      this.rpcServer = rpcServer;
66      // Add size of the call to queue size.
67      if (call != null && rpcServer != null) {
68        this.rpcServer.addCallSize(call.getSize());
69      }
70    }
71  
72    public Call getCall() {
73      return call;
74    }
75  
76    public void setStatus(MonitoredRPCHandler status) {
77      this.status = status;
78    }
79  
80    /**
81     * Cleanup after ourselves... let go of references.
82     */
83    private void cleanup() {
84      this.call = null;
85      this.rpcServer = null;
86    }
87  
88    public void run() {
89      try {
90        if (!call.connection.channel.isOpen()) {
91          if (RpcServer.LOG.isDebugEnabled()) {
92            RpcServer.LOG.debug(Thread.currentThread().getName() + ": skipped " + call);
93          }
94          return;
95        }
96        call.startTime = System.currentTimeMillis();
97        if (call.startTime > call.deadline) {
98          RpcServer.LOG.info("Drop timeout call: " + call);
99          return;
100       }
101       this.status.setStatus("Setting up call");
102       this.status.setConnection(call.connection.getHostAddress(), call.connection.getRemotePort());
103       if (RpcServer.LOG.isTraceEnabled()) {
104         UserGroupInformation remoteUser = call.connection.ugi;
105         RpcServer.LOG.trace(call.toShortString() + " executing as " +
106             ((remoteUser == null) ? "NULL principal" : remoteUser.getUserName()));
107       }
108       Throwable errorThrowable = null;
109       String error = null;
110       Pair<Message, CellScanner> resultPair = null;
111       RpcServer.CurCall.set(call);
112       TraceScope traceScope = null;
113       try {
114         if (!this.rpcServer.isStarted()) {
115           InetSocketAddress address = rpcServer.getListenerAddress();
116           throw new ServerNotRunningYetException("Server " +
117               (address != null ? address : "(channel closed)") + " is not running yet");
118         }
119         if (call.tinfo != null) {
120           traceScope = Trace.startSpan(call.toTraceString(), call.tinfo);
121         }
122         // make the call
123         resultPair = this.rpcServer.call(call.service, call.md, call.param, call.cellScanner,
124           call.timestamp, this.status, call.startTime, call.timeout);
125       } catch (Throwable e) {
126         RpcServer.LOG.debug(Thread.currentThread().getName() + ": " + call.toShortString(), e);
127         errorThrowable = e;
128         error = StringUtils.stringifyException(e);
129         if (e instanceof Error) {
130           throw (Error)e;
131         }
132       } finally {
133         if (traceScope != null) {
134           traceScope.close();
135         }
136         RpcServer.CurCall.set(null);
137         if (resultPair != null) {
138           this.rpcServer.addCallSize(call.getSize() * -1);
139           sucessful = true;
140         }
141       }
142       // Set the response
143       Message param = resultPair != null ? resultPair.getFirst() : null;
144       CellScanner cells = resultPair != null ? resultPair.getSecond() : null;
145       call.setResponse(param, cells, errorThrowable, error);
146       call.sendResponseIfReady();
147       this.status.markComplete("Sent response");
148       this.status.pause("Waiting for a call");
149     } catch (OutOfMemoryError e) {
150       if (this.rpcServer.getErrorHandler() != null) {
151         if (this.rpcServer.getErrorHandler().checkOOME(e)) {
152           RpcServer.LOG.info(Thread.currentThread().getName() + ": exiting on OutOfMemoryError");
153           return;
154         }
155       } else {
156         // rethrow if no handler
157         throw e;
158       }
159     } catch (ClosedChannelException cce) {
160       InetSocketAddress address = rpcServer.getListenerAddress();
161       RpcServer.LOG.warn(Thread.currentThread().getName() + ": caught a ClosedChannelException, " +
162           "this means that the server " + (address != null ? address : "(channel closed)") +
163           " was processing a request but the client went away. The error message was: " +
164           cce.getMessage());
165     } catch (Exception e) {
166       RpcServer.LOG.warn(Thread.currentThread().getName()
167           + ": caught: " + StringUtils.stringifyException(e));
168     } finally {
169       if (!sucessful) {
170         this.rpcServer.addCallSize(call.getSize() * -1);
171       }
172       cleanup();
173     }
174   }
175 
176   /**
177    * When we want to drop this call because of server is overloaded.
178    */
179   public void drop() {
180     try {
181       if (!call.connection.channel.isOpen()) {
182         if (RpcServer.LOG.isDebugEnabled()) {
183           RpcServer.LOG.debug(Thread.currentThread().getName() + ": skipped " + call);
184         }
185         return;
186       }
187 
188       // Set the response
189       InetSocketAddress address = rpcServer.getListenerAddress();
190       call.setResponse(null, null, CALL_DROPPED_EXCEPTION, "Call dropped, server "
191         + (address != null ? address : "(channel closed)") + " is overloaded, please retry.");
192       call.sendResponseIfReady();
193     } catch (ClosedChannelException cce) {
194       InetSocketAddress address = rpcServer.getListenerAddress();
195       RpcServer.LOG.warn(Thread.currentThread().getName() + ": caught a ClosedChannelException, " +
196         "this means that the server " + (address != null ? address : "(channel closed)") +
197         " was processing a request but the client went away. The error message was: " +
198         cce.getMessage());
199     } catch (Exception e) {
200       RpcServer.LOG.warn(Thread.currentThread().getName()
201         + ": caught: " + StringUtils.stringifyException(e));
202     } finally {
203       if (!sucessful) {
204         this.rpcServer.addCallSize(call.getSize() * -1);
205       }
206       cleanup();
207     }
208   }
209 }