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