001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
003 * agreements. See the NOTICE file distributed with this work for additional information regarding
004 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
005 * "License"); you may not use this file except in compliance with the License. You may obtain a
006 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
007 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
008 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
009 * for the specific language governing permissions and limitations under the License.
010 */
011
012package org.apache.hadoop.hbase.client;
013
014import org.apache.hadoop.conf.Configuration;
015import org.apache.hadoop.hbase.HConstants;
016import org.apache.yetus.audience.InterfaceAudience;
017
018import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
019
020/**
021 * Configuration parameters for the connection.
022 * Configuration is a heavy weight registry that does a lot of string operations and regex matching.
023 * Method calls into Configuration account for high CPU usage and have huge performance impact.
024 * This class caches connection-related configuration values in the  ConnectionConfiguration
025 * object so that expensive conf.getXXX() calls are avoided every time HTable, etc is instantiated.
026 * see HBASE-12128
027 */
028@InterfaceAudience.Private
029public class ConnectionConfiguration {
030
031  public static final String WRITE_BUFFER_SIZE_KEY = "hbase.client.write.buffer";
032  public static final long WRITE_BUFFER_SIZE_DEFAULT = 2097152;
033  public static final String WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS =
034          "hbase.client.write.buffer.periodicflush.timeout.ms";
035  public static final String WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS =
036          "hbase.client.write.buffer.periodicflush.timertick.ms";
037  public static final long WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT = 0; // 0 == Disabled
038  public static final long WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS_DEFAULT = 1000L; // 1 second
039  public static final String MAX_KEYVALUE_SIZE_KEY = "hbase.client.keyvalue.maxsize";
040  public static final int MAX_KEYVALUE_SIZE_DEFAULT = 10485760;
041  public static final String PRIMARY_CALL_TIMEOUT_MICROSECOND =
042    "hbase.client.primaryCallTimeout.get";
043  public static final int PRIMARY_CALL_TIMEOUT_MICROSECOND_DEFAULT = 10000; // 10ms
044  public static final String PRIMARY_SCAN_TIMEOUT_MICROSECOND =
045    "hbase.client.replicaCallTimeout.scan";
046  public static final int PRIMARY_SCAN_TIMEOUT_MICROSECOND_DEFAULT = 1000000; // 1s
047
048  private final long writeBufferSize;
049  private final long writeBufferPeriodicFlushTimeoutMs;
050  private final long writeBufferPeriodicFlushTimerTickMs;
051  private final int metaOperationTimeout;
052  private final int operationTimeout;
053  private final int scannerCaching;
054  private final long scannerMaxResultSize;
055  private final int primaryCallTimeoutMicroSecond;
056  private final int replicaCallTimeoutMicroSecondScan;
057  private final int metaReplicaCallTimeoutMicroSecondScan;
058  private final int retries;
059  private final int maxKeyValueSize;
060  private final int rpcTimeout;
061  private final int readRpcTimeout;
062  private final int writeRpcTimeout;
063  // toggle for async/sync prefetch
064  private final boolean clientScannerAsyncPrefetch;
065
066    /**
067   * Constructor
068   * @param conf Configuration object
069   */
070  ConnectionConfiguration(Configuration conf) {
071    this.writeBufferSize = conf.getLong(WRITE_BUFFER_SIZE_KEY, WRITE_BUFFER_SIZE_DEFAULT);
072
073    this.writeBufferPeriodicFlushTimeoutMs = conf.getLong(
074            WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS,
075            WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT);
076
077    this.writeBufferPeriodicFlushTimerTickMs = conf.getLong(
078            WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS,
079            WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS_DEFAULT);
080
081    this.metaOperationTimeout = conf.getInt(HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT,
082        HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
083
084    this.operationTimeout = conf.getInt(
085      HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
086
087    this.scannerCaching = conf.getInt(
088      HConstants.HBASE_CLIENT_SCANNER_CACHING, HConstants.DEFAULT_HBASE_CLIENT_SCANNER_CACHING);
089
090    this.scannerMaxResultSize =
091        conf.getLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY,
092            HConstants.DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE);
093
094    this.primaryCallTimeoutMicroSecond =
095      conf.getInt(PRIMARY_CALL_TIMEOUT_MICROSECOND, PRIMARY_CALL_TIMEOUT_MICROSECOND_DEFAULT);
096
097    this.replicaCallTimeoutMicroSecondScan =
098      conf.getInt(PRIMARY_SCAN_TIMEOUT_MICROSECOND, PRIMARY_SCAN_TIMEOUT_MICROSECOND_DEFAULT);
099
100    this.metaReplicaCallTimeoutMicroSecondScan =
101      conf.getInt(HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT,
102        HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT_DEFAULT);
103
104    this.retries = conf.getInt(
105       HConstants.HBASE_CLIENT_RETRIES_NUMBER, HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
106
107    this.clientScannerAsyncPrefetch = conf.getBoolean(
108       Scan.HBASE_CLIENT_SCANNER_ASYNC_PREFETCH, Scan.DEFAULT_HBASE_CLIENT_SCANNER_ASYNC_PREFETCH);
109
110    this.maxKeyValueSize = conf.getInt(MAX_KEYVALUE_SIZE_KEY, MAX_KEYVALUE_SIZE_DEFAULT);
111
112    this.rpcTimeout =
113        conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT);
114
115    this.readRpcTimeout = conf.getInt(HConstants.HBASE_RPC_READ_TIMEOUT_KEY,
116        conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT));
117
118    this.writeRpcTimeout = conf.getInt(HConstants.HBASE_RPC_WRITE_TIMEOUT_KEY,
119        conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT));
120  }
121
122  /**
123   * Constructor
124   * This is for internal testing purpose (using the default value).
125   * In real usage, we should read the configuration from the Configuration object.
126   */
127  @VisibleForTesting
128  protected ConnectionConfiguration() {
129    this.writeBufferSize = WRITE_BUFFER_SIZE_DEFAULT;
130    this.writeBufferPeriodicFlushTimeoutMs = WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT;
131    this.writeBufferPeriodicFlushTimerTickMs = WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS_DEFAULT;
132    this.metaOperationTimeout = HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT;
133    this.operationTimeout = HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT;
134    this.scannerCaching = HConstants.DEFAULT_HBASE_CLIENT_SCANNER_CACHING;
135    this.scannerMaxResultSize = HConstants.DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE;
136    this.primaryCallTimeoutMicroSecond = 10000;
137    this.replicaCallTimeoutMicroSecondScan = 1000000;
138    this.metaReplicaCallTimeoutMicroSecondScan =
139        HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT_DEFAULT;
140    this.retries = HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER;
141    this.clientScannerAsyncPrefetch = Scan.DEFAULT_HBASE_CLIENT_SCANNER_ASYNC_PREFETCH;
142    this.maxKeyValueSize = MAX_KEYVALUE_SIZE_DEFAULT;
143    this.readRpcTimeout = HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
144    this.writeRpcTimeout = HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
145    this.rpcTimeout = HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
146  }
147
148  public int getReadRpcTimeout() {
149    return readRpcTimeout;
150  }
151
152  public int getWriteRpcTimeout() {
153    return writeRpcTimeout;
154  }
155
156  public long getWriteBufferSize() {
157    return writeBufferSize;
158  }
159
160  public long getWriteBufferPeriodicFlushTimeoutMs() {
161    return writeBufferPeriodicFlushTimeoutMs;
162  }
163
164  public long getWriteBufferPeriodicFlushTimerTickMs() {
165    return writeBufferPeriodicFlushTimerTickMs;
166  }
167
168  public int getMetaOperationTimeout() {
169    return metaOperationTimeout;
170  }
171
172  public int getOperationTimeout() {
173    return operationTimeout;
174  }
175
176  public int getScannerCaching() {
177    return scannerCaching;
178  }
179
180  public int getPrimaryCallTimeoutMicroSecond() {
181    return primaryCallTimeoutMicroSecond;
182  }
183
184  public int getReplicaCallTimeoutMicroSecondScan() {
185    return replicaCallTimeoutMicroSecondScan;
186  }
187
188  public int getMetaReplicaCallTimeoutMicroSecondScan() {
189    return metaReplicaCallTimeoutMicroSecondScan;
190  }
191
192  public int getRetriesNumber() {
193    return retries;
194  }
195
196  public int getMaxKeyValueSize() {
197    return maxKeyValueSize;
198  }
199
200  public long getScannerMaxResultSize() {
201    return scannerMaxResultSize;
202  }
203
204  public boolean isClientScannerAsyncPrefetch() {
205    return clientScannerAsyncPrefetch;
206  }
207
208  public int getRpcTimeout() {
209    return rpcTimeout;
210  }
211
212}