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 java.io.IOException;
23  import java.net.ConnectException;
24  import java.net.SocketTimeoutException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.classification.InterfaceStability;
30  import org.apache.hadoop.hbase.HRegionLocation;
31  import org.apache.hadoop.hbase.NotServingRegionException;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.exceptions.RegionMovedException;
34  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  /**
38   * Implementations call a RegionServer and implement {@link #call()}.
39   * Passed to a {@link RpcRetryingCaller} so we retry on fail.
40   * @param <T> the class that the ServerCallable handles
41   */
42  @InterfaceAudience.Public
43  @InterfaceStability.Stable
44  public abstract class RegionServerCallable<T> implements RetryingCallable<T> {
45    // Public because used outside of this package over in ipc.
46    static final Log LOG = LogFactory.getLog(RegionServerCallable.class);
47    private final HConnection connection;
48    private final TableName tableName;
49    private final byte [] row;
50    private HRegionLocation location;
51    private ClientService.BlockingInterface stub;
52  
53    protected final static int MIN_WAIT_DEAD_SERVER = 10000;
54  
55    /**
56     * @param connection Connection to use.
57     * @param tableName Table name to which <code>row</code> belongs.
58     * @param row The row we want in <code>tableName</code>.
59     */
60    public RegionServerCallable(HConnection connection, TableName tableName, byte [] row) {
61      this.connection = connection;
62      this.tableName = tableName;
63      this.row = row;
64    }
65  
66    /**
67     * Prepare for connection to the server hosting region with row from tablename.  Does lookup
68     * to find region location and hosting server.
69     * @param reload Set this to true if connection should re-find the region
70     * @throws IOException e
71     */
72    public void prepare(final boolean reload) throws IOException {
73      this.location = connection.getRegionLocation(tableName, row, reload);
74      if (this.location == null) {
75        throw new IOException("Failed to find location, tableName=" + tableName +
76          ", row=" + Bytes.toString(row) + ", reload=" + reload);
77      }
78      setStub(getConnection().getClient(getLocation().getServerName()));
79    }
80  
81    /**
82     * @return {@link HConnection} instance used by this Callable.
83     */
84    HConnection getConnection() {
85      return this.connection;
86    }
87  
88    protected ClientService.BlockingInterface getStub() {
89      return this.stub;
90    }
91  
92    void setStub(final ClientService.BlockingInterface stub) {
93      this.stub = stub;
94    }
95  
96    protected HRegionLocation getLocation() {
97      return this.location;
98    }
99  
100   protected void setLocation(final HRegionLocation location) {
101     this.location = location;
102   }
103 
104   public TableName getTableName() {
105     return this.tableName;
106   }
107 
108   public byte [] getRow() {
109     return this.row;
110   }
111 
112   @Override
113   public void throwable(Throwable t, boolean retrying) {
114     if (t instanceof SocketTimeoutException ||
115         t instanceof ConnectException ||
116         t instanceof RetriesExhaustedException ||
117         (location != null && getConnection().isDeadServer(location.getServerName()))) {
118       // if thrown these exceptions, we clear all the cache entries that
119       // map to that slow/dead server; otherwise, let cache miss and ask
120       // .META. again to find the new location
121       getConnection().clearCaches(location.getServerName());
122     } else if (t instanceof RegionMovedException) {
123       getConnection().updateCachedLocations(tableName, row, t, location);
124     } else if (t instanceof NotServingRegionException && !retrying) {
125       // Purge cache entries for this specific region from META cache
126       // since we don't call connect(true) when number of retries is 1.
127       getConnection().deleteCachedRegionLocation(location);
128     }
129   }
130 
131   @Override
132   public String getExceptionMessageAdditionalDetail() {
133     return "row '" + Bytes.toString(row) + "' on table '" + tableName;
134   }
135 
136   @Override
137   public long sleep(long pause, int tries) {
138     // Tries hasn't been bumped up yet so we use "tries + 1" to get right pause time
139     long sleep = ConnectionUtils.getPauseTime(pause, tries + 1);
140     if (sleep < MIN_WAIT_DEAD_SERVER 
141         && (location == null || getConnection().isDeadServer(location.getServerName()))) {
142       sleep = ConnectionUtils.addJitter(MIN_WAIT_DEAD_SERVER, 0.10f);
143     }
144     return sleep;
145   }
146 }