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 = connConf.getScanTimeoutNs();
059    this.rpcTimeoutNs = connConf.getRpcTimeoutNs();
060    this.readRpcTimeoutNs = connConf.getReadRpcTimeoutNs();
061    this.writeRpcTimeoutNs = connConf.getWriteRpcTimeoutNs();
062    this.pauseNs = connConf.getPauseNs();
063    this.pauseNsForServerOverloaded = connConf.getPauseNsForServerOverloaded();
064    this.maxAttempts = retries2Attempts(connConf.getMaxRetries());
065    this.startLogErrorsCnt = connConf.getStartLogErrorsCnt();
066  }
067
068  @Override
069  public AsyncTableBuilderBase<C> setOperationTimeout(long timeout, TimeUnit unit) {
070    this.operationTimeoutNs = unit.toNanos(timeout);
071    return this;
072  }
073
074  @Override
075  public AsyncTableBuilderBase<C> setScanTimeout(long timeout, TimeUnit unit) {
076    this.scanTimeoutNs = unit.toNanos(timeout);
077    return this;
078  }
079
080  @Override
081  public AsyncTableBuilderBase<C> setRpcTimeout(long timeout, TimeUnit unit) {
082    this.rpcTimeoutNs = unit.toNanos(timeout);
083    return this;
084  }
085
086  @Override
087  public AsyncTableBuilderBase<C> setReadRpcTimeout(long timeout, TimeUnit unit) {
088    this.readRpcTimeoutNs = unit.toNanos(timeout);
089    return this;
090  }
091
092  @Override
093  public AsyncTableBuilderBase<C> setWriteRpcTimeout(long timeout, TimeUnit unit) {
094    this.writeRpcTimeoutNs = unit.toNanos(timeout);
095    return this;
096  }
097
098  @Override
099  public AsyncTableBuilderBase<C> setRetryPause(long pause, TimeUnit unit) {
100    this.pauseNs = unit.toNanos(pause);
101    return this;
102  }
103
104  @Override
105  public AsyncTableBuilderBase<C> setRetryPauseForServerOverloaded(long pause, TimeUnit unit) {
106    this.pauseNsForServerOverloaded = unit.toNanos(pause);
107    return this;
108  }
109
110  @Override
111  public AsyncTableBuilderBase<C> setMaxAttempts(int maxAttempts) {
112    this.maxAttempts = maxAttempts;
113    return this;
114  }
115
116  @Override
117  public AsyncTableBuilderBase<C> setStartLogErrorsCnt(int startLogErrorsCnt) {
118    this.startLogErrorsCnt = startLogErrorsCnt;
119    return this;
120  }
121}