Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ private[spark] class KubernetesClusterSchedulerBackend(

private val minRegisteredExecutors = initialExecutors * minRegisteredRatio

private val appId: String =
conf.getOption("spark.app.id").getOrElse(KubernetesConf.getKubernetesAppId())

private val namespace = conf.get(KUBERNETES_NAMESPACE)

private val PATCH_CONTEXT = PatchContext.of(PatchType.STRATEGIC_MERGE)
Expand Down Expand Up @@ -98,13 +101,11 @@ private[spark] class KubernetesClusterSchedulerBackend(
/**
* Get an application ID associated with the job.
* This returns the string value of spark.app.id if set, otherwise
* the locally-generated ID.
* a generated Kubernetes app ID.
*
* @return The application ID
*/
override def applicationId(): String = {
conf.getOption("spark.app.id").getOrElse(KubernetesConf.getKubernetesAppId())
}
override def applicationId(): String = appId

override def start(): Unit = {
super.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,36 @@ class KubernetesClusterSchedulerBackendSuite extends SparkFunSuite with BeforeAn
endpoint.receiveAndReply(context).apply(GenerateExecID("cheeseBurger"))
verify(context).reply("1")
}

test("SPARK-56238: applicationId() is stable across calls when spark.app.id is not set") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case seems to reproduce the reported scenario.

// Use isolated mocks so we don't mutate the shared sc/rpcEnv state.
val confWithoutAppId = new SparkConf(false)
.set("spark.executor.instances", "3")
.set(KUBERNETES_EXECUTOR_DECOMMISSION_LABEL.key, "soLong")
.set(KUBERNETES_EXECUTOR_DECOMMISSION_LABEL_VALUE.key, "cruelWorld")
val localSc = mock(classOf[SparkContext])
val localEnv = mock(classOf[SparkEnv])
val localRpcEnv = mock(classOf[RpcEnv])
when(localSc.conf).thenReturn(confWithoutAppId)
when(localSc.env).thenReturn(localEnv)
when(localSc.resourceProfileManager).thenReturn(resourceProfileManager)
when(localEnv.rpcEnv).thenReturn(localRpcEnv)
when(localRpcEnv.setupEndpoint(any(), any())).thenReturn(driverEndpointRef)
val localTaskScheduler = mock(classOf[TaskSchedulerImpl])
when(localTaskScheduler.sc).thenReturn(localSc)
val backendWithoutAppId = new KubernetesClusterSchedulerBackend(
Copy link
Copy Markdown
Member

@dongjoon-hyun dongjoon-hyun Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you happen to know when this situation happens in the production environment, @xiaoxuandev ? I'm wondering if this is a valid case in the Apache Spark usage.

This only affects client mode.

One more question. Do you know if this is a regression or not? (as Enrico claims)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a regression introduced by #54269. The original code cached the generated ID in private val appId, which was correct.

Affected versions: v4.2.0-preview3+. All 4.0.x and 4.1.x releases are clean.

Regarding production usage: this affects Kubernetes client mode (--deploy-mode client), where the driver runs outside the K8s cluster. In that path, spark.app.id is not pre-set before backend.start(), so each call to applicationId() during start() would generate a different UUID.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thank you for confirming, @xiaoxuandev .

localTaskScheduler,
localSc,
kubernetesClient,
schedulerExecutorService,
eventQueue,
podAllocator,
lifecycleManager,
watchEvents,
pollEvents)
val id1 = backendWithoutAppId.applicationId()
val id2 = backendWithoutAppId.applicationId()
assert(id1 === id2, "applicationId() must return the same value on repeated calls")
assert(id1.startsWith("spark-"), "generated app ID should have the spark- prefix")
}
}