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.util.Collections;
023import java.util.Optional;
024import java.util.concurrent.CompletableFuture;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.exceptions.ClientExceptionsUtil;
027import org.apache.hadoop.hbase.ipc.HBaseRpcController;
028import org.apache.hadoop.hbase.ipc.ServerNotRunningYetException;
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.MasterProtos.MasterService;
034
035/**
036 * Retry caller for a request call to master.
037 * @since 2.0.0
038 */
039@InterfaceAudience.Private
040public class AsyncMasterRequestRpcRetryingCaller<T> extends AsyncRpcRetryingCaller<T> {
041
042  @FunctionalInterface
043  public interface Callable<T> {
044    CompletableFuture<T> call(HBaseRpcController controller, MasterService.Interface stub);
045  }
046
047  private final Optional<TableName> tableName;
048
049  private final Callable<T> callable;
050
051  public AsyncMasterRequestRpcRetryingCaller(Timer retryTimer, AsyncConnectionImpl conn,
052    Callable<T> callable, TableName tableName, int priority, long pauseNs,
053    long pauseNsForServerOverloaded, int maxRetries, long operationTimeoutNs, long rpcTimeoutNs,
054    int startLogErrorsCnt) {
055    super(retryTimer, conn, priority, pauseNs, pauseNsForServerOverloaded, maxRetries,
056      operationTimeoutNs, rpcTimeoutNs, startLogErrorsCnt, Collections.emptyMap());
057    this.tableName = Optional.ofNullable(tableName);
058    this.callable = callable;
059  }
060
061  @Override
062  protected Optional<TableName> getTableName() {
063    return tableName;
064  }
065
066  private void clearMasterStubCacheOnError(MasterService.Interface stub, Throwable error) {
067    // ServerNotRunningYetException may because it is the backup master.
068    if (
069      ClientExceptionsUtil.isConnectionException(error)
070        || error instanceof ServerNotRunningYetException
071    ) {
072      conn.clearMasterStubCache(stub);
073    }
074  }
075
076  @Override
077  protected void doCall() {
078    addListener(conn.getMasterStub(), (stub, error) -> {
079      if (error != null) {
080        onError(error, () -> "Get async master stub failed", err -> {
081        });
082        return;
083      }
084      resetCallTimeout();
085      addListener(callable.call(controller, stub), (result, error2) -> {
086        if (error2 != null) {
087          onError(error2, () -> "Call to master failed",
088            err -> clearMasterStubCacheOnError(stub, err));
089          return;
090        }
091        future.complete(result);
092      });
093    });
094  }
095}