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