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