001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.client;
021
022import java.io.IOException;
023
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * A Callable<T> that will be retried. If {@link #call(int)} invocation throws exceptions,
028 * we will call {@link #throwable(Throwable, boolean)} with whatever the exception was.
029 * @param <T> result class from executing <tt>this</tt>
030 */
031@InterfaceAudience.Private
032public interface RetryingCallable<T> {
033  /**
034   * Prepare by setting up any connections to servers, etc., ahead of call invocation.
035   * TODO: We call prepare before EVERY call. Seems wrong. FIX!!!!
036   * @param reload Set this to true if need to requery locations
037   * @throws IOException e
038   */
039  void prepare(final boolean reload) throws IOException;
040
041  /**
042   * Called when call throws an exception and we are going to retry; take action to
043   * make it so we succeed on next call (clear caches, do relookup of locations, etc.).
044   * @param t        throwable which was thrown
045   * @param retrying True if we are in retrying mode (we are not in retrying mode when max
046   *                 retries == 1; we ARE in retrying mode if retries &gt; 1 even when we are the
047   *                 last attempt)
048   */
049  void throwable(final Throwable t, boolean retrying);
050
051  /**
052   * @return Some details from the implementation that we would like to add to a terminating
053   *         exception; i.e. a fatal exception is being thrown ending retries and we might like to
054   *         add more implementation-specific detail on to the exception being thrown.
055   */
056  String getExceptionMessageAdditionalDetail();
057
058  /**
059   * @param pause time to pause
060   * @param tries amount of tries until till sleep
061   * @return Suggestion on how much to sleep between retries
062   */
063  long sleep(final long pause, final int tries);
064
065  /**
066   * Computes a result, or throws an exception if unable to do so.
067   *
068   * @param callTimeout - the time available for this call. 0 for infinite.
069   * @return computed result
070   * @throws Exception if unable to compute a result
071   */
072  T call(int callTimeout) throws Exception;
073}