Skip to content

Commit

Permalink
[FLINK-2268] Only print Hadoop env info if Hadoop is in the classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
aljoscha committed Sep 27, 2017
1 parent 5bdcf9b commit f5d3c72
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
import java.util.Properties;

import org.apache.flink.util.OperatingSystem;
import org.apache.hadoop.util.VersionInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hadoop.security.UserGroupInformation;

/**
* Utility class that gives access to the execution environment of the JVM, like
* the executing user, startup options, or the JVM version.
Expand Down Expand Up @@ -87,9 +84,20 @@ public static RevisionInformation getRevisionInformation() {
*
* @return The name of the user that is running the JVM.
*/
public static String getUserRunning() {
public static String getHadoopUser() {
try {
return UserGroupInformation.getCurrentUser().getShortUserName();
Class<?> ugiClass = Class.forName(
"org.apache.hadoop.security.UserGroupInformation",
false,
EnvironmentInformation.class.getClassLoader());

Method currentUserMethod = ugiClass.getMethod("getCurrentUser");
Method shortUserNameMethod = ugiClass.getMethod("getShortUserName");
Object ugi = currentUserMethod.invoke(null);
return (String) shortUserNameMethod.invoke(ugi);
}
catch (ClassNotFoundException e) {
return "<no hadoop dependency found>";
}
catch (LinkageError e) {
// hadoop classes are not in the classpath
Expand All @@ -101,12 +109,7 @@ public static String getUserRunning() {
LOG.warn("Error while accessing user/group information via Hadoop utils.", t);
}

String user = System.getProperty("user.name");
if (user == null) {
user = UNKNOWN;
LOG.debug("Cannot determine user/group information for the current user.");
}
return user;
return UNKNOWN;
}

/**
Expand Down Expand Up @@ -268,8 +271,6 @@ public static void logEnvironmentInfo(Logger log, String componentName, String[]
RevisionInformation rev = getRevisionInformation();
String version = getVersion();

String user = getUserRunning();

String jvmVersion = getJvmVersion();
String[] options = getJvmStartupOptionsArray();

Expand All @@ -280,11 +281,18 @@ public static void logEnvironmentInfo(Logger log, String componentName, String[]
log.info("--------------------------------------------------------------------------------");
log.info(" Starting " + componentName + " (Version: " + version + ", "
+ "Rev:" + rev.commitId + ", " + "Date:" + rev.commitDate + ")");
log.info(" Current user: " + user);
log.info(" OS current user: " + System.getProperty("user.name"));
log.info(" Current Hadoop/Kerberos user: " + getHadoopUser());
log.info(" JVM: " + jvmVersion);
log.info(" Maximum heap size: " + maxHeapMegabytes + " MiBytes");
log.info(" JAVA_HOME: " + (javaHome == null ? "(not set)" : javaHome));
log.info(" Hadoop version: " + VersionInfo.getVersion());

String hadoopVersionString = getHadoopVersionString();
if (hadoopVersionString != null) {
log.info(" Hadoop version: " + hadoopVersionString);
} else {
log.info(" No Hadoop Dependency available");
}

if (options.length == 0) {
log.info(" JVM Options: (none)");
Expand Down Expand Up @@ -312,6 +320,22 @@ public static void logEnvironmentInfo(Logger log, String componentName, String[]
}
}

public static String getHadoopVersionString() {
try {
Class<?> versionInfoClass = Class.forName(
"org.apache.hadoop.util.VersionInfo",
false,
EnvironmentInformation.class.getClassLoader());
Method method = versionInfoClass.getMethod("getVersion");
return (String) method.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException e) {
return null;
} catch (Throwable e) {
LOG.error("Cannot invoke VersionInfo.getVersion reflectively.", e);
return null;
}
}

// --------------------------------------------------------------------------------------------

/** Don't instantiate this class */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void testEnvironmentMethods() {
assertNotNull(EnvironmentInformation.getJvmVersion());
assertNotNull(EnvironmentInformation.getRevisionInformation());
assertNotNull(EnvironmentInformation.getVersion());
assertNotNull(EnvironmentInformation.getUserRunning());
EnvironmentInformation.getHadoopVersionString();
assertNotNull(EnvironmentInformation.getHadoopUser());
assertTrue(EnvironmentInformation.getOpenFileHandlesLimit() >= -1);
}
catch (Exception e) {
Expand Down

0 comments on commit f5d3c72

Please sign in to comment.