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.util.FutureUtils.addListener;
021
022import java.io.IOException;
023import java.util.Optional;
024import java.util.concurrent.CompletableFuture;
025import org.apache.hadoop.hbase.HRegionLocation;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.ipc.HBaseRpcController;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.yetus.audience.InterfaceAudience;
030
031import org.apache.hbase.thirdparty.io.netty.util.Timer;
032
033import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService;
034
035/**
036 * Retry caller for a single request, such as get, put, delete, etc.
037 */
038@InterfaceAudience.Private
039class AsyncSingleRequestRpcRetryingCaller<T> extends AsyncRpcRetryingCaller<T> {
040
041  @FunctionalInterface
042  public interface Callable<T> {
043    CompletableFuture<T> call(HBaseRpcController controller, HRegionLocation loc,
044      ClientService.Interface stub);
045  }
046
047  private final TableName tableName;
048
049  private final byte[] row;
050
051  private final int replicaId;
052
053  private final RegionLocateType locateType;
054
055  private final Callable<T> callable;
056
057  public AsyncSingleRequestRpcRetryingCaller(Timer retryTimer, AsyncConnectionImpl conn,
058    TableName tableName, byte[] row, int replicaId, RegionLocateType locateType,
059    Callable<T> callable, int priority, long pauseNs, long pauseNsForServerOverloaded,
060    int maxAttempts, long operationTimeoutNs, long rpcTimeoutNs, int startLogErrorsCnt) {
061    super(retryTimer, conn, priority, pauseNs, pauseNsForServerOverloaded, maxAttempts,
062      operationTimeoutNs, rpcTimeoutNs, startLogErrorsCnt);
063    this.tableName = tableName;
064    this.row = row;
065    this.replicaId = replicaId;
066    this.locateType = locateType;
067    this.callable = callable;
068  }
069
070  private void call(HRegionLocation loc) {
071    ClientService.Interface stub;
072    try {
073      stub = conn.getRegionServerStub(loc.getServerName());
074    } catch (IOException e) {
075      onError(e,
076        () -> "Get async stub to " + loc.getServerName() + " for '" + Bytes.toStringBinary(row)
077          + "' in " + loc.getRegion().getEncodedName() + " of " + tableName + " failed",
078        err -> conn.getLocator().updateCachedLocationOnError(loc, err));
079      return;
080    }
081    resetCallTimeout();
082    addListener(callable.call(controller, loc, stub), (result, error) -> {
083      if (error != null) {
084        onError(error,
085          () -> "Call to " + loc.getServerName() + " for '" + Bytes.toStringBinary(row) + "' in "
086            + loc.getRegion().getEncodedName() + " of " + tableName + " failed",
087          err -> conn.getLocator().updateCachedLocationOnError(loc, err));
088        return;
089      }
090      future.complete(result);
091    });
092  }
093
094  @Override
095  protected void doCall() {
096    long locateTimeoutNs;
097    if (operationTimeoutNs > 0) {
098      locateTimeoutNs = remainingTimeNs();
099      if (locateTimeoutNs <= 0) {
100        completeExceptionally();
101        return;
102      }
103    } else {
104      locateTimeoutNs = -1L;
105    }
106    addListener(
107      conn.getLocator().getRegionLocation(tableName, row, replicaId, locateType, locateTimeoutNs),
108      (loc, error) -> {
109        if (error != null) {
110          onError(error,
111            () -> "Locate '" + Bytes.toStringBinary(row) + "' in " + tableName + " failed", err -> {
112            });
113          return;
114        }
115        call(loc);
116      });
117  }
118
119  @Override
120  protected Optional<TableName> getTableName() {
121    return Optional.of(tableName);
122  }
123}