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.client.ConnectionUtils.retries2Attempts;
021
022import java.util.concurrent.TimeUnit;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Base class for all asynchronous table builders.
028 */
029@InterfaceAudience.Private
030abstract class AsyncTableBuilderBase<C extends ScanResultConsumerBase>
031  implements AsyncTableBuilder<C> {
032
033  protected TableName tableName;
034
035  protected long operationTimeoutNs;
036
037  protected long scanTimeoutNs;
038
039  protected long rpcTimeoutNs;
040
041  protected long readRpcTimeoutNs;
042
043  protected long writeRpcTimeoutNs;
044
045  protected long pauseNs;
046
047  protected long pauseNsForServerOverloaded;
048
049  protected int maxAttempts;
050
051  protected int startLogErrorsCnt;
052
053  AsyncTableBuilderBase(TableName tableName, AsyncConnectionConfiguration connConf) {
054    this.tableName = tableName;
055    this.operationTimeoutNs = tableName.isSystemTable()
056      ? connConf.getMetaOperationTimeoutNs()
057      : connConf.getOperationTimeoutNs();
058    this.scanTimeoutNs =
059      tableName.isSystemTable() ? connConf.getMetaScanTimeoutNs() : connConf.getScanTimeoutNs();
060    this.rpcTimeoutNs = connConf.getRpcTimeoutNs();
061    this.readRpcTimeoutNs = tableName.isSystemTable()
062      ? connConf.getMetaReadRpcTimeoutNs()
063      : connConf.getReadRpcTimeoutNs();
064    this.writeRpcTimeoutNs = connConf.getWriteRpcTimeoutNs();
065    this.pauseNs = connConf.getPauseNs();
066    this.pauseNsForServerOverloaded = connConf.getPauseNsForServerOverloaded();
067    this.maxAttempts = retries2Attempts(connConf.getMaxRetries());
068    this.startLogErrorsCnt = connConf.getStartLogErrorsCnt();
069  }
070
071  @Override
072  public AsyncTableBuilderBase<C> setOperationTimeout(long timeout, TimeUnit unit) {
073    this.operationTimeoutNs = unit.toNanos(timeout);
074    return this;
075  }
076
077  @Override
078  public AsyncTableBuilderBase<C> setScanTimeout(long timeout, TimeUnit unit) {
079    this.scanTimeoutNs = unit.toNanos(timeout);
080    return this;
081  }
082
083  @Override
084  public AsyncTableBuilderBase<C> setRpcTimeout(long timeout, TimeUnit unit) {
085    this.rpcTimeoutNs = unit.toNanos(timeout);
086    return this;
087  }
088
089  @Override
090  public AsyncTableBuilderBase<C> setReadRpcTimeout(long timeout, TimeUnit unit) {
091    this.readRpcTimeoutNs = unit.toNanos(timeout);
092    return this;
093  }
094
095  @Override
096  public AsyncTableBuilderBase<C> setWriteRpcTimeout(long timeout, TimeUnit unit) {
097    this.writeRpcTimeoutNs = unit.toNanos(timeout);
098    return this;
099  }
100
101  @Override
102  public AsyncTableBuilderBase<C> setRetryPause(long pause, TimeUnit unit) {
103    this.pauseNs = unit.toNanos(pause);
104    return this;
105  }
106
107  @Override
108  public AsyncTableBuilderBase<C> setRetryPauseForServerOverloaded(long pause, TimeUnit unit) {
109    this.pauseNsForServerOverloaded = unit.toNanos(pause);
110    return this;
111  }
112
113  @Override
114  public AsyncTableBuilderBase<C> setMaxAttempts(int maxAttempts) {
115    this.maxAttempts = maxAttempts;
116    return this;
117  }
118
119  @Override
120  public AsyncTableBuilderBase<C> setStartLogErrorsCnt(int startLogErrorsCnt) {
121    this.startLogErrorsCnt = startLogErrorsCnt;
122    return this;
123  }
124}