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_SCANNER_TIMEOUT_PERIOD;
021import static org.apache.hadoop.hbase.HConstants.DEFAULT_HBASE_META_SCANNER_CACHING;
022import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD;
023import static org.apache.hadoop.hbase.HConstants.HBASE_META_SCANNER_CACHING;
024import static org.apache.hadoop.hbase.HConstants.HBASE_RPC_READ_TIMEOUT_KEY;
025import static org.apache.hadoop.hbase.HConstants.HBASE_RPC_WRITE_TIMEOUT_KEY;
026import static org.apache.hadoop.hbase.client.ConnectionConfiguration.HBASE_CLIENT_META_READ_RPC_TIMEOUT_KEY;
027import static org.apache.hadoop.hbase.client.ConnectionConfiguration.HBASE_CLIENT_META_SCANNER_TIMEOUT;
028
029import java.util.concurrent.TimeUnit;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * Timeout configs.
035 */
036@InterfaceAudience.Private
037class AsyncConnectionConfiguration {
038
039  /**
040   * Configure the number of failures after which the client will start logging. A few failures is
041   * fine: region moved, then is not opened, then is overloaded. We try to have an acceptable
042   * heuristic for the number of errors we don't log. 5 was chosen because we wait for 1s at this
043   * stage.
044   */
045  public static final String START_LOG_ERRORS_AFTER_COUNT_KEY =
046    "hbase.client.start.log.errors.counter";
047  public static final int DEFAULT_START_LOG_ERRORS_AFTER_COUNT = 5;
048
049  private final long metaOperationTimeoutNs;
050
051  // timeout for a whole operation such as get, put or delete. Notice that scan will not be effected
052  // by this value, see scanTimeoutNs.
053  private final long operationTimeoutNs;
054
055  // timeout for each rpc request. Can be overridden by a more specific config, such as
056  // readRpcTimeout or writeRpcTimeout.
057  private final long rpcTimeoutNs;
058
059  // timeout for each read rpc request
060  private final long readRpcTimeoutNs;
061
062  // timeout for each read rpc request against system tables
063  private final long metaReadRpcTimeoutNs;
064
065  // timeout for each write rpc request
066  private final long writeRpcTimeoutNs;
067
068  private final long pauseNs;
069
070  private final long pauseNsForServerOverloaded;
071
072  private final int maxRetries;
073
074  /** How many retries are allowed before we start to log */
075  private final int startLogErrorsCnt;
076
077  // As now we have heartbeat support for scan, ideally a scan will never timeout unless the RS is
078  // crash. The RS will always return something before the rpc timeout or scan timeout to tell the
079  // client that it is still alive. The scan timeout is used as operation timeout for every
080  // operations in a scan, such as openScanner or next.
081  private final long scanTimeoutNs;
082  private final long metaScanTimeoutNs;
083
084  private final int scannerCaching;
085
086  private final int metaScannerCaching;
087
088  private final long scannerMaxResultSize;
089
090  private final long writeBufferSize;
091
092  private final long writeBufferPeriodicFlushTimeoutNs;
093
094  // this is for supporting region replica get, if the primary does not finished within this
095  // timeout, we will send request to secondaries.
096  private final long primaryCallTimeoutNs;
097
098  private final long primaryScanTimeoutNs;
099
100  private final long primaryMetaScanTimeoutNs;
101
102  private final int maxKeyValueSize;
103
104  AsyncConnectionConfiguration(Configuration conf) {
105    ConnectionConfiguration connectionConf = new ConnectionConfiguration(conf);
106
107    // fields we can pull directly from connection configuration
108    this.scannerCaching = connectionConf.getScannerCaching();
109    this.scannerMaxResultSize = connectionConf.getScannerMaxResultSize();
110    this.writeBufferSize = connectionConf.getWriteBufferSize();
111    this.writeBufferPeriodicFlushTimeoutNs = connectionConf.getWriteBufferPeriodicFlushTimeoutMs();
112    this.maxKeyValueSize = connectionConf.getMaxKeyValueSize();
113    this.maxRetries = connectionConf.getRetriesNumber();
114
115    // fields from connection configuration that need to be converted to nanos
116    this.metaOperationTimeoutNs =
117      TimeUnit.MILLISECONDS.toNanos(connectionConf.getMetaOperationTimeout());
118    this.operationTimeoutNs = TimeUnit.MILLISECONDS.toNanos(connectionConf.getOperationTimeout());
119    this.rpcTimeoutNs = TimeUnit.MILLISECONDS.toNanos(connectionConf.getRpcTimeout());
120    long readRpcTimeoutMillis =
121      conf.getLong(HBASE_RPC_READ_TIMEOUT_KEY, connectionConf.getRpcTimeout());
122    this.readRpcTimeoutNs = TimeUnit.MILLISECONDS.toNanos(readRpcTimeoutMillis);
123    this.metaReadRpcTimeoutNs = TimeUnit.MILLISECONDS
124      .toNanos(conf.getLong(HBASE_CLIENT_META_READ_RPC_TIMEOUT_KEY, readRpcTimeoutMillis));
125    this.writeRpcTimeoutNs = TimeUnit.MILLISECONDS
126      .toNanos(conf.getLong(HBASE_RPC_WRITE_TIMEOUT_KEY, connectionConf.getWriteRpcTimeout()));
127    this.pauseNs = TimeUnit.MILLISECONDS.toNanos(connectionConf.getPauseMillis());
128    this.pauseNsForServerOverloaded =
129      TimeUnit.MILLISECONDS.toNanos(connectionConf.getPauseMillisForServerOverloaded());
130    this.primaryCallTimeoutNs =
131      TimeUnit.MICROSECONDS.toNanos(connectionConf.getPrimaryCallTimeoutMicroSecond());
132    this.primaryScanTimeoutNs =
133      TimeUnit.MICROSECONDS.toNanos(connectionConf.getReplicaCallTimeoutMicroSecondScan());
134    this.primaryMetaScanTimeoutNs =
135      TimeUnit.MICROSECONDS.toNanos(connectionConf.getMetaReplicaCallTimeoutMicroSecondScan());
136    long scannerTimeoutMillis = conf.getLong(HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD,
137      DEFAULT_HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD);
138    this.scanTimeoutNs = TimeUnit.MILLISECONDS.toNanos(scannerTimeoutMillis);
139    this.metaScanTimeoutNs = TimeUnit.MILLISECONDS
140      .toNanos(conf.getLong(HBASE_CLIENT_META_SCANNER_TIMEOUT, scannerTimeoutMillis));
141
142    // fields not in connection configuration
143    this.startLogErrorsCnt =
144      conf.getInt(START_LOG_ERRORS_AFTER_COUNT_KEY, DEFAULT_START_LOG_ERRORS_AFTER_COUNT);
145    this.metaScannerCaching =
146      conf.getInt(HBASE_META_SCANNER_CACHING, DEFAULT_HBASE_META_SCANNER_CACHING);
147  }
148
149  long getMetaOperationTimeoutNs() {
150    return metaOperationTimeoutNs;
151  }
152
153  long getOperationTimeoutNs() {
154    return operationTimeoutNs;
155  }
156
157  long getRpcTimeoutNs() {
158    return rpcTimeoutNs;
159  }
160
161  long getReadRpcTimeoutNs() {
162    return readRpcTimeoutNs;
163  }
164
165  long getMetaReadRpcTimeoutNs() {
166    return metaReadRpcTimeoutNs;
167  }
168
169  long getWriteRpcTimeoutNs() {
170    return writeRpcTimeoutNs;
171  }
172
173  long getPauseNs() {
174    return pauseNs;
175  }
176
177  long getPauseNsForServerOverloaded() {
178    return pauseNsForServerOverloaded;
179  }
180
181  int getMaxRetries() {
182    return maxRetries;
183  }
184
185  int getStartLogErrorsCnt() {
186    return startLogErrorsCnt;
187  }
188
189  long getScanTimeoutNs() {
190    return scanTimeoutNs;
191  }
192
193  long getMetaScanTimeoutNs() {
194    return metaScanTimeoutNs;
195  }
196
197  int getScannerCaching() {
198    return scannerCaching;
199  }
200
201  int getMetaScannerCaching() {
202    return metaScannerCaching;
203  }
204
205  long getScannerMaxResultSize() {
206    return scannerMaxResultSize;
207  }
208
209  long getWriteBufferSize() {
210    return writeBufferSize;
211  }
212
213  long getWriteBufferPeriodicFlushTimeoutNs() {
214    return writeBufferPeriodicFlushTimeoutNs;
215  }
216
217  long getPrimaryCallTimeoutNs() {
218    return primaryCallTimeoutNs;
219  }
220
221  long getPrimaryScanTimeoutNs() {
222    return primaryScanTimeoutNs;
223  }
224
225  long getPrimaryMetaScanTimeoutNs() {
226    return primaryMetaScanTimeoutNs;
227  }
228
229  int getMaxKeyValueSize() {
230    return maxKeyValueSize;
231  }
232}