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