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    /**
54     * @param connection Connection to use.
55     * @param tableName Table name to which <code>row</code> belongs.
56     * @param row The row we want in <code>tableName</code>.
57     */
58    public RegionServerCallable(Connection connection, TableName tableName, byte [] row) {
59      this.connection = connection;
60      this.tableName = tableName;
61      this.row = row;
62    }
63  
64    /**
65     * Prepare for connection to the server hosting region with row from tablename.  Does lookup
66     * to find region location and hosting server.
67     * @param reload Set this to true if connection should re-find the region
68     * @throws IOException e
69     */
70    @Override
71    public void prepare(final boolean reload) throws IOException {
72      try (RegionLocator regionLocator = connection.getRegionLocator(tableName)) {
73        this.location = regionLocator.getRegionLocation(row, reload);
74      }
75      if (this.location == null) {
76        throw new IOException("Failed to find location, tableName=" + tableName +
77          ", row=" + Bytes.toString(row) + ", reload=" + reload);
78      }
79      setStub(getConnection().getClient(this.location.getServerName()));
80    }
81  
82    /**
83     * @return {@link HConnection} instance used by this Callable.
84     */
85    HConnection getConnection() {
86      return (HConnection) this.connection;
87    }
88  
89    protected ClientService.BlockingInterface getStub() {
90      return this.stub;
91    }
92  
93    void setStub(final ClientService.BlockingInterface stub) {
94      this.stub = stub;
95    }
96  
97    protected HRegionLocation getLocation() {
98      return this.location;
99    }
100 
101   protected void setLocation(final HRegionLocation location) {
102     this.location = location;
103   }
104 
105   public TableName getTableName() {
106     return this.tableName;
107   }
108 
109   public byte [] getRow() {
110     return this.row;
111   }
112 
113   @Override
114   public void throwable(Throwable t, boolean retrying) {
115     if (location != null) {
116       getConnection().updateCachedLocations(tableName, location.getRegionInfo().getRegionName(),
117           row, t, location.getServerName());
118     }
119   }
120 
121   @Override
122   public String getExceptionMessageAdditionalDetail() {
123     return "row '" + Bytes.toString(row) + "' on table '" + tableName + "' at " + location;
124   }
125 
126   @Override
127   public long sleep(long pause, int tries) {
128     return ConnectionUtils.getPauseTime(pause, tries);
129   }
130 
131   /**
132    * @return the HRegionInfo for the current region
133    */
134   public HRegionInfo getHRegionInfo() {
135     if (this.location == null) {
136       return null;
137     }
138     return this.location.getRegionInfo();
139   }
140 }