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, int priority, long pauseNs, long pauseNsForServerOverloaded, 048 int maxRetries, long operationTimeoutNs, long rpcTimeoutNs, int startLogErrorsCnt) { 049 super(retryTimer, conn, priority, pauseNs, pauseNsForServerOverloaded, maxRetries, 050 operationTimeoutNs, rpcTimeoutNs, 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 ( 057 ClientExceptionsUtil.isConnectionException(error) 058 || error instanceof ServerNotRunningYetException 059 ) { 060 conn.clearMasterStubCache(stub); 061 } 062 } 063 064 @Override 065 protected void doCall() { 066 addListener(conn.getMasterStub(), (stub, error) -> { 067 if (error != null) { 068 onError(error, () -> "Get async master stub failed", err -> { 069 }); 070 return; 071 } 072 resetCallTimeout(); 073 addListener(callable.call(controller, stub), (result, error2) -> { 074 if (error2 != null) { 075 onError(error2, () -> "Call to master failed", 076 err -> clearMasterStubCacheOnError(stub, error2)); 077 return; 078 } 079 future.complete(result); 080 }); 081 }); 082 } 083}