@@ -24,25 +24,36 @@ import org.apache.hadoop.mapred.JobConf
2424import org .apache .hadoop .security .Credentials
2525import org .apache .hadoop .security .UserGroupInformation
2626
27- import org .apache .spark .{SparkContext , SparkException }
27+ import org .apache .spark .{Logging , SparkContext , SparkException }
2828
2929import scala .collection .JavaConversions ._
3030
3131/**
3232 * Contains util methods to interact with Hadoop from Spark.
3333 */
34- class SparkHadoopUtil {
34+ class SparkHadoopUtil extends Logging {
3535 val conf : Configuration = newConfiguration()
3636 UserGroupInformation .setConfiguration(conf)
3737
38- def runAsUser (user : String )(func : () => Unit ) {
38+ /**
39+ * Runs the given function with a Hadoop UserGroupInformation as a thread local variable
40+ * (distributed to child threads), used for authenticating HDFS and YARN calls.
41+ *
42+ * IMPORTANT NOTE: If this function is going to be called repeated in the same process
43+ * you need to look https://issues.apache.org/jira/browse/HDFS-3545 and possibly
44+ * do a FileSystem.closeAllForUGI in order to avoid leaking Filesystems
45+ */
46+ def runAsSparkUser (func : () => Unit ) {
47+ val user = Option (System .getenv(" SPARK_USER" )).getOrElse(SparkContext .SPARK_UNKNOWN_USER )
3948 if (user != SparkContext .SPARK_UNKNOWN_USER ) {
49+ logDebug(" running as user: " + user)
4050 val ugi = UserGroupInformation .createRemoteUser(user)
4151 transferCredentials(UserGroupInformation .getCurrentUser(), ugi)
4252 ugi.doAs(new PrivilegedExceptionAction [Unit ] {
4353 def run : Unit = func()
4454 })
4555 } else {
56+ logDebug(" running as SPARK_UNKNOWN_USER" )
4657 func()
4758 }
4859 }
0 commit comments