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