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