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