001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.apache.hadoop.hbase.client.ConnectionUtils.retries2Attempts;
021
022import java.util.concurrent.TimeUnit;
023import org.apache.hadoop.hbase.HBaseServerException;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * For creating {@link AsyncTable}.
028 * <p>
029 * The implementation should have default configurations set before returning the builder to user.
030 * So users are free to only set the configs they care about to create a new
031 * AsyncTable/RawAsyncTable instance.
032 * @since 2.0.0
033 */
034@InterfaceAudience.Public
035public interface AsyncTableBuilder<C extends ScanResultConsumerBase> {
036
037  /**
038   * Set timeout for a whole operation such as get, put or delete. Notice that scan will not be
039   * effected by this value, see scanTimeoutNs.
040   * <p>
041   * Operation timeout and max attempt times(or max retry times) are both limitations for retrying,
042   * we will stop retrying when we reach any of the limitations.
043   * @see #setMaxAttempts(int)
044   * @see #setMaxRetries(int)
045   * @see #setScanTimeout(long, TimeUnit)
046   */
047  AsyncTableBuilder<C> setOperationTimeout(long timeout, TimeUnit unit);
048
049  /**
050   * As now we have heartbeat support for scan, ideally a scan will never timeout unless the RS is
051   * crash. The RS will always return something before the rpc timed out or scan timed out to tell
052   * the client that it is still alive. The scan timeout is used as operation timeout for every
053   * operation in a scan, such as openScanner or next.
054   * @see #setScanTimeout(long, TimeUnit)
055   */
056  AsyncTableBuilder<C> setScanTimeout(long timeout, TimeUnit unit);
057
058  /**
059   * Set timeout for each rpc request.
060   * <p>
061   * Notice that this will <strong>NOT</strong> change the rpc timeout for read(get, scan) request
062   * and write request(put, delete).
063   */
064  AsyncTableBuilder<C> setRpcTimeout(long timeout, TimeUnit unit);
065
066  /**
067   * Set timeout for each read(get, scan) rpc request.
068   */
069  AsyncTableBuilder<C> setReadRpcTimeout(long timeout, TimeUnit unit);
070
071  /**
072   * Set timeout for each write(put, delete) rpc request.
073   */
074  AsyncTableBuilder<C> setWriteRpcTimeout(long timeout, TimeUnit unit);
075
076  /**
077   * Set the base pause time for retrying. We use an exponential policy to generate sleep time when
078   * retrying.
079   * @see #setRetryPauseForServerOverloaded(long, TimeUnit)
080   */
081  AsyncTableBuilder<C> setRetryPause(long pause, TimeUnit unit);
082
083  /**
084   * Set the base pause time for retrying when {@link HBaseServerException#isServerOverloaded()}. We
085   * use an exponential policy to generate sleep time when retrying.
086   * <p/>
087   * This value should be greater than the normal pause value which could be set with the above
088   * {@link #setRetryPause(long, TimeUnit)} method, as usually
089   * {@link HBaseServerException#isServerOverloaded()} means the server is overloaded. We just use
090   * the normal pause value for {@link HBaseServerException#isServerOverloaded()} if here you
091   * specify a smaller value.
092   * @see #setRetryPause(long, TimeUnit)
093   * @deprecated Since 2.5.0, will be removed in 4.0.0. Please use
094   *             {@link #setRetryPauseForServerOverloaded(long, TimeUnit)} instead.
095   */
096  @Deprecated
097  default AsyncTableBuilder<C> setRetryPauseForCQTBE(long pause, TimeUnit unit) {
098    return setRetryPauseForServerOverloaded(pause, unit);
099  }
100
101  /**
102   * Set the base pause time for retrying when {@link HBaseServerException#isServerOverloaded()}. We
103   * use an exponential policy to generate sleep time when retrying.
104   * <p/>
105   * This value should be greater than the normal pause value which could be set with the above
106   * {@link #setRetryPause(long, TimeUnit)} method, as usually
107   * {@link HBaseServerException#isServerOverloaded()} means the server is overloaded. We just use
108   * the normal pause value for {@link HBaseServerException#isServerOverloaded()} if here you
109   * specify a smaller value.
110   * @see #setRetryPause(long, TimeUnit)
111   */
112  AsyncTableBuilder<C> setRetryPauseForServerOverloaded(long pause, TimeUnit unit);
113
114  /**
115   * Set the max retry times for an operation. Usually it is the max attempt times minus 1.
116   * <p>
117   * Operation timeout and max attempt times(or max retry times) are both limitations for retrying,
118   * we will stop retrying when we reach any of the limitations.
119   * @see #setMaxAttempts(int)
120   * @see #setOperationTimeout(long, TimeUnit)
121   */
122  default AsyncTableBuilder<C> setMaxRetries(int maxRetries) {
123    return setMaxAttempts(retries2Attempts(maxRetries));
124  }
125
126  /**
127   * Set the max attempt times for an operation. Usually it is the max retry times plus 1. Operation
128   * timeout and max attempt times(or max retry times) are both limitations for retrying, we will
129   * stop retrying when we reach any of the limitations.
130   * @see #setMaxRetries(int)
131   * @see #setOperationTimeout(long, TimeUnit)
132   */
133  AsyncTableBuilder<C> setMaxAttempts(int maxAttempts);
134
135  /**
136   * Set the number of retries that are allowed before we start to log.
137   */
138  AsyncTableBuilder<C> setStartLogErrorsCnt(int startLogErrorsCnt);
139
140  /**
141   * Set a request attribute
142   */
143  AsyncTableBuilder<C> setRequestAttribute(String key, byte[] value);
144
145  /**
146   * Create the {@link AsyncTable} instance.
147   */
148  AsyncTable<C> build();
149}