View Javadoc

1   /**
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  
20  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import com.google.protobuf.ServiceException;
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HRegionLocation;
30  import org.apache.hadoop.hbase.exceptions.DoNotRetryIOException;
31  import org.apache.hadoop.hbase.ipc.HBaseClientRPC;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
34  import org.apache.hadoop.ipc.RemoteException;
35  
36  import java.io.IOException;
37  import java.lang.reflect.UndeclaredThrowableException;
38  import java.net.ConnectException;
39  import java.net.SocketTimeoutException;
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.concurrent.Callable;
43  
44  /**
45   * Abstract class that implements {@link Callable}.  Implementation stipulates
46   * return type and method we actually invoke on remote Server.  Usually
47   * used inside a try/catch that fields usual connection failures all wrapped
48   * up in a retry loop.
49   * <p>Call {@link #connect(boolean)} to connect to server hosting region
50   * that contains the passed row in the passed table before invoking
51   * {@link #call()}.
52   * @see HConnection#getRegionServerWithoutRetries(ServerCallable)
53   * @param <T> the class that the ServerCallable handles
54   */
55  @InterfaceAudience.Public
56  @InterfaceStability.Stable
57  public abstract class ServerCallable<T> implements Callable<T> {
58    static final Log LOG = LogFactory.getLog(ServerCallable.class);
59  
60    protected final HConnection connection;
61    protected final byte [] tableName;
62    protected final byte [] row;
63    protected HRegionLocation location;
64    protected ClientProtocol server;
65    protected int callTimeout;
66    protected long globalStartTime;
67    protected long startTime, endTime;
68    protected final static int MIN_RPC_TIMEOUT = 2000;
69    protected final static int MIN_WAIT_DEAD_SERVER = 10000;
70  
71    /**
72     * @param connection Connection to use.
73     * @param tableName Table name to which <code>row</code> belongs.
74     * @param row The row we want in <code>tableName</code>.
75     */
76    public ServerCallable(HConnection connection, byte [] tableName, byte [] row) {
77      this(connection, tableName, row, HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
78    }
79  
80    public ServerCallable(HConnection connection, byte [] tableName, byte [] row, int callTimeout) {
81      this.connection = connection;
82      this.tableName = tableName;
83      this.row = row;
84      this.callTimeout = callTimeout;
85    }
86  
87    /**
88     * Connect to the server hosting region with row from tablename.
89     * @param reload Set this to true if connection should re-find the region
90     * @throws IOException e
91     */
92    public void connect(final boolean reload) throws IOException {
93      this.location = connection.getRegionLocation(tableName, row, reload);
94      this.server = connection.getClient(location.getServerName());
95    }
96  
97    /** @return the server name
98     * @deprecated Just use {@link #toString()} instead.
99     */
100   public String getServerName() {
101     if (location == null) return null;
102     return location.getHostnamePort();
103   }
104 
105   /** @return the region name
106    * @deprecated Just use {@link #toString()} instead.
107    */
108   public byte[] getRegionName() {
109     if (location == null) return null;
110     return location.getRegionInfo().getRegionName();
111   }
112 
113   /** @return the row
114    * @deprecated Just use {@link #toString()} instead.
115    */
116   public byte [] getRow() {
117     return row;
118   }
119 
120   public void beforeCall() {
121     this.startTime = EnvironmentEdgeManager.currentTimeMillis();
122     int remaining = (int)(callTimeout - (this.startTime - this.globalStartTime));
123     if (remaining < MIN_RPC_TIMEOUT) {
124       // If there is no time left, we're trying anyway. It's too late.
125       // 0 means no timeout, and it's not the intent here. So we secure both cases by
126       // resetting to the minimum.
127       remaining = MIN_RPC_TIMEOUT;
128     }
129     HBaseClientRPC.setRpcTimeout(remaining);
130   }
131 
132   public void afterCall() {
133     HBaseClientRPC.resetRpcTimeout();
134     this.endTime = EnvironmentEdgeManager.currentTimeMillis();
135   }
136 
137   /**
138    * @return {@link HConnection} instance used by this Callable.
139    */
140   HConnection getConnection() {
141     return this.connection;
142   }
143 
144   /**
145    * Run this instance with retries, timed waits,
146    * and refinds of missing regions.
147    *
148    * @return an object of type T
149    * @throws IOException if a remote or network exception occurs
150    * @throws RuntimeException other unspecified error
151    */
152   public T withRetries()
153   throws IOException, RuntimeException {
154     Configuration c = getConnection().getConfiguration();
155     final long pause = c.getLong(HConstants.HBASE_CLIENT_PAUSE,
156       HConstants.DEFAULT_HBASE_CLIENT_PAUSE);
157     final int numRetries = c.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
158       HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
159     List<RetriesExhaustedException.ThrowableWithExtraContext> exceptions =
160       new ArrayList<RetriesExhaustedException.ThrowableWithExtraContext>();
161     this.globalStartTime = EnvironmentEdgeManager.currentTimeMillis();
162     for (int tries = 0;; tries++) {
163       long expectedSleep = 0;
164       try {
165         beforeCall();
166         connect(tries != 0); // if called with false, check table status on ZK
167         return call();
168       } catch (Throwable t) {
169         LOG.warn("Received exception, tries=" + tries + ", numRetries=" + numRetries +
170             " message=" + t.getMessage());
171 
172         t = translateException(t);
173         // translateException throws an exception when we should not retry, i.e. when it's the
174         //  request that is bad.
175 
176         if (t instanceof SocketTimeoutException ||
177             t instanceof ConnectException ||
178             t instanceof RetriesExhaustedException ||
179             getConnection().isDeadServer(location.getServerName())) {
180           // if thrown these exceptions, we clear all the cache entries that
181           // map to that slow/dead server; otherwise, let cache miss and ask
182           // .META. again to find the new location
183           getConnection().clearCaches(location.getServerName());
184         }
185 
186         RetriesExhaustedException.ThrowableWithExtraContext qt =
187           new RetriesExhaustedException.ThrowableWithExtraContext(t,
188               EnvironmentEdgeManager.currentTimeMillis(), toString());
189         exceptions.add(qt);
190         if (tries >= numRetries - 1) {
191           throw new RetriesExhaustedException(tries, exceptions);
192         }
193 
194         // If the server is dead, we need to wait a little before retrying, to give
195         //  a chance to the regions to be
196         expectedSleep = ConnectionUtils.getPauseTime(pause, tries);
197         if (expectedSleep < MIN_WAIT_DEAD_SERVER &&
198             getConnection().isDeadServer(location.getServerName())){
199           expectedSleep = ConnectionUtils.addJitter(MIN_WAIT_DEAD_SERVER, 0.10f);
200         }
201 
202         // If, after the planned sleep, there won't be enough time left, we stop now.
203         if (((this.endTime - this.globalStartTime) + MIN_RPC_TIMEOUT + expectedSleep) >
204             this.callTimeout) {
205           throw (SocketTimeoutException) new SocketTimeoutException(
206               "Call to access row '" + Bytes.toString(row) + "' on table '"
207                   + Bytes.toString(tableName)
208                   + "' failed on timeout. " + " callTimeout=" + this.callTimeout +
209                   ", time=" + (this.endTime - this.startTime)).initCause(t);
210         }
211       } finally {
212         afterCall();
213       }
214       try {
215         Thread.sleep(expectedSleep);
216       } catch (InterruptedException e) {
217         Thread.currentThread().interrupt();
218         throw new IOException("Interrupted after " + tries + " tries  on " + numRetries, e);
219       }
220     }
221   }
222 
223   /**
224    * Run this instance against the server once.
225    * @return an object of type T
226    * @throws IOException if a remote or network exception occurs
227    * @throws RuntimeException other unspecified error
228    */
229   public T withoutRetries()
230   throws IOException, RuntimeException {
231     // The code of this method should be shared with withRetries.
232     this.globalStartTime = EnvironmentEdgeManager.currentTimeMillis();
233     try {
234       beforeCall();
235       connect(false);
236       return call();
237     } catch (Throwable t) {
238       Throwable t2 = translateException(t);
239       // It would be nice to clear the location cache here.
240       if (t2 instanceof IOException) {
241         throw (IOException)t2;
242       } else {
243         throw new RuntimeException(t2);
244       }
245     } finally {
246       afterCall();
247     }
248   }
249 
250   /**
251    * Get the good or the remote exception if any, throws the DoNotRetryIOException.
252    * @param t the throwable to analyze
253    * @return the translated exception, if it's not a DoNotRetryIOException
254    * @throws DoNotRetryIOException - if we find it, we throw it instead of translating.
255    */
256   protected static Throwable translateException(Throwable t) throws DoNotRetryIOException {
257     if (t instanceof UndeclaredThrowableException) {
258       t = t.getCause();
259     }
260     if (t instanceof RemoteException) {
261       t = ((RemoteException)t).unwrapRemoteException();
262     }
263     if (t instanceof ServiceException) {
264       ServiceException se = (ServiceException)t;
265       Throwable cause = se.getCause();
266       if (cause != null && cause instanceof DoNotRetryIOException) {
267         throw (DoNotRetryIOException)cause;
268       }
269     } else if (t instanceof DoNotRetryIOException) {
270       throw (DoNotRetryIOException)t;
271     }
272     return t;
273   }
274 }