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 019package org.apache.oozie.util; 020 021import com.google.common.annotations.VisibleForTesting; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.fs.FileContext; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.mapreduce.MRConfig; 027import org.apache.hadoop.mapreduce.MRJobConfig; 028import org.apache.hadoop.mapreduce.filecache.DistributedCache; 029import org.apache.hadoop.mapreduce.v2.util.MRApps; 030import org.apache.hadoop.util.StringUtils; 031import org.apache.hadoop.yarn.api.ApplicationConstants; 032import org.apache.hadoop.yarn.conf.YarnConfiguration; 033 034import java.io.File; 035import java.io.IOException; 036import java.net.URI; 037import java.net.URISyntaxException; 038import java.util.Arrays; 039import java.util.HashMap; 040import java.util.List; 041import java.util.Locale; 042import java.util.Map; 043 044 045public class ClasspathUtils { 046 private static boolean usingMiniYarnCluster = false; 047 private static final List<String> CLASSPATH_ENTRIES = Arrays.asList( 048 ApplicationConstants.Environment.PWD.$(), 049 ApplicationConstants.Environment.PWD.$() + Path.SEPARATOR + "*" 050 ); 051 052 @VisibleForTesting 053 public static void setUsingMiniYarnCluster(boolean useMiniYarnCluster) { 054 usingMiniYarnCluster = useMiniYarnCluster; 055 } 056 057 // Adapted from MRApps#setClasspath. Adds Yarn, HDFS, Common, and distributed cache jars. 058 public static void setupClasspath(Map<String, String> env, Configuration conf) throws IOException { 059 // Propagate the system classpath when using the mini cluster 060 if (usingMiniYarnCluster) { 061 MRApps.addToEnvironment( 062 env, 063 ApplicationConstants.Environment.CLASSPATH.name(), 064 System.getProperty("java.class.path"), conf); 065 } 066 067 for (String entry : CLASSPATH_ENTRIES) { 068 MRApps.addToEnvironment(env, ApplicationConstants.Environment.CLASSPATH.name(), entry, conf); 069 } 070 071 // a * in the classpath will only find a .jar, so we need to filter out 072 // all .jars and add everything else 073 addToClasspathIfNotJar(org.apache.hadoop.mapreduce.filecache.DistributedCache.getFileClassPaths(conf), 074 org.apache.hadoop.mapreduce.filecache.DistributedCache.getCacheFiles(conf), 075 conf, 076 env, ApplicationConstants.Environment.PWD.$()); 077 addToClasspathIfNotJar(org.apache.hadoop.mapreduce.filecache.DistributedCache.getArchiveClassPaths(conf), 078 org.apache.hadoop.mapreduce.filecache.DistributedCache.getCacheArchives(conf), 079 conf, 080 env, ApplicationConstants.Environment.PWD.$()); 081 082 083 boolean crossPlatform = conf.getBoolean(MRConfig.MAPREDUCE_APP_SUBMISSION_CROSS_PLATFORM, 084 MRConfig.DEFAULT_MAPREDUCE_APP_SUBMISSION_CROSS_PLATFORM); 085 086 for (String c : conf.getStrings(YarnConfiguration.YARN_APPLICATION_CLASSPATH, 087 crossPlatform 088 ? YarnConfiguration.DEFAULT_YARN_CROSS_PLATFORM_APPLICATION_CLASSPATH 089 : YarnConfiguration.DEFAULT_YARN_APPLICATION_CLASSPATH)) { 090 MRApps.addToEnvironment(env, ApplicationConstants.Environment.CLASSPATH.name(), 091 c.trim(), conf); 092 } 093 094 addGPLExtrasJarsAndNativeLibs(env, conf); 095 } 096 097 /** 098 * Fix for CDH-70855 in case of {@code GPLEXTRAS} parcel is present: 099 * <ul> 100 * <li>if {@code $MR2_CLASSPATH} environment variable is present, it's added to {@code CLASSPATH}</li> 101 * <li>if {@code $JAVA_LIBRARY_PATH} environment variable is present, it's added to {@code LD_LIBRARY_PATH}</li> 102 * </ul> 103 * <p> 104 * If these environment variables are not present (in case no {@code GPLEXTRAS} parcel is installed), no effect is taken. 105 * <p> 106 * If {@code GPLEXTRAS} parcel is installed it's important to add any JARs and native libs so that Spark / MR actions can use. 107 * 108 * @param env environment variables as source 109 * @param conf {@link Configuration} as target 110 */ 111 private static void addGPLExtrasJarsAndNativeLibs(final Map<String, String> env, final Configuration conf) { 112 MRApps.addToEnvironment(env, ApplicationConstants.Environment.CLASSPATH.name(), "$MR2_CLASSPATH", conf); 113 MRApps.addToEnvironment(env, ApplicationConstants.Environment.LD_LIBRARY_PATH.name(), "$JAVA_LIBRARY_PATH", conf); 114 } 115 116 // Adapted from MRApps#setClasspath 117 public static void addMapReduceToClasspath(Map<String, String> env, Configuration conf) throws IOException { 118 boolean crossPlatform = conf.getBoolean(MRConfig.MAPREDUCE_APP_SUBMISSION_CROSS_PLATFORM, 119 MRConfig.DEFAULT_MAPREDUCE_APP_SUBMISSION_CROSS_PLATFORM); 120 121 for (String c : conf.getStrings(MRJobConfig.MAPREDUCE_APPLICATION_CLASSPATH, 122 crossPlatform ? 123 StringUtils.getStrings(MRJobConfig.DEFAULT_MAPREDUCE_CROSS_PLATFORM_APPLICATION_CLASSPATH) 124 : StringUtils.getStrings(MRJobConfig.DEFAULT_MAPREDUCE_APPLICATION_CLASSPATH))) { 125 MRApps.addToEnvironment(env, ApplicationConstants.Environment.CLASSPATH.name(), 126 c.trim(), conf); 127 } 128 addMRFrameworkToDistributedCache(conf); 129 } 130 131 /** 132 * Copied over from org.apache.hadoop.mapreduce.JobSubmitter 133 * @param conf the configuration 134 * @throws IOException in case of IO error 135 */ 136 private static void addMRFrameworkToDistributedCache(Configuration conf) 137 throws IOException { 138 String framework = 139 conf.get(MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH, ""); 140 if (!framework.isEmpty()) { 141 URI uri; 142 try { 143 uri = new URI(framework); 144 } catch (URISyntaxException e) { 145 throw new IllegalArgumentException("Unable to parse '" + framework 146 + "' as a URI, check the setting for " 147 + MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH, e); 148 } 149 150 String linkedName = uri.getFragment(); 151 152 // resolve any symlinks in the URI path so using a "current" symlink 153 // to point to a specific version shows the specific version 154 // in the distributed cache configuration 155 FileSystem fs = FileSystem.get(uri, conf); 156 Path frameworkPath = fs.makeQualified( 157 new Path(uri.getScheme(), uri.getAuthority(), uri.getPath())); 158 FileContext fc = FileContext.getFileContext(frameworkPath.toUri(), conf); 159 frameworkPath = fc.resolvePath(frameworkPath); 160 uri = frameworkPath.toUri(); 161 try { 162 uri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), 163 null, linkedName); 164 } catch (URISyntaxException e) { 165 throw new IllegalArgumentException(e); 166 } 167 168 DistributedCache.addCacheArchive(uri, conf); 169 } 170 } 171 172 // Borrowed from MRApps#addToClasspathIfNotJar 173 private static void addToClasspathIfNotJar(Path[] paths, 174 URI[] withLinks, Configuration conf, 175 Map<String, String> environment, 176 String classpathEnvVar) throws IOException { 177 if (paths != null) { 178 HashMap<Path, String> linkLookup = new HashMap<Path, String>(); 179 if (withLinks != null) { 180 for (URI u: withLinks) { 181 Path p = new Path(u); 182 FileSystem remoteFS = p.getFileSystem(conf); 183 p = remoteFS.resolvePath(p.makeQualified(remoteFS.getUri(), 184 remoteFS.getWorkingDirectory())); 185 String name = (null == u.getFragment()) 186 ? p.getName() : u.getFragment(); 187 if (!name.toLowerCase(Locale.ENGLISH).endsWith(".jar")) { 188 linkLookup.put(p, name); 189 } 190 } 191 } 192 193 for (Path p : paths) { 194 FileSystem remoteFS = p.getFileSystem(conf); 195 p = remoteFS.resolvePath(p.makeQualified(remoteFS.getUri(), 196 remoteFS.getWorkingDirectory())); 197 String name = linkLookup.get(p); 198 if (name == null) { 199 name = p.getName(); 200 } 201 if(!name.toLowerCase(Locale.ENGLISH).endsWith(".jar")) { 202 MRApps.addToEnvironment( 203 environment, 204 classpathEnvVar, 205 ApplicationConstants.Environment.PWD.$() + Path.SEPARATOR + name, conf); 206 } 207 } 208 } 209 } 210 211 public static Configuration addToClasspathFromLocalShareLib(Configuration conf, Path libPath) { 212 if (conf == null) { 213 conf = new Configuration(false); 214 } 215 final String pathStr = normalizedLocalFsPath(libPath); 216 217 String appClassPath = conf.get(YarnConfiguration.YARN_APPLICATION_CLASSPATH); 218 219 if (org.apache.commons.lang.StringUtils.isEmpty(appClassPath)) { 220 addPathToYarnClasspathInConfig(conf, pathStr, StringUtils.join(File.pathSeparator, 221 YarnConfiguration.DEFAULT_YARN_CROSS_PLATFORM_APPLICATION_CLASSPATH)); 222 } else { 223 addPathToYarnClasspathInConfig(conf, pathStr, appClassPath); 224 } 225 return conf; 226 } 227 228 private static void addPathToYarnClasspathInConfig(Configuration conf, String pathStr, String appClassPath) { 229 conf.set(YarnConfiguration.YARN_APPLICATION_CLASSPATH, appClassPath + File.pathSeparator + pathStr); 230 } 231 232 private static String normalizedLocalFsPath(Path libPath) { 233 return org.apache.commons.lang.StringUtils.replace(libPath.toString(), FSUtils.FILE_SCHEME_PREFIX, ""); 234 } 235}