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