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
018/**
019 * Configuration parameters for the connection.
020 * Configuration is a heavy weight registry that does a lot of string operations and regex matching.
021 * Method calls into Configuration account for high CPU usage and have huge performance impact.
022 * This class caches connection-related configuration values in the  ConnectionConfiguration
023 * object so that expensive conf.getXXX() calls are avoided every time HTable, etc is instantiated.
024 * see HBASE-12128
025 */
026@InterfaceAudience.Private
027public class ConnectionConfiguration {
028
029  public static final String WRITE_BUFFER_SIZE_KEY = "hbase.client.write.buffer";
030  public static final long WRITE_BUFFER_SIZE_DEFAULT = 2097152;
031  public static final String WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS =
032          "hbase.client.write.buffer.periodicflush.timeout.ms";
033  public static final String WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS =
034          "hbase.client.write.buffer.periodicflush.timertick.ms";
035  public static final long WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT = 0; // 0 == Disabled
036  public static final long WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS_DEFAULT = 1000L; // 1 second
037  public static final String MAX_KEYVALUE_SIZE_KEY = "hbase.client.keyvalue.maxsize";
038  public static final int MAX_KEYVALUE_SIZE_DEFAULT = 10485760;
039  public static final String PRIMARY_CALL_TIMEOUT_MICROSECOND =
040    "hbase.client.primaryCallTimeout.get";
041  public static final int PRIMARY_CALL_TIMEOUT_MICROSECOND_DEFAULT = 10000; // 10ms
042  public static final String PRIMARY_SCAN_TIMEOUT_MICROSECOND =
043    "hbase.client.replicaCallTimeout.scan";
044  public static final int PRIMARY_SCAN_TIMEOUT_MICROSECOND_DEFAULT = 1000000; // 1s
045
046  private final long writeBufferSize;
047  private final long writeBufferPeriodicFlushTimeoutMs;
048  private final long writeBufferPeriodicFlushTimerTickMs;
049  private final int metaOperationTimeout;
050  private final int operationTimeout;
051  private final int scannerCaching;
052  private final long scannerMaxResultSize;
053  private final int primaryCallTimeoutMicroSecond;
054  private final int replicaCallTimeoutMicroSecondScan;
055  private final int metaReplicaCallTimeoutMicroSecondScan;
056  private final int retries;
057  private final int maxKeyValueSize;
058  private final int rpcTimeout;
059  private final int readRpcTimeout;
060  private final int writeRpcTimeout;
061  // toggle for async/sync prefetch
062  private final boolean clientScannerAsyncPrefetch;
063
064  /**
065   * Constructor
066   * @param conf Configuration object
067   */
068  ConnectionConfiguration(Configuration conf) {
069    this.writeBufferSize = conf.getLong(WRITE_BUFFER_SIZE_KEY, WRITE_BUFFER_SIZE_DEFAULT);
070
071    this.writeBufferPeriodicFlushTimeoutMs = conf.getLong(
072            WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS,
073            WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT);
074
075    this.writeBufferPeriodicFlushTimerTickMs = conf.getLong(
076            WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS,
077            WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS_DEFAULT);
078
079    this.metaOperationTimeout = conf.getInt(HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT,
080        HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
081
082    this.operationTimeout = conf.getInt(
083      HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
084
085    this.scannerCaching = conf.getInt(
086      HConstants.HBASE_CLIENT_SCANNER_CACHING, HConstants.DEFAULT_HBASE_CLIENT_SCANNER_CACHING);
087
088    this.scannerMaxResultSize =
089        conf.getLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY,
090            HConstants.DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE);
091
092    this.primaryCallTimeoutMicroSecond =
093      conf.getInt(PRIMARY_CALL_TIMEOUT_MICROSECOND, PRIMARY_CALL_TIMEOUT_MICROSECOND_DEFAULT);
094
095    this.replicaCallTimeoutMicroSecondScan =
096      conf.getInt(PRIMARY_SCAN_TIMEOUT_MICROSECOND, PRIMARY_SCAN_TIMEOUT_MICROSECOND_DEFAULT);
097
098    this.metaReplicaCallTimeoutMicroSecondScan =
099      conf.getInt(HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT,
100        HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT_DEFAULT);
101
102    this.retries = conf.getInt(
103       HConstants.HBASE_CLIENT_RETRIES_NUMBER, HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
104
105    this.clientScannerAsyncPrefetch = conf.getBoolean(
106       Scan.HBASE_CLIENT_SCANNER_ASYNC_PREFETCH, Scan.DEFAULT_HBASE_CLIENT_SCANNER_ASYNC_PREFETCH);
107
108    this.maxKeyValueSize = conf.getInt(MAX_KEYVALUE_SIZE_KEY, MAX_KEYVALUE_SIZE_DEFAULT);
109
110    this.rpcTimeout =
111        conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT);
112
113    this.readRpcTimeout = conf.getInt(HConstants.HBASE_RPC_READ_TIMEOUT_KEY,
114        conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT));
115
116    this.writeRpcTimeout = conf.getInt(HConstants.HBASE_RPC_WRITE_TIMEOUT_KEY,
117        conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT));
118  }
119
120  /**
121   * Constructor
122   * This is for internal testing purpose (using the default value).
123   * In real usage, we should read the configuration from the Configuration object.
124   */
125  protected ConnectionConfiguration() {
126    this.writeBufferSize = WRITE_BUFFER_SIZE_DEFAULT;
127    this.writeBufferPeriodicFlushTimeoutMs = WRITE_BUFFER_PERIODIC_FLUSH_TIMEOUT_MS_DEFAULT;
128    this.writeBufferPeriodicFlushTimerTickMs = WRITE_BUFFER_PERIODIC_FLUSH_TIMERTICK_MS_DEFAULT;
129    this.metaOperationTimeout = HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT;
130    this.operationTimeout = HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT;
131    this.scannerCaching = HConstants.DEFAULT_HBASE_CLIENT_SCANNER_CACHING;
132    this.scannerMaxResultSize = HConstants.DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE;
133    this.primaryCallTimeoutMicroSecond = 10000;
134    this.replicaCallTimeoutMicroSecondScan = 1000000;
135    this.metaReplicaCallTimeoutMicroSecondScan =
136        HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT_DEFAULT;
137    this.retries = HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER;
138    this.clientScannerAsyncPrefetch = Scan.DEFAULT_HBASE_CLIENT_SCANNER_ASYNC_PREFETCH;
139    this.maxKeyValueSize = MAX_KEYVALUE_SIZE_DEFAULT;
140    this.readRpcTimeout = HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
141    this.writeRpcTimeout = HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
142    this.rpcTimeout = HConstants.DEFAULT_HBASE_RPC_TIMEOUT;
143  }
144
145  public int getReadRpcTimeout() {
146    return readRpcTimeout;
147  }
148
149  public int getWriteRpcTimeout() {
150    return writeRpcTimeout;
151  }
152
153  public long getWriteBufferSize() {
154    return writeBufferSize;
155  }
156
157  public long getWriteBufferPeriodicFlushTimeoutMs() {
158    return writeBufferPeriodicFlushTimeoutMs;
159  }
160
161  public long getWriteBufferPeriodicFlushTimerTickMs() {
162    return writeBufferPeriodicFlushTimerTickMs;
163  }
164
165  public int getMetaOperationTimeout() {
166    return metaOperationTimeout;
167  }
168
169  public int getOperationTimeout() {
170    return operationTimeout;
171  }
172
173  public int getScannerCaching() {
174    return scannerCaching;
175  }
176
177  public int getPrimaryCallTimeoutMicroSecond() {
178    return primaryCallTimeoutMicroSecond;
179  }
180
181  public int getReplicaCallTimeoutMicroSecondScan() {
182    return replicaCallTimeoutMicroSecondScan;
183  }
184
185  public int getMetaReplicaCallTimeoutMicroSecondScan() {
186    return metaReplicaCallTimeoutMicroSecondScan;
187  }
188
189  public int getRetriesNumber() {
190    return retries;
191  }
192
193  public int getMaxKeyValueSize() {
194    return maxKeyValueSize;
195  }
196
197  public long getScannerMaxResultSize() {
198    return scannerMaxResultSize;
199  }
200
201  public boolean isClientScannerAsyncPrefetch() {
202    return clientScannerAsyncPrefetch;
203  }
204
205  public int getRpcTimeout() {
206    return rpcTimeout;
207  }
208}