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.HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT;
021import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_CLIENT_PAUSE;
022import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER;
023import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_CLIENT_SCANNER_CACHING;
024import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE;
025import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD;
026import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_META_SCANNER_CACHING;
027import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
028import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT;
029import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT;
030import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT_DEFAULT;
031import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_OPERATION_TIMEOUT;
032import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_PAUSE;
033import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_RETRIES_NUMBER;
034import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_SCANNER_CACHING;
035import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY;
036import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD;
037import static org.apache.hadoop.hbase.HConstants.HBASE_META_SCANNER_CACHING;
038import static org.apache.hadoop.hbase.HConstants.HBASE_RPC_READ_TIMEOUT_KEY;
039import static org.apache.hadoop.hbase.HConstants.HBASE_RPC_TIMEOUT_KEY;
040import static org.apache.hadoop.hbase.HConstants.HBASE_RPC_WRITE_TIMEOUT_KEY;
041import static org.apache.hadoop.hbase.client.AsyncProcess.DEFAULT_START_LOG_ERRORS_AFTER_COUNT;
042import static org.apache.hadoop.hbase.client.AsyncProcess.START_LOG_ERRORS_AFTER_COUNT_KEY;
043import static org.apache.hadoop.hbase.client.ConnectionConfiguration.PRIMARY_CALL_TIMEOUT_MICROSECOND;
044import static org.apache.hadoop.hbase.client.ConnectionConfiguration.PRIMARY_CALL_TIMEOUT_MICROSECOND_DEFAULT;
045import static org.apache.hadoop.hbase.client.ConnectionConfiguration.PRIMARY_SCAN_TIMEOUT_MICROSECOND;
046import static org.apache.hadoop.hbase.client.ConnectionConfiguration.PRIMARY_SCAN_TIMEOUT_MICROSECOND_DEFAULT;
047import static org.apache.hadoop.hbase.client.ConnectionConfiguration.WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS;
048import static org.apache.hadoop.hbase.client.ConnectionConfiguration.WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT;
049import static org.apache.hadoop.hbase.client.ConnectionConfiguration.WRITE_BUFFER_SIZE_DEFAULT;
050import static org.apache.hadoop.hbase.client.ConnectionConfiguration.WRITE_BUFFER_SIZE_KEY;
051
052import java.util.concurrent.TimeUnit;
053import org.apache.hadoop.conf.Configuration;
054import org.apache.yetus.audience.InterfaceAudience;
055
056/**
057 * Timeout configs.
058 */
059@InterfaceAudience.Private
060class AsyncConnectionConfiguration {
061
062  private final long metaOperationTimeoutNs;
063
064  // timeout for a whole operation such as get, put or delete. Notice that scan will not be effected
065  // by this value, see scanTimeoutNs.
066  private final long operationTimeoutNs;
067
068  // timeout for each rpc request. Can be overridden by a more specific config, such as
069  // readRpcTimeout or writeRpcTimeout.
070  private final long rpcTimeoutNs;
071
072  // timeout for each read rpc request
073  private final long readRpcTimeoutNs;
074
075  // timeout for each write rpc request
076  private final long writeRpcTimeoutNs;
077
078  private final long pauseNs;
079
080  private final int maxRetries;
081
082  /** How many retries are allowed before we start to log */
083  private final int startLogErrorsCnt;
084
085  // As now we have heartbeat support for scan, ideally a scan will never timeout unless the RS is
086  // crash. The RS will always return something before the rpc timeout or scan timeout to tell the
087  // client that it is still alive. The scan timeout is used as operation timeout for every
088  // operations in a scan, such as openScanner or next.
089  private final long scanTimeoutNs;
090
091  private final int scannerCaching;
092
093  private final int metaScannerCaching;
094
095  private final long scannerMaxResultSize;
096
097  private final long writeBufferSize;
098
099  private final long writeBufferPeriodicFlushTimeoutNs;
100
101  // this is for supporting region replica get, if the primary does not finished within this
102  // timeout, we will send request to secondaries.
103  private final long primaryCallTimeoutNs;
104
105  private final long primaryScanTimeoutNs;
106
107  private final long primaryMetaScanTimeoutNs;
108
109  AsyncConnectionConfiguration(Configuration conf) {
110    this.metaOperationTimeoutNs = TimeUnit.MILLISECONDS.toNanos(
111      conf.getLong(HBASE_CLIENT_META_OPERATION_TIMEOUT, DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT));
112    this.operationTimeoutNs = TimeUnit.MILLISECONDS.toNanos(
113      conf.getLong(HBASE_CLIENT_OPERATION_TIMEOUT, DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT));
114    this.rpcTimeoutNs =
115      TimeUnit.MILLISECONDS.toNanos(conf.getLong(HBASE_RPC_TIMEOUT_KEY, DEFAULT_HBASE_RPC_TIMEOUT));
116    this.readRpcTimeoutNs =
117      TimeUnit.MILLISECONDS.toNanos(conf.getLong(HBASE_RPC_READ_TIMEOUT_KEY, rpcTimeoutNs));
118    this.writeRpcTimeoutNs =
119      TimeUnit.MILLISECONDS.toNanos(conf.getLong(HBASE_RPC_WRITE_TIMEOUT_KEY, rpcTimeoutNs));
120    this.pauseNs =
121      TimeUnit.MILLISECONDS.toNanos(conf.getLong(HBASE_CLIENT_PAUSE, DEFAULT_HBASE_CLIENT_PAUSE));
122    this.maxRetries = conf.getInt(HBASE_CLIENT_RETRIES_NUMBER, DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
123    this.startLogErrorsCnt =
124      conf.getInt(START_LOG_ERRORS_AFTER_COUNT_KEY, DEFAULT_START_LOG_ERRORS_AFTER_COUNT);
125    this.scanTimeoutNs = TimeUnit.MILLISECONDS.toNanos(
126        conf.getInt(HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD,
127            DEFAULT_HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD));
128    this.scannerCaching =
129      conf.getInt(HBASE_CLIENT_SCANNER_CACHING, DEFAULT_HBASE_CLIENT_SCANNER_CACHING);
130    this.metaScannerCaching =
131      conf.getInt(HBASE_META_SCANNER_CACHING, DEFAULT_HBASE_META_SCANNER_CACHING);
132    this.scannerMaxResultSize = conf.getLong(HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY,
133      DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE);
134    this.writeBufferSize = conf.getLong(WRITE_BUFFER_SIZE_KEY, WRITE_BUFFER_SIZE_DEFAULT);
135    this.writeBufferPeriodicFlushTimeoutNs =
136      TimeUnit.MILLISECONDS.toNanos(conf.getLong(WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS,
137        WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT));
138    this.primaryCallTimeoutNs = TimeUnit.MICROSECONDS.toNanos(
139      conf.getLong(PRIMARY_CALL_TIMEOUT_MICROSECOND, PRIMARY_CALL_TIMEOUT_MICROSECOND_DEFAULT));
140    this.primaryScanTimeoutNs = TimeUnit.MICROSECONDS.toNanos(
141      conf.getLong(PRIMARY_SCAN_TIMEOUT_MICROSECOND, PRIMARY_SCAN_TIMEOUT_MICROSECOND_DEFAULT));
142    this.primaryMetaScanTimeoutNs =
143      TimeUnit.MICROSECONDS.toNanos(conf.getLong(HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT,
144        HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT_DEFAULT));
145  }
146
147  long getMetaOperationTimeoutNs() {
148    return metaOperationTimeoutNs;
149  }
150
151  long getOperationTimeoutNs() {
152    return operationTimeoutNs;
153  }
154
155  long getRpcTimeoutNs() {
156    return rpcTimeoutNs;
157  }
158
159  long getReadRpcTimeoutNs() {
160    return readRpcTimeoutNs;
161  }
162
163  long getWriteRpcTimeoutNs() {
164    return writeRpcTimeoutNs;
165  }
166
167  long getPauseNs() {
168    return pauseNs;
169  }
170
171  int getMaxRetries() {
172    return maxRetries;
173  }
174
175  int getStartLogErrorsCnt() {
176    return startLogErrorsCnt;
177  }
178
179  long getScanTimeoutNs() {
180    return scanTimeoutNs;
181  }
182
183  int getScannerCaching() {
184    return scannerCaching;
185  }
186
187  int getMetaScannerCaching() {
188    return metaScannerCaching;
189  }
190
191  long getScannerMaxResultSize() {
192    return scannerMaxResultSize;
193  }
194
195  long getWriteBufferSize() {
196    return writeBufferSize;
197  }
198
199  long getWriteBufferPeriodicFlushTimeoutNs() {
200    return writeBufferPeriodicFlushTimeoutNs;
201  }
202
203  long getPrimaryCallTimeoutNs() {
204    return primaryCallTimeoutNs;
205  }
206
207  long getPrimaryScanTimeoutNs() {
208    return primaryScanTimeoutNs;
209  }
210
211  long getPrimaryMetaScanTimeoutNs() {
212    return primaryMetaScanTimeoutNs;
213  }
214}