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 java.io.IOException;
021import java.util.concurrent.CompletableFuture;
022import org.apache.hadoop.hbase.ServerName;
023import org.apache.hadoop.hbase.ipc.HBaseRpcController;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hbase.thirdparty.io.netty.util.Timer;
027
028import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService;
029
030/**
031 * Retry caller for a request call to region server.
032 * Now only used for coprocessor call to region server.
033 */
034@InterfaceAudience.Private
035public class AsyncServerRequestRpcRetryingCaller<T> extends AsyncRpcRetryingCaller<T> {
036
037  @FunctionalInterface
038  public interface Callable<T> {
039    CompletableFuture<T> call(HBaseRpcController controller, ClientService.Interface stub);
040  }
041
042  private final Callable<T> callable;
043  private ServerName serverName;
044
045  public AsyncServerRequestRpcRetryingCaller(Timer retryTimer, AsyncConnectionImpl conn,
046      long pauseNs, int maxAttempts, long operationTimeoutNs, long rpcTimeoutNs,
047      int startLogErrorsCnt, ServerName serverName, Callable<T> callable) {
048    super(retryTimer, conn, pauseNs, maxAttempts, operationTimeoutNs, rpcTimeoutNs,
049        startLogErrorsCnt);
050    this.serverName = serverName;
051    this.callable = callable;
052  }
053
054  @Override
055  protected void doCall() {
056    ClientService.Interface stub;
057    try {
058      stub = this.conn.getRegionServerStub(serverName);
059    } catch (IOException e) {
060      onError(e, () -> "Get async admin stub to " + serverName + " failed", err -> {
061      });
062      return;
063    }
064    resetCallTimeout();
065    callable.call(controller, stub).whenComplete((result, error) -> {
066      if (error != null) {
067        onError(error, () -> "Call to admin stub failed", err -> {
068        });
069        return;
070      }
071      future.complete(result);
072    });
073  }
074}