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