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.DoNotRetryIOException;
026import org.apache.hadoop.hbase.NotServingRegionException;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.ipc.HBaseRpcController;
029import org.apache.yetus.audience.InterfaceAudience;
030
031import org.apache.hbase.thirdparty.io.netty.util.Timer;
032
033import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService;
034
035@InterfaceAudience.Private
036public class AsyncAdminRequestRetryingCaller<T> extends AsyncRpcRetryingCaller<T> {
037
038  @FunctionalInterface
039  public interface Callable<T> {
040    CompletableFuture<T> call(HBaseRpcController controller, AdminService.Interface stub);
041  }
042
043  private final Callable<T> callable;
044  private ServerName serverName;
045
046  public AsyncAdminRequestRetryingCaller(Timer retryTimer, AsyncConnectionImpl conn, int priority,
047    long pauseNs, long pauseNsForServerOverloaded, int maxAttempts, long operationTimeoutNs,
048    long rpcTimeoutNs, int startLogErrorsCnt, ServerName serverName, Callable<T> callable) {
049    super(retryTimer, conn, priority, pauseNs, pauseNsForServerOverloaded, maxAttempts,
050      operationTimeoutNs, rpcTimeoutNs, startLogErrorsCnt, Collections.emptyMap());
051    this.serverName = serverName;
052    this.callable = callable;
053  }
054
055  @Override
056  protected Throwable preProcessError(Throwable error) {
057    // This retrying caller is mainly used for admin operations, thus we do not implement
058    // complicated relocating logic. If here we get a NotServingRegionException, there is no way to
059    // recover since we just pass in the server name, which means we can not send request to another
060    // region server. So here we just wrap it with a DoNotRetryIOException to fail the request
061    // immediately
062    if (error instanceof NotServingRegionException) {
063      return new DoNotRetryIOException("region is not on " + serverName + ", give up retrying",
064        error);
065    }
066    return error;
067  }
068
069  @Override
070  protected void doCall() {
071    AdminService.Interface adminStub;
072    try {
073      adminStub = this.conn.getAdminStub(serverName);
074    } catch (IOException e) {
075      onError(e, () -> "Get async admin stub to " + serverName + " failed", err -> {
076      });
077      return;
078    }
079    resetCallTimeout();
080    addListener(callable.call(controller, adminStub), (result, error) -> {
081      if (error != null) {
082        onError(error, () -> "Call to admin stub failed", err -> {
083        });
084        return;
085      }
086      future.complete(result);
087    });
088  }
089}