All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.hbase.client.AsyncRpcRetryingCallerFactory Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta-1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase.client;

import static org.apache.hadoop.hbase.HConstants.PRIORITY_UNSET;
import static org.apache.hadoop.hbase.client.ConnectionUtils.calcPriority;
import static org.apache.hadoop.hbase.client.ConnectionUtils.retries2Attempts;
import static org.apache.hbase.thirdparty.com.google.common.base.Preconditions.checkArgument;
import static org.apache.hbase.thirdparty.com.google.common.base.Preconditions.checkNotNull;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
import org.apache.hadoop.hbase.ipc.HBaseRpcController;
import org.apache.yetus.audience.InterfaceAudience;

import org.apache.hbase.thirdparty.io.netty.util.Timer;

import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService;
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse;

/**
 * Factory to create an AsyncRpcRetryCaller.
 * @since 2.0.0
 */
@InterfaceAudience.Private
class AsyncRpcRetryingCallerFactory {

  private final AsyncConnectionImpl conn;

  private final Timer retryTimer;

  public AsyncRpcRetryingCallerFactory(AsyncConnectionImpl conn, Timer retryTimer) {
    this.conn = conn;
    this.retryTimer = retryTimer;
  }

  private abstract class BuilderBase {

    protected long pauseNs = conn.connConf.getPauseNs();

    protected long pauseNsForServerOverloaded = conn.connConf.getPauseNsForServerOverloaded();

    protected int maxAttempts = retries2Attempts(conn.connConf.getMaxRetries());

    protected int startLogErrorsCnt = conn.connConf.getStartLogErrorsCnt();
  }

  public class SingleRequestCallerBuilder extends BuilderBase {

    private TableName tableName;

    private byte[] row;

    private AsyncSingleRequestRpcRetryingCaller.Callable callable;

    private long operationTimeoutNs = -1L;

    private long rpcTimeoutNs = -1L;

    private RegionLocateType locateType = RegionLocateType.CURRENT;

    private int replicaId = RegionReplicaUtil.DEFAULT_REPLICA_ID;

    private int priority = PRIORITY_UNSET;

    private Map requestAttributes = Collections.emptyMap();

    public SingleRequestCallerBuilder table(TableName tableName) {
      this.tableName = tableName;
      return this;
    }

    public SingleRequestCallerBuilder row(byte[] row) {
      this.row = row;
      return this;
    }

    public SingleRequestCallerBuilder
      action(AsyncSingleRequestRpcRetryingCaller.Callable callable) {
      this.callable = callable;
      return this;
    }

    public SingleRequestCallerBuilder operationTimeout(long operationTimeout, TimeUnit unit) {
      this.operationTimeoutNs = unit.toNanos(operationTimeout);
      return this;
    }

    public SingleRequestCallerBuilder rpcTimeout(long rpcTimeout, TimeUnit unit) {
      this.rpcTimeoutNs = unit.toNanos(rpcTimeout);
      return this;
    }

    public SingleRequestCallerBuilder locateType(RegionLocateType locateType) {
      this.locateType = locateType;
      return this;
    }

    public SingleRequestCallerBuilder pause(long pause, TimeUnit unit) {
      this.pauseNs = unit.toNanos(pause);
      return this;
    }

    public SingleRequestCallerBuilder pauseForServerOverloaded(long pause, TimeUnit unit) {
      this.pauseNsForServerOverloaded = unit.toNanos(pause);
      return this;
    }

    public SingleRequestCallerBuilder maxAttempts(int maxAttempts) {
      this.maxAttempts = maxAttempts;
      return this;
    }

    public SingleRequestCallerBuilder startLogErrorsCnt(int startLogErrorsCnt) {
      this.startLogErrorsCnt = startLogErrorsCnt;
      return this;
    }

    public SingleRequestCallerBuilder replicaId(int replicaId) {
      this.replicaId = replicaId;
      return this;
    }

    public SingleRequestCallerBuilder priority(int priority) {
      this.priority = priority;
      return this;
    }

    public SingleRequestCallerBuilder
      setRequestAttributes(Map requestAttributes) {
      this.requestAttributes = requestAttributes;
      return this;
    }

    private void preCheck() {
      checkArgument(replicaId >= 0, "invalid replica id %s", replicaId);
      checkNotNull(tableName, "tableName is null");
      checkNotNull(row, "row is null");
      checkNotNull(locateType, "locateType is null");
      checkNotNull(callable, "action is null");
      this.priority = calcPriority(priority, tableName);
    }

    public AsyncSingleRequestRpcRetryingCaller build() {
      preCheck();
      return new AsyncSingleRequestRpcRetryingCaller<>(retryTimer, conn, tableName, row, replicaId,
        locateType, callable, priority, pauseNs, pauseNsForServerOverloaded, maxAttempts,
        operationTimeoutNs, rpcTimeoutNs, startLogErrorsCnt, requestAttributes);
    }

    /**
     * Shortcut for {@code build().call()}
     */
    public CompletableFuture call() {
      return build().call();
    }
  }

  /**
   * Create retry caller for single action, such as get, put, delete, etc.
   */
  public  SingleRequestCallerBuilder single() {
    return new SingleRequestCallerBuilder<>();
  }

  public class ScanSingleRegionCallerBuilder extends BuilderBase {

    private Long scannerId = null;

    private Scan scan;

    private ScanMetrics scanMetrics;

    private ScanResultCache resultCache;

    private AdvancedScanResultConsumer consumer;

    private ClientService.Interface stub;

    private HRegionLocation loc;

    private boolean isRegionServerRemote;

    private long scannerLeaseTimeoutPeriodNs;

    private long scanTimeoutNs;

    private long rpcTimeoutNs;

    private int priority = PRIORITY_UNSET;

    private Map requestAttributes = Collections.emptyMap();

    public ScanSingleRegionCallerBuilder id(long scannerId) {
      this.scannerId = scannerId;
      return this;
    }

    public ScanSingleRegionCallerBuilder setScan(Scan scan) {
      this.scan = scan;
      this.priority = scan.getPriority();
      return this;
    }

    public ScanSingleRegionCallerBuilder metrics(ScanMetrics scanMetrics) {
      this.scanMetrics = scanMetrics;
      return this;
    }

    public ScanSingleRegionCallerBuilder remote(boolean isRegionServerRemote) {
      this.isRegionServerRemote = isRegionServerRemote;
      return this;
    }

    public ScanSingleRegionCallerBuilder resultCache(ScanResultCache resultCache) {
      this.resultCache = resultCache;
      return this;
    }

    public ScanSingleRegionCallerBuilder consumer(AdvancedScanResultConsumer consumer) {
      this.consumer = consumer;
      return this;
    }

    public ScanSingleRegionCallerBuilder stub(ClientService.Interface stub) {
      this.stub = stub;
      return this;
    }

    public ScanSingleRegionCallerBuilder location(HRegionLocation loc) {
      this.loc = loc;
      return this;
    }

    public ScanSingleRegionCallerBuilder scannerLeaseTimeoutPeriod(long scannerLeaseTimeoutPeriod,
      TimeUnit unit) {
      this.scannerLeaseTimeoutPeriodNs = unit.toNanos(scannerLeaseTimeoutPeriod);
      return this;
    }

    public ScanSingleRegionCallerBuilder scanTimeout(long scanTimeout, TimeUnit unit) {
      this.scanTimeoutNs = unit.toNanos(scanTimeout);
      return this;
    }

    public ScanSingleRegionCallerBuilder rpcTimeout(long rpcTimeout, TimeUnit unit) {
      this.rpcTimeoutNs = unit.toNanos(rpcTimeout);
      return this;
    }

    public ScanSingleRegionCallerBuilder pause(long pause, TimeUnit unit) {
      this.pauseNs = unit.toNanos(pause);
      return this;
    }

    public ScanSingleRegionCallerBuilder pauseForServerOverloaded(long pause, TimeUnit unit) {
      this.pauseNsForServerOverloaded = unit.toNanos(pause);
      return this;
    }

    public ScanSingleRegionCallerBuilder maxAttempts(int maxAttempts) {
      this.maxAttempts = maxAttempts;
      return this;
    }

    public ScanSingleRegionCallerBuilder startLogErrorsCnt(int startLogErrorsCnt) {
      this.startLogErrorsCnt = startLogErrorsCnt;
      return this;
    }

    public ScanSingleRegionCallerBuilder
      setRequestAttributes(Map requestAttributes) {
      this.requestAttributes = requestAttributes;
      return this;
    }

    private void preCheck() {
      checkArgument(scannerId != null, "invalid scannerId %d", scannerId);
      checkNotNull(scan, "scan is null");
      checkNotNull(resultCache, "resultCache is null");
      checkNotNull(consumer, "consumer is null");
      checkNotNull(stub, "stub is null");
      checkNotNull(loc, "location is null");
      this.priority = calcPriority(priority, loc.getRegion().getTable());
    }

    public AsyncScanSingleRegionRpcRetryingCaller build() {
      preCheck();
      return new AsyncScanSingleRegionRpcRetryingCaller(retryTimer, conn, scan, scanMetrics,
        scannerId, resultCache, consumer, stub, loc, isRegionServerRemote, priority,
        scannerLeaseTimeoutPeriodNs, pauseNs, pauseNsForServerOverloaded, maxAttempts,
        scanTimeoutNs, rpcTimeoutNs, startLogErrorsCnt, requestAttributes);
    }

    /**
     * Short cut for {@code build().start(HBaseRpcController, ScanResponse)}.
     */
    public CompletableFuture start(HBaseRpcController controller,
      ScanResponse respWhenOpen) {
      return build().start(controller, respWhenOpen);
    }
  }

  /**
   * Create retry caller for scanning a region.
   */
  public ScanSingleRegionCallerBuilder scanSingleRegion() {
    return new ScanSingleRegionCallerBuilder();
  }

  public class BatchCallerBuilder extends BuilderBase {

    private TableName tableName;

    private List actions;

    private long operationTimeoutNs = -1L;

    private long rpcTimeoutNs = -1L;

    private Map requestAttributes = Collections.emptyMap();

    public BatchCallerBuilder table(TableName tableName) {
      this.tableName = tableName;
      return this;
    }

    public BatchCallerBuilder actions(List actions) {
      this.actions = actions;
      return this;
    }

    public BatchCallerBuilder operationTimeout(long operationTimeout, TimeUnit unit) {
      this.operationTimeoutNs = unit.toNanos(operationTimeout);
      return this;
    }

    public BatchCallerBuilder rpcTimeout(long rpcTimeout, TimeUnit unit) {
      this.rpcTimeoutNs = unit.toNanos(rpcTimeout);
      return this;
    }

    public BatchCallerBuilder pause(long pause, TimeUnit unit) {
      this.pauseNs = unit.toNanos(pause);
      return this;
    }

    public BatchCallerBuilder pauseForServerOverloaded(long pause, TimeUnit unit) {
      this.pauseNsForServerOverloaded = unit.toNanos(pause);
      return this;
    }

    public BatchCallerBuilder maxAttempts(int maxAttempts) {
      this.maxAttempts = maxAttempts;
      return this;
    }

    public BatchCallerBuilder startLogErrorsCnt(int startLogErrorsCnt) {
      this.startLogErrorsCnt = startLogErrorsCnt;
      return this;
    }

    public BatchCallerBuilder setRequestAttributes(Map requestAttributes) {
      this.requestAttributes = requestAttributes;
      return this;
    }

    public  AsyncBatchRpcRetryingCaller build() {
      return new AsyncBatchRpcRetryingCaller<>(retryTimer, conn, tableName, actions, pauseNs,
        pauseNsForServerOverloaded, maxAttempts, operationTimeoutNs, rpcTimeoutNs,
        startLogErrorsCnt, requestAttributes);
    }

    public  List> call() {
      return this. build().call();
    }
  }

  public BatchCallerBuilder batch() {
    return new BatchCallerBuilder();
  }

  public class MasterRequestCallerBuilder extends BuilderBase {
    private AsyncMasterRequestRpcRetryingCaller.Callable callable;

    private long operationTimeoutNs = -1L;

    private long rpcTimeoutNs = -1L;

    private int priority = PRIORITY_UNSET;

    public MasterRequestCallerBuilder
      action(AsyncMasterRequestRpcRetryingCaller.Callable callable) {
      this.callable = callable;
      return this;
    }

    public MasterRequestCallerBuilder operationTimeout(long operationTimeout, TimeUnit unit) {
      this.operationTimeoutNs = unit.toNanos(operationTimeout);
      return this;
    }

    public MasterRequestCallerBuilder rpcTimeout(long rpcTimeout, TimeUnit unit) {
      this.rpcTimeoutNs = unit.toNanos(rpcTimeout);
      return this;
    }

    public MasterRequestCallerBuilder pause(long pause, TimeUnit unit) {
      this.pauseNs = unit.toNanos(pause);
      return this;
    }

    public MasterRequestCallerBuilder pauseForServerOverloaded(long pause, TimeUnit unit) {
      this.pauseNsForServerOverloaded = unit.toNanos(pause);
      return this;
    }

    public MasterRequestCallerBuilder maxAttempts(int maxAttempts) {
      this.maxAttempts = maxAttempts;
      return this;
    }

    public MasterRequestCallerBuilder startLogErrorsCnt(int startLogErrorsCnt) {
      this.startLogErrorsCnt = startLogErrorsCnt;
      return this;
    }

    public MasterRequestCallerBuilder priority(TableName tableName) {
      this.priority = Math.max(priority, ConnectionUtils.getPriority(tableName));
      return this;
    }

    public MasterRequestCallerBuilder priority(int priority) {
      this.priority = Math.max(this.priority, priority);
      return this;
    }

    private void preCheck() {
      checkNotNull(callable, "action is null");
    }

    public AsyncMasterRequestRpcRetryingCaller build() {
      preCheck();
      return new AsyncMasterRequestRpcRetryingCaller(retryTimer, conn, callable, priority,
        pauseNs, pauseNsForServerOverloaded, maxAttempts, operationTimeoutNs, rpcTimeoutNs,
        startLogErrorsCnt);
    }

    /**
     * Shortcut for {@code build().call()}
     */
    public CompletableFuture call() {
      return build().call();
    }
  }

  public  MasterRequestCallerBuilder masterRequest() {
    return new MasterRequestCallerBuilder<>();
  }

  public class AdminRequestCallerBuilder extends BuilderBase {
    // TODO: maybe we can reuse AdminRequestCallerBuild, MasterRequestCallerBuild etc.

    private AsyncAdminRequestRetryingCaller.Callable callable;

    private long operationTimeoutNs = -1L;

    private long rpcTimeoutNs = -1L;

    private ServerName serverName;

    private int priority;

    public AdminRequestCallerBuilder
      action(AsyncAdminRequestRetryingCaller.Callable callable) {
      this.callable = callable;
      return this;
    }

    public AdminRequestCallerBuilder operationTimeout(long operationTimeout, TimeUnit unit) {
      this.operationTimeoutNs = unit.toNanos(operationTimeout);
      return this;
    }

    public AdminRequestCallerBuilder rpcTimeout(long rpcTimeout, TimeUnit unit) {
      this.rpcTimeoutNs = unit.toNanos(rpcTimeout);
      return this;
    }

    public AdminRequestCallerBuilder pause(long pause, TimeUnit unit) {
      this.pauseNs = unit.toNanos(pause);
      return this;
    }

    public AdminRequestCallerBuilder pauseForServerOverloaded(long pause, TimeUnit unit) {
      this.pauseNsForServerOverloaded = unit.toNanos(pause);
      return this;
    }

    public AdminRequestCallerBuilder maxAttempts(int maxAttempts) {
      this.maxAttempts = maxAttempts;
      return this;
    }

    public AdminRequestCallerBuilder startLogErrorsCnt(int startLogErrorsCnt) {
      this.startLogErrorsCnt = startLogErrorsCnt;
      return this;
    }

    public AdminRequestCallerBuilder serverName(ServerName serverName) {
      this.serverName = serverName;
      return this;
    }

    public AdminRequestCallerBuilder priority(int priority) {
      this.priority = priority;
      return this;
    }

    public AsyncAdminRequestRetryingCaller build() {
      return new AsyncAdminRequestRetryingCaller(retryTimer, conn, priority, pauseNs,
        pauseNsForServerOverloaded, maxAttempts, operationTimeoutNs, rpcTimeoutNs,
        startLogErrorsCnt, checkNotNull(serverName, "serverName is null"),
        checkNotNull(callable, "action is null"));
    }

    public CompletableFuture call() {
      return build().call();
    }
  }

  public  AdminRequestCallerBuilder adminRequest() {
    return new AdminRequestCallerBuilder<>();
  }

  public class ServerRequestCallerBuilder extends BuilderBase {

    private AsyncServerRequestRpcRetryingCaller.Callable callable;

    private long operationTimeoutNs = -1L;

    private long rpcTimeoutNs = -1L;

    private ServerName serverName;

    public ServerRequestCallerBuilder
      action(AsyncServerRequestRpcRetryingCaller.Callable callable) {
      this.callable = callable;
      return this;
    }

    public ServerRequestCallerBuilder operationTimeout(long operationTimeout, TimeUnit unit) {
      this.operationTimeoutNs = unit.toNanos(operationTimeout);
      return this;
    }

    public ServerRequestCallerBuilder rpcTimeout(long rpcTimeout, TimeUnit unit) {
      this.rpcTimeoutNs = unit.toNanos(rpcTimeout);
      return this;
    }

    public ServerRequestCallerBuilder pause(long pause, TimeUnit unit) {
      this.pauseNs = unit.toNanos(pause);
      return this;
    }

    public ServerRequestCallerBuilder pauseForServerOverloaded(long pause, TimeUnit unit) {
      this.pauseNsForServerOverloaded = unit.toNanos(pause);
      return this;
    }

    public ServerRequestCallerBuilder maxAttempts(int maxAttempts) {
      this.maxAttempts = maxAttempts;
      return this;
    }

    public ServerRequestCallerBuilder startLogErrorsCnt(int startLogErrorsCnt) {
      this.startLogErrorsCnt = startLogErrorsCnt;
      return this;
    }

    public ServerRequestCallerBuilder serverName(ServerName serverName) {
      this.serverName = serverName;
      return this;
    }

    public AsyncServerRequestRpcRetryingCaller build() {
      return new AsyncServerRequestRpcRetryingCaller(retryTimer, conn, pauseNs,
        pauseNsForServerOverloaded, maxAttempts, operationTimeoutNs, rpcTimeoutNs,
        startLogErrorsCnt, checkNotNull(serverName, "serverName is null"),
        checkNotNull(callable, "action is null"));
    }

    public CompletableFuture call() {
      return build().call();
    }
  }

  public  ServerRequestCallerBuilder serverRequest() {
    return new ServerRequestCallerBuilder<>();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy