Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4688#discussion_r25979352
  
    --- Diff: 
yarn/src/main/scala/org/apache/spark/deploy/yarn/YarnSparkHadoopUtil.scala ---
    @@ -82,6 +100,179 @@ class YarnSparkHadoopUtil extends SparkHadoopUtil {
         if (credentials != null) credentials.getSecretKey(new Text(key)) else 
null
       }
     
    +  private[spark] override def scheduleLoginFromKeytab(): Unit = {
    +    sparkConf.getOption("spark.yarn.principal").foreach { principal =>
    +      val keytab = sparkConf.get("spark.yarn.keytab")
    +      val delegationTokenRenewerRunnable =
    +        new Runnable {
    +          override def run(): Unit = {
    +            renewCredentials(principal, keytab)
    +            delegationTokenRenewer.schedule(
    +              this, (0.75 * (getLatestValidity - 
System.currentTimeMillis())).toLong,
    +              TimeUnit.MILLISECONDS)
    +          }
    +        }
    +      val timeToRenewal = (0.75 * (getLatestValidity - 
System.currentTimeMillis())).toLong
    +      delegationTokenRenewer.schedule(
    +        delegationTokenRenewerRunnable, timeToRenewal, 
TimeUnit.MILLISECONDS)
    +    }
    +  }
    +
    +  private def renewCredentials(principal: String, keytab: String): Unit = {
    +    if (!loggedInViaKeytab) {
    +      // Keytab is copied by YARN to the working directory of the AM, so 
full path is
    +      // not needed.
    +      loggedInUGI = UserGroupInformation.loginUserFromKeytabAndReturnUGI(
    +        principal, keytab)
    +      loggedInViaKeytab = true
    +    }
    +    val nns = getNameNodesToAccess(sparkConf)
    +    val newCredentials = loggedInUGI.getCredentials
    +    obtainTokensForNamenodes(nns, conf, newCredentials)
    +    val remoteFs = FileSystem.get(conf)
    +    val nextSuffix = lastCredentialsFileSuffix + 1
    +    val tokenPathStr =
    +      sparkConf.get("spark.yarn.credentials.file") + "-" + nextSuffix
    +    val tokenPath = new Path(tokenPathStr)
    +    val tempTokenPath = new Path(tokenPathStr + ".tmp")
    +    val stream = Option(remoteFs.create(tempTokenPath, true))
    +    try {
    +      stream.foreach { s =>
    +        newCredentials.writeTokenStorageToStream(s)
    +        s.hflush()
    +        s.close()
    +        remoteFs.rename(tempTokenPath, tokenPath)
    +      }
    +    } catch {
    +      case e: Exception =>
    +    } finally {
    +      stream.foreach(_.close())
    +    }
    +
    +    lastCredentialsFileSuffix = nextSuffix
    +  }
    +
    +  override def updateCredentialsIfRequired(): Unit = {
    +    try {
    +      sparkConf.getOption("spark.yarn.credentials.file").foreach { 
credentialsFile =>
    +        val credentialsFilePath = new Path(credentialsFile)
    +        val remoteFs = FileSystem.get(conf)
    +        val stagingDirPath = new Path(remoteFs.getHomeDirectory, 
credentialsFilePath.getParent)
    +        val fileStatuses =
    +          remoteFs.listStatus(stagingDirPath,
    +            new PathFilter {
    +              override def accept(path: Path): Boolean = {
    +                val name = path.getName
    +                name.startsWith(credentialsFilePath.getName) && 
!name.endsWith(".tmp")
    +              }
    +            })
    +        util.Arrays.sort(fileStatuses, new Comparator[FileStatus] {
    +          override def compare(o1: FileStatus, o2: FileStatus): Int = {
    +            // can't return this directly, as it might cause int to 
overflow
    +            val diff = o1.getModificationTime - o2.getModificationTime
    +            if (diff < 0) {
    +              -1
    +            } else {
    +              1
    +            }
    +          }
    +        })
    +        val credentialsStatus = fileStatuses(fileStatuses.length - 1)
    +        val credentials = credentialsStatus.getPath
    +        val suffix = 
credentials.getName.substring(credentials.getName.lastIndexOf("-") + 1).toInt
    +        if (suffix > lastCredentialsFileSuffix) {
    +          val newCredentials = getCredentialsFromHDFSFile(remoteFs, 
credentials)
    +          
UserGroupInformation.getCurrentUser.addCredentials(newCredentials)
    +          val totalValidity = getLatestValidity - 
credentialsStatus.getModificationTime
    +          val timeToRunRenewal =
    +            credentialsStatus.getModificationTime + (0.8 * 
totalValidity).toLong
    +          val timeFromNowToRenewal = timeToRunRenewal - 
System.currentTimeMillis()
    +          
delegationTokenRenewer.schedule(delegationTokenExecuterUpdaterRunnable,
    +            timeFromNowToRenewal, TimeUnit.MILLISECONDS)
    +        } else {
    +          // Check every hour to see if new credentials arrived.
    +          
delegationTokenRenewer.schedule(delegationTokenExecuterUpdaterRunnable, 1, 
TimeUnit.HOURS)
    +        }
    +      }
    +    } catch {
    +      // Since the file may get deleted while we are reading it, catch the 
Exception and come
    +      // back in an hour to try again
    +      case e: Exception =>
    +        logWarning(
    +          "Error encountered while trying to update credentials, will try 
again in 1 hour", e)
    +        
delegationTokenRenewer.schedule(delegationTokenExecuterUpdaterRunnable, 1, 
TimeUnit.HOURS)
    +    }
    +  }
    +
    +  private[spark] def getCredentialsFromHDFSFile(
    +      remoteFs: FileSystem,
    +      tokenPath: Path): Credentials = {
    +    val stream = remoteFs.open(tokenPath)
    --- End diff --
    
    `try { ... } finally { stream.close() }`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to