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  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HRegionLocation;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  /**
34   * Implementations call a RegionServer and implement {@link #call(int)}.
35   * Passed to a {@link RpcRetryingCaller} so we retry on fail.
36   * TODO: this class is actually tied to one region, because most of the paths make use of
37   *       the regioninfo part of location when building requests. The only reason it works for
38   *       multi-region requests (e.g. batch) is that they happen to not use the region parts.
39   *       This could be done cleaner (e.g. having a generic parameter and 2 derived classes,
40   *       RegionCallable and actual RegionServerCallable with ServerName.
41   * @param <T> the class that the ServerCallable handles
42   */
43  @InterfaceAudience.Private
44  public abstract class RegionServerCallable<T> implements RetryingCallable<T> {
45    // Public because used outside of this package over in ipc.
46    private static final Log LOG = LogFactory.getLog(RegionServerCallable.class);
47    protected final Connection connection;
48    protected final TableName tableName;
49    protected final byte[] row;
50    protected 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(Connection 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    @Override
73    public void prepare(final boolean reload) throws IOException {
74      try (RegionLocator regionLocator = connection.getRegionLocator(tableName)) {
75        this.location = regionLocator.getRegionLocation(row, reload);
76      }
77      if (this.location == null) {
78        throw new IOException("Failed to find location, tableName=" + tableName +
79          ", row=" + Bytes.toString(row) + ", reload=" + reload);
80      }
81      setStub(getConnection().getClient(this.location.getServerName()));
82    }
83  
84    /**
85     * @return {@link HConnection} instance used by this Callable.
86     */
87    HConnection getConnection() {
88      return (HConnection) this.connection;
89    }
90  
91    protected ClientService.BlockingInterface getStub() {
92      return this.stub;
93    }
94  
95    void setStub(final ClientService.BlockingInterface stub) {
96      this.stub = stub;
97    }
98  
99    protected HRegionLocation getLocation() {
100     return this.location;
101   }
102 
103   protected void setLocation(final HRegionLocation location) {
104     this.location = location;
105   }
106 
107   public TableName getTableName() {
108     return this.tableName;
109   }
110 
111   public byte [] getRow() {
112     return this.row;
113   }
114 
115   @Override
116   public void throwable(Throwable t, boolean retrying) {
117     if (location != null) {
118       getConnection().updateCachedLocations(tableName, location.getRegionInfo().getRegionName(),
119           row, t, location.getServerName());
120     }
121   }
122 
123   @Override
124   public String getExceptionMessageAdditionalDetail() {
125     return "row '" + Bytes.toString(row) + "' on table '" + tableName + "' at " + location;
126   }
127 
128   @Override
129   public long sleep(long pause, int tries) {
130     long sleep = ConnectionUtils.getPauseTime(pause, tries);
131     if (sleep < MIN_WAIT_DEAD_SERVER
132         && (location == null || getConnection().isDeadServer(location.getServerName()))) {
133       sleep = ConnectionUtils.addJitter(MIN_WAIT_DEAD_SERVER, 0.10f);
134     }
135     return sleep;
136   }
137 
138   /**
139    * @return the HRegionInfo for the current region
140    */
141   public HRegionInfo getHRegionInfo() {
142     if (this.location == null) {
143       return null;
144     }
145     return this.location.getRegionInfo();
146   }
147 }