@@ -76,6 +76,10 @@ private[spark] class Client(
7676
7777 private val isClusterMode = sparkConf.get(SUBMIT_DEPLOY_MODE ) == " cluster"
7878
79+ // ContainerLaunchContext.setTokensConf is only available in Hadoop 2.9+ and 3.x, so here we use
80+ // reflection to avoid compilation for Hadoop 2.7 profile.
81+ private val SET_TOKENS_CONF_METHOD = " setTokensConf"
82+
7983 private val isClientUnmanagedAMEnabled = sparkConf.get(YARN_UNMANAGED_AM ) && ! isClusterMode
8084 private var appMaster : ApplicationMaster = _
8185 private var stagingDirPath : Path = _
@@ -348,19 +352,29 @@ private[spark] class Client(
348352 // for fetching delegation tokens. See YARN-5910 for more details.
349353 // The feature is only supported in Hadoop 3.x and up, hence the check below.
350354 val regex = sparkConf.get(config.AM_SEND_TOKEN_CONF )
351- if (regex != null && regex .nonEmpty && VersionUtils .isHadoop3) {
355+ if (regex.nonEmpty && VersionUtils .isHadoop3) {
352356 logInfo(s " Processing token conf (spark.yarn.am.sendTokenConf) with regex $regex" )
353357 val dob = new DataOutputBuffer ();
354358 val copy = new Configuration (false );
355359 copy.clear();
356360 hadoopConf.asScala.foreach { entry =>
357- if (entry.getKey.matches(regex)) {
361+ if (entry.getKey.matches(regex.get )) {
358362 copy.set(entry.getKey, entry.getValue)
359363 logInfo(s " Captured key: ${entry.getKey} -> value: ${entry.getValue}" )
360364 }
361365 }
362366 copy.write(dob);
363- amContainer.setTokensConf(ByteBuffer .wrap(dob.getData))
367+
368+ // since this method was added in Hadoop 2.9 and 3.0, we use reflection here to avoid
369+ // compilation error for Hadoop 2.7 profile.
370+ val setTokensConfMethod = try {
371+ amContainer.getClass.getMethod(SET_TOKENS_CONF_METHOD , classOf [ByteBuffer ])
372+ } catch {
373+ case _ : NoSuchMethodException =>
374+ throw new SparkException (s " Cannot find setTokensConf method in ${amContainer.getClass}. " +
375+ s " Please check YARN version and make sure it is 2.9+ or 3.x " )
376+ }
377+ setTokensConfMethod.invoke(ByteBuffer .wrap(dob.getData))
364378 }
365379 }
366380
0 commit comments