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.ServerName;
026import org.apache.hadoop.hbase.ipc.HBaseRpcController;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.io.netty.util.Timer;
030
031import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService;
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, int priority,
045    long pauseNs, long pauseNsForServerOverloaded, int maxAttempts, long operationTimeoutNs,
046    long rpcTimeoutNs, int startLogErrorsCnt, ServerName serverName, Callable<T> callable) {
047    super(retryTimer, conn, priority, pauseNs, pauseNsForServerOverloaded, maxAttempts,
048      operationTimeoutNs, rpcTimeoutNs, startLogErrorsCnt, Collections.emptyMap());
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    addListener(callable.call(controller, adminStub), (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}