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  
21  package org.apache.hadoop.hbase.client;
22  
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.DoNotRetryIOException;
28  import org.apache.hadoop.hbase.HBaseIOException;
29  import org.apache.hadoop.hbase.HRegionLocation;
30  import org.apache.hadoop.hbase.RegionLocations;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
35  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
36  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
37  import org.apache.hadoop.hbase.protobuf.RequestConverter;
38  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
39  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
40  
41  import com.google.protobuf.ServiceException;
42  
43  
44  import java.io.IOException;
45  import java.io.InterruptedIOException;
46  import java.util.Collections;
47  import java.util.List;
48  import java.util.concurrent.CancellationException;
49  import java.util.concurrent.ExecutionException;
50  import java.util.concurrent.Executor;
51  import java.util.concurrent.ExecutorService;
52  import java.util.concurrent.Future;
53  import java.util.concurrent.RunnableFuture;
54  import java.util.concurrent.TimeUnit;
55  import java.util.concurrent.TimeoutException;
56  
57  /**
58   * Caller that goes to replica if the primary region does no answer within a configurable
59   * timeout. If the timeout is reached, it calls all the secondary replicas, and returns
60   * the first answer. If the answer comes from one of the secondary replica, it will
61   * be marked as stale.
62   */
63  @InterfaceAudience.Private
64  public class RpcRetryingCallerWithReadReplicas {
65    static final Log LOG = LogFactory.getLog(RpcRetryingCallerWithReadReplicas.class);
66  
67    protected final ExecutorService pool;
68    protected final ClusterConnection cConnection;
69    protected final Configuration conf;
70    protected final Get get;
71    protected final TableName tableName;
72    protected final int timeBeforeReplicas;
73    private final int callTimeout;
74    private final int retries;
75    private final RpcControllerFactory rpcControllerFactory;
76    private final RpcRetryingCallerFactory rpcRetryingCallerFactory;
77  
78    public RpcRetryingCallerWithReadReplicas(
79        RpcControllerFactory rpcControllerFactory, TableName tableName,
80        ClusterConnection cConnection, final Get get,
81        ExecutorService pool, int retries, int callTimeout,
82        int timeBeforeReplicas) {
83      this.rpcControllerFactory = rpcControllerFactory;
84      this.tableName = tableName;
85      this.cConnection = cConnection;
86      this.conf = cConnection.getConfiguration();
87      this.get = get;
88      this.pool = pool;
89      this.retries = retries;
90      this.callTimeout = callTimeout;
91      this.timeBeforeReplicas = timeBeforeReplicas;
92      this.rpcRetryingCallerFactory = new RpcRetryingCallerFactory(conf);
93    }
94  
95    /**
96     * A RegionServerCallable that takes into account the replicas, i.e.
97     * - the call can be on any replica
98     * - we need to stop retrying when the call is completed
99     * - we can be interrupted
100    */
101   class ReplicaRegionServerCallable extends RegionServerCallable<Result> implements Cancellable {
102     final int id;
103     private final PayloadCarryingRpcController controller;
104 
105     public ReplicaRegionServerCallable(int id, HRegionLocation location) {
106       super(RpcRetryingCallerWithReadReplicas.this.cConnection,
107           RpcRetryingCallerWithReadReplicas.this.tableName, get.getRow());
108       this.id = id;
109       this.location = location;
110       this.controller = rpcControllerFactory.newController();
111       controller.setPriority(tableName);
112     }
113 
114     @Override
115     public void cancel() {
116       controller.startCancel();
117     }
118 
119     /**
120      * Two responsibilities
121      * - if the call is already completed (by another replica) stops the retries.
122      * - set the location to the right region, depending on the replica.
123      */
124     @Override
125     public void prepare(final boolean reload) throws IOException {
126       if (controller.isCanceled()) return;
127 
128       if (Thread.interrupted()) {
129         throw new InterruptedIOException();
130       }
131 
132       if (reload || location == null) {
133         RegionLocations rl = getRegionLocations(false, id, cConnection, tableName, get.getRow());
134         location = id < rl.size() ? rl.getRegionLocation(id) : null;
135       }
136 
137       if (location == null || location.getServerName() == null) {
138         // With this exception, there will be a retry. The location can be null for a replica
139         //  when the table is created or after a split.
140         throw new HBaseIOException("There is no location for replica id #" + id);
141       }
142 
143       ServerName dest = location.getServerName();
144 
145       setStub(cConnection.getClient(dest));
146     }
147 
148     @Override
149     public Result call(int callTimeout) throws Exception {
150       if (controller.isCanceled()) return null;
151 
152       if (Thread.interrupted()) {
153         throw new InterruptedIOException();
154       }
155 
156       byte[] reg = location.getRegionInfo().getRegionName();
157 
158       ClientProtos.GetRequest request =
159           RequestConverter.buildGetRequest(reg, get);
160       controller.setCallTimeout(callTimeout);
161 
162       try {
163         ClientProtos.GetResponse response = getStub().get(controller, request);
164         if (response == null) {
165           return null;
166         }
167         return ProtobufUtil.toResult(response.getResult());
168       } catch (ServiceException se) {
169         throw ProtobufUtil.getRemoteException(se);
170       }
171     }
172 
173     @Override
174     public boolean isCancelled() {
175       return controller.isCanceled();
176     }
177   }
178 
179   /**
180    * Algo:
181    * - we put the query into the execution pool.
182    * - after x ms, if we don't have a result, we add the queries for the secondary replicas
183    * - we take the first answer
184    * - when done, we cancel what's left. Cancelling means:
185    * - removing from the pool if the actual call was not started
186    * - interrupting the call if it has started
187    * Client side, we need to take into account
188    * - a call is not executed immediately after being put into the pool
189    * - a call is a thread. Let's not multiply the number of thread by the number of replicas.
190    * Server side, if we can cancel when it's still in the handler pool, it's much better, as a call
191    * can take some i/o.
192    * <p/>
193    * Globally, the number of retries, timeout and so on still applies, but it's per replica,
194    * not global. We continue until all retries are done, or all timeouts are exceeded.
195    */
196   public synchronized Result call()
197       throws DoNotRetryIOException, InterruptedIOException, RetriesExhaustedException {
198     boolean isTargetReplicaSpecified = (get.getReplicaId() >= 0);
199 
200     RegionLocations rl = getRegionLocations(true, (isTargetReplicaSpecified ? get.getReplicaId()
201         : RegionReplicaUtil.DEFAULT_REPLICA_ID), cConnection, tableName, get.getRow());
202     ResultBoundedCompletionService<Result> cs =
203         new ResultBoundedCompletionService<Result>(this.rpcRetryingCallerFactory, pool, rl.size());
204 
205     if(isTargetReplicaSpecified) {
206       addCallsForReplica(cs, rl, get.getReplicaId(), get.getReplicaId());
207     } else {
208       addCallsForReplica(cs, rl, 0, 0);
209       try {
210         // wait for the timeout to see whether the primary responds back
211         Future<Result> f = cs.poll(timeBeforeReplicas, TimeUnit.MICROSECONDS); // Yes, microseconds
212         if (f != null) {
213           return f.get(); //great we got a response
214         }
215       } catch (ExecutionException e) {
216         throwEnrichedException(e, retries);
217       } catch (CancellationException e) {
218         throw new InterruptedIOException();
219       } catch (InterruptedException e) {
220         throw new InterruptedIOException();
221       }
222 
223       // submit call for the all of the secondaries at once
224       addCallsForReplica(cs, rl, 1, rl.size() - 1);
225     }
226 
227     try {
228       try {
229         Future<Result> f = cs.take();
230         return f.get();
231       } catch (ExecutionException e) {
232         throwEnrichedException(e, retries);
233       }
234     } catch (CancellationException e) {
235       throw new InterruptedIOException();
236     } catch (InterruptedException e) {
237       throw new InterruptedIOException();
238     } finally {
239       // We get there because we were interrupted or because one or more of the
240       // calls succeeded or failed. In all case, we stop all our tasks.
241       cs.cancelAll();
242     }
243 
244     return null; // unreachable
245   }
246 
247   /**
248    * Extract the real exception from the ExecutionException, and throws what makes more
249    * sense.
250    */
251   static void throwEnrichedException(ExecutionException e, int retries)
252       throws RetriesExhaustedException, DoNotRetryIOException {
253     Throwable t = e.getCause();
254     assert t != null; // That's what ExecutionException is about: holding an exception
255 
256     if (t instanceof RetriesExhaustedException) {
257       throw (RetriesExhaustedException) t;
258     }
259 
260     if (t instanceof DoNotRetryIOException) {
261       throw (DoNotRetryIOException) t;
262     }
263 
264     RetriesExhaustedException.ThrowableWithExtraContext qt =
265         new RetriesExhaustedException.ThrowableWithExtraContext(t,
266             EnvironmentEdgeManager.currentTime(), null);
267 
268     List<RetriesExhaustedException.ThrowableWithExtraContext> exceptions =
269         Collections.singletonList(qt);
270 
271     throw new RetriesExhaustedException(retries, exceptions);
272   }
273 
274   /**
275    * Creates the calls and submit them
276    *
277    * @param cs  - the completion service to use for submitting
278    * @param rl  - the region locations
279    * @param min - the id of the first replica, inclusive
280    * @param max - the id of the last replica, inclusive.
281    */
282   private void addCallsForReplica(ResultBoundedCompletionService<Result> cs,
283                                  RegionLocations rl, int min, int max) {
284     for (int id = min; id <= max; id++) {
285       HRegionLocation hrl = rl.getRegionLocation(id);
286       ReplicaRegionServerCallable callOnReplica = new ReplicaRegionServerCallable(id, hrl);
287       cs.submit(callOnReplica, callTimeout, id);
288     }
289   }
290 
291   static RegionLocations getRegionLocations(boolean useCache, int replicaId,
292                  ClusterConnection cConnection, TableName tableName, byte[] row)
293       throws RetriesExhaustedException, DoNotRetryIOException, InterruptedIOException {
294 
295     RegionLocations rl;
296     try {
297       if (!useCache) {
298         rl = cConnection.relocateRegion(tableName, row, replicaId);
299       } else {
300         rl = cConnection.locateRegion(tableName, row, useCache, true, replicaId);
301       }
302     } catch (DoNotRetryIOException e) {
303       throw e;
304     } catch (RetriesExhaustedException e) {
305       throw e;
306     } catch (InterruptedIOException e) {
307       throw e;
308     } catch (IOException e) {
309       throw new RetriesExhaustedException("Can't get the location", e);
310     }
311     if (rl == null) {
312       throw new RetriesExhaustedException("Can't get the locations");
313     }
314 
315     return rl;
316   }
317 }