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     */
018    
019    package org.apache.hadoop.yarn.client;
020    
021    import java.io.IOException;
022    import java.net.ConnectException;
023    import java.net.InetSocketAddress;
024    import java.security.PrivilegedAction;
025    import java.util.HashMap;
026    import java.util.Map;
027    import java.util.concurrent.TimeUnit;
028    
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    import org.apache.hadoop.classification.InterfaceAudience;
032    import org.apache.hadoop.classification.InterfaceAudience.Private;
033    import org.apache.hadoop.classification.InterfaceStability;
034    import org.apache.hadoop.conf.Configuration;
035    import org.apache.hadoop.io.retry.RetryPolicies;
036    import org.apache.hadoop.io.retry.RetryPolicy;
037    import org.apache.hadoop.io.retry.RetryProxy;
038    import org.apache.hadoop.security.UserGroupInformation;
039    import org.apache.hadoop.yarn.conf.YarnConfiguration;
040    import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
041    import org.apache.hadoop.yarn.ipc.YarnRPC;
042    
043    import com.google.common.annotations.VisibleForTesting;
044    
045    @InterfaceAudience.Public
046    @InterfaceStability.Evolving
047    @SuppressWarnings("unchecked")
048    public class RMProxy<T> {
049    
050      private static final Log LOG = LogFactory.getLog(RMProxy.class);
051    
052      public static <T> T createRMProxy(final Configuration conf,
053          final Class<T> protocol, InetSocketAddress rmAddress) throws IOException {
054        RetryPolicy retryPolicy = createRetryPolicy(conf);
055        T proxy = RMProxy.<T>getProxy(conf, protocol, rmAddress);
056        LOG.info("Connecting to ResourceManager at " + rmAddress);
057        return (T) RetryProxy.create(protocol, proxy, retryPolicy);
058      }
059    
060      private static <T> T getProxy(final Configuration conf,
061          final Class<T> protocol, final InetSocketAddress rmAddress)
062          throws IOException {
063        return UserGroupInformation.getCurrentUser().doAs(
064          new PrivilegedAction<T>() {
065    
066            @Override
067            public T run() {
068              return (T) YarnRPC.create(conf).getProxy(protocol, rmAddress, conf);
069            }
070          });
071      }
072    
073      @Private
074      @VisibleForTesting
075      public static RetryPolicy createRetryPolicy(Configuration conf) {
076        long rmConnectWaitMS =
077            conf.getInt(
078                YarnConfiguration.RESOURCEMANAGER_CONNECT_MAX_WAIT_MS,
079                YarnConfiguration.DEFAULT_RESOURCEMANAGER_CONNECT_MAX_WAIT_MS);
080        long rmConnectionRetryIntervalMS =
081            conf.getLong(
082                YarnConfiguration.RESOURCEMANAGER_CONNECT_RETRY_INTERVAL_MS,
083                YarnConfiguration
084                .DEFAULT_RESOURCEMANAGER_CONNECT_RETRY_INTERVAL_MS);
085    
086        if (rmConnectionRetryIntervalMS < 0) {
087          throw new YarnRuntimeException("Invalid Configuration. " +
088              YarnConfiguration.RESOURCEMANAGER_CONNECT_RETRY_INTERVAL_MS +
089              " should not be negative.");
090        }
091    
092        boolean waitForEver = (rmConnectWaitMS == -1);
093    
094        if (waitForEver) {
095          return  RetryPolicies.RETRY_FOREVER;
096        } else {
097          if (rmConnectWaitMS < 0) {
098            throw new YarnRuntimeException("Invalid Configuration. "
099                + YarnConfiguration.RESOURCEMANAGER_CONNECT_MAX_WAIT_MS
100                + " can be -1, but can not be other negative numbers");
101          }
102    
103          // try connect once
104          if (rmConnectWaitMS < rmConnectionRetryIntervalMS) {
105            LOG.warn(YarnConfiguration.RESOURCEMANAGER_CONNECT_MAX_WAIT_MS
106                + " is smaller than "
107                + YarnConfiguration.RESOURCEMANAGER_CONNECT_RETRY_INTERVAL_MS
108                + ". Only try connect once.");
109            rmConnectWaitMS = 0;
110          }
111        }
112    
113        RetryPolicy retryPolicy =
114            RetryPolicies.retryUpToMaximumTimeWithFixedSleep(rmConnectWaitMS,
115                rmConnectionRetryIntervalMS,
116                TimeUnit.MILLISECONDS);
117    
118        Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap =
119            new HashMap<Class<? extends Exception>, RetryPolicy>();
120        exceptionToPolicyMap.put(ConnectException.class, retryPolicy);
121        //TO DO: after HADOOP-9576,  IOException can be changed to EOFException
122        exceptionToPolicyMap.put(IOException.class, retryPolicy);
123    
124        return RetryPolicies.retryByException(RetryPolicies.TRY_ONCE_THEN_FAIL,
125          exceptionToPolicyMap);
126      }
127    }