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