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