@@ -21,6 +21,7 @@ import java.io.{File, FileOutputStream, IOException, OutputStreamWriter}
2121import java .net .{InetAddress , UnknownHostException , URI }
2222import java .nio .ByteBuffer
2323import java .nio .charset .StandardCharsets
24+ import java .security .PrivilegedExceptionAction
2425import java .util .{Locale , Properties , UUID }
2526import java .util .zip .{ZipEntry , ZipOutputStream }
2627
@@ -192,16 +193,29 @@ private[spark] class Client(
192193 * Cleanup application staging directory.
193194 */
194195 private def cleanupStagingDir (appId : ApplicationId ): Unit = {
195- val stagingDirPath = new Path (appStagingBaseDir, getAppStagingDir(appId))
196- try {
197- val preserveFiles = sparkConf.get(PRESERVE_STAGING_FILES )
198- val fs = stagingDirPath.getFileSystem(hadoopConf)
199- if (! preserveFiles && fs.delete(stagingDirPath, true )) {
200- logInfo(s " Deleted staging directory $stagingDirPath" )
196+ def cleanupStagingDirInternal (): Unit = {
197+ val stagingDirPath = new Path (appStagingBaseDir, getAppStagingDir(appId))
198+ try {
199+ val preserveFiles = sparkConf.get(PRESERVE_STAGING_FILES )
200+ val fs = stagingDirPath.getFileSystem(hadoopConf)
201+ if (! preserveFiles && fs.delete(stagingDirPath, true )) {
202+ logInfo(s " Deleted staging directory $stagingDirPath" )
203+ }
204+ } catch {
205+ case ioe : IOException =>
206+ logWarning(" Failed to cleanup staging dir " + stagingDirPath, ioe)
201207 }
202- } catch {
203- case ioe : IOException =>
204- logWarning(" Failed to cleanup staging dir " + stagingDirPath, ioe)
208+ }
209+
210+ if (isClusterMode && principal != null && keytab != null ) {
211+ val newUgi = UserGroupInformation .loginUserFromKeytabAndReturnUGI(principal, keytab)
212+ newUgi.doAs(new PrivilegedExceptionAction [Unit ] {
213+ override def run (): Unit = {
214+ cleanupStagingDirInternal()
215+ }
216+ })
217+ } else {
218+ cleanupStagingDirInternal()
205219 }
206220 }
207221
0 commit comments