org.apache.hadoop.yarn.client.RequestHedgingRMFailoverProxyProvider Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in org.apache.hadoop.shaded.com.liance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org.apache.hadoop.shaded.org.licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.shaded.org.apache.hadoop.yarn.client;
import java.org.apache.hadoop.shaded.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.org.apache.hadoop.shaded.net.InetSocketAddress;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import org.apache.hadoop.shaded.org.slf4j.Logger;
import org.apache.hadoop.shaded.org.slf4j.LoggerFactory;
import org.apache.hadoop.shaded.org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.org.apache.hadoop.org.apache.hadoop.shaded.io.retry.RetryPolicy;
import org.apache.hadoop.shaded.org.apache.hadoop.org.apache.hadoop.shaded.io.retry.RetryProxy;
import org.apache.hadoop.shaded.org.apache.hadoop.util.concurrent.HadoopExecutors;
import org.apache.hadoop.shaded.org.apache.hadoop.yarn.conf.HAUtil;
import org.apache.hadoop.shaded.org.apache.hadoop.yarn.conf.YarnConfiguration;
/**
* A FailoverProxyProvider implementation that technically does not "failover"
* per-se. It constructs a wrapper proxy that sends the request to ALL
* underlying proxies simultaneously. Each proxy inside the wrapper proxy will
* retry the corresponding target. It assumes the in an HA setup, there will be
* only one Active, and the active should respond faster than any configured
* standbys. Once it receives a response from any one of the configured proxies,
* outstanding requests to other proxies are immediately cancelled.
*/
public class RequestHedgingRMFailoverProxyProvider
extends ConfiguredRMFailoverProxyProvider {
private static final Logger LOG =
LoggerFactory.getLogger(RequestHedgingRMFailoverProxyProvider.class);
private volatile String successfulProxy = null;
private ProxyInfo wrappedProxy = null;
private Map nonRetriableProxy = new HashMap<>();
@Override
@SuppressWarnings("unchecked")
public void init(Configuration configuration, RMProxy rmProxy,
Class protocol) {
super.init(configuration, rmProxy, protocol);
Map> retriableProxies = new HashMap<>();
String originalId = HAUtil.getRMHAId(conf);
for (String rmId : rmServiceIds) {
conf.set(YarnConfiguration.RM_HA_ID, rmId);
nonRetriableProxy.put(rmId, super.getProxyInternal());
T proxy = createRetriableProxy();
ProxyInfo pInfo = new ProxyInfo(proxy, rmId);
retriableProxies.put(rmId, pInfo);
}
conf.set(YarnConfiguration.RM_HA_ID, originalId);
T proxyInstance = (T) Proxy.newProxyInstance(
RMRequestHedgingInvocationHandler.class.getClassLoader(),
new Class>[] {protocol},
new RMRequestHedgingInvocationHandler(retriableProxies));
String org.apache.hadoop.shaded.com.inedInfo = Arrays.toString(rmServiceIds);
wrappedProxy = new ProxyInfo(proxyInstance, org.apache.hadoop.shaded.com.inedInfo);
LOG.info("Created wrapped proxy for " + org.apache.hadoop.shaded.com.inedInfo);
}
@SuppressWarnings("unchecked")
protected T createRetriableProxy() {
try {
// Create proxy that can retry exceptions properly.
RetryPolicy retryPolicy = RMProxy.createRetryPolicy(conf, false);
InetSocketAddress rmAddress = rmProxy.getRMAddress(conf, protocol);
T proxy = rmProxy.getProxy(conf, protocol, rmAddress);
return (T) RetryProxy.create(protocol, proxy, retryPolicy);
} catch (IOException org.apache.hadoop.shaded.io.) {
LOG.error("Unable to create proxy to the ResourceManager "
+ HAUtil.getRMHAId(conf), org.apache.hadoop.shaded.io.);
return null;
}
}
class RMRequestHedgingInvocationHandler implements InvocationHandler {
final private Map> allProxies;
public RMRequestHedgingInvocationHandler(
Map> allProxies) {
this.allProxies = new HashMap<>(allProxies);
}
protected Object invokeMethod(Object proxy, Method method, Object[] args)
throws Throwable {
try {
return method.invoke(proxy, args);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
private Throwable extraRootException(Exception ex) {
Throwable rootCause = ex;
if (ex instanceof ExecutionException) {
Throwable cause = ex.getCause();
if (cause instanceof InvocationTargetException) {
rootCause = cause.getCause();
}
}
return rootCause;
}
/**
* Creates a Executor and invokes all proxies concurrently.
*/
@Override
public Object invoke(Object proxy, final Method method, final Object[] args)
throws Throwable {
if (successfulProxy != null) {
return invokeMethod(nonRetriableProxy.get(successfulProxy), method,
args);
}
LOG.info("Looking for the active RM in " + Arrays.toString(rmServiceIds)
+ "...");
ExecutorService executor = null;
CompletionService
© 2015 - 2025 Weber Informatics LLC | Privacy Policy