-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-54173][K8S] Add support for Deployment API on K8s #52867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ForVic
wants to merge
7
commits into
apache:master
Choose a base branch
from
ForVic:dev/victors/deployment_allocator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
749e75d
Implement a DeploymentPodsAllocator
victors-oai 347f77f
compile with 2.13 scala
victors-oai 218b733
more compile issues
victors-oai efa302d
import order
victors-oai 4d9725f
import order
victors-oai 6bae7d9
formatting
victors-oai b7ca243
4.2.0
victors-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
.../core/src/main/scala/org/apache/spark/scheduler/cluster/k8s/DeploymentPodsAllocator.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.spark.scheduler.cluster.k8s | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import scala.collection.mutable | ||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| import io.fabric8.kubernetes.api.model.{Pod, PodSpec, PodSpecBuilder, PodTemplateSpec} | ||
| import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder | ||
| import io.fabric8.kubernetes.client.KubernetesClient | ||
|
|
||
| import org.apache.spark.{SecurityManager, SparkConf, SparkException} | ||
| import org.apache.spark.deploy.k8s.Config._ | ||
| import org.apache.spark.deploy.k8s.Constants._ | ||
| import org.apache.spark.deploy.k8s.KubernetesConf | ||
| import org.apache.spark.deploy.k8s.KubernetesUtils.addOwnerReference | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.resource.ResourceProfile | ||
| import org.apache.spark.util.{Clock, Utils} | ||
|
|
||
| /** | ||
| * A pods allocator backed by Kubernetes Deployments. | ||
| * | ||
| * The Deployment controller honours the `controller.kubernetes.io/pod-deletion-cost` | ||
| * annotation, so executors selected by Spark for removal can be prioritised when the | ||
| * deployment scales down. This provides predictable downscale behaviour for dynamic | ||
| * allocation that is not possible with StatefulSets which only remove pods in ordinal order. | ||
| */ | ||
| class DeploymentPodsAllocator( | ||
| conf: SparkConf, | ||
| secMgr: SecurityManager, | ||
| executorBuilder: KubernetesExecutorBuilder, | ||
| kubernetesClient: KubernetesClient, | ||
| snapshotsStore: ExecutorPodsSnapshotsStore, | ||
| clock: Clock) extends AbstractPodsAllocator() with Logging { | ||
|
|
||
| private val rpIdToResourceProfile = new mutable.HashMap[Int, ResourceProfile] | ||
|
|
||
| private val driverPodReadinessTimeout = conf.get(KUBERNETES_ALLOCATION_DRIVER_READINESS_TIMEOUT) | ||
|
|
||
| private val namespace = conf.get(KUBERNETES_NAMESPACE) | ||
|
|
||
| private val kubernetesDriverPodName = conf.get(KUBERNETES_DRIVER_POD_NAME) | ||
|
|
||
| val driverPod: Option[Pod] = kubernetesDriverPodName | ||
| .map(name => Option(kubernetesClient.pods() | ||
| .inNamespace(namespace) | ||
| .withName(name) | ||
| .get()) | ||
| .getOrElse(throw new SparkException( | ||
| s"No pod was found named $name in the cluster in the " + | ||
| s"namespace $namespace (this was supposed to be the driver pod.)."))) | ||
|
|
||
| private var appId: String = _ | ||
|
|
||
| private val deploymentsCreated = new mutable.HashSet[Int]() | ||
|
|
||
| private val podDeletionCostAnnotation = "controller.kubernetes.io/pod-deletion-cost" | ||
|
|
||
| override def start( | ||
| applicationId: String, | ||
| schedulerBackend: KubernetesClusterSchedulerBackend): Unit = { | ||
| appId = applicationId | ||
| driverPod.foreach { pod => | ||
| Utils.tryLogNonFatalError { | ||
| kubernetesClient | ||
| .pods() | ||
| .inNamespace(namespace) | ||
| .withName(pod.getMetadata.getName) | ||
| .waitUntilReady(driverPodReadinessTimeout, TimeUnit.SECONDS) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override def setTotalExpectedExecutors( | ||
| resourceProfileToTotalExecs: Map[ResourceProfile, Int]): Unit = { | ||
| if (appId == null) { | ||
| throw new SparkException("setTotalExpectedExecutors called before start of allocator.") | ||
| } | ||
| resourceProfileToTotalExecs.foreach { case (rp, numExecs) => | ||
| rpIdToResourceProfile.getOrElseUpdate(rp.id, rp) | ||
| setTargetExecutorsDeployment(numExecs, appId, rp.id) | ||
| } | ||
| } | ||
|
|
||
| override def isDeleted(executorId: String): Boolean = false | ||
|
|
||
| private def setName(applicationId: String, resourceProfileId: Int): String = { | ||
| s"spark-d-$applicationId-$resourceProfileId" | ||
| } | ||
|
|
||
| private def setTargetExecutorsDeployment( | ||
| expected: Int, | ||
| applicationId: String, | ||
| resourceProfileId: Int): Unit = { | ||
| if (deploymentsCreated.contains(resourceProfileId)) { | ||
| kubernetesClient | ||
| .apps() | ||
| .deployments() | ||
| .inNamespace(namespace) | ||
| .withName(setName(applicationId, resourceProfileId)) | ||
| .scale(expected) | ||
| } else { | ||
| val executorConf = KubernetesConf.createExecutorConf( | ||
| conf, | ||
| "EXECID", | ||
| applicationId, | ||
| driverPod, | ||
| resourceProfileId) | ||
| val resolvedExecutorSpec = executorBuilder.buildFromFeatures( | ||
| executorConf, | ||
| secMgr, | ||
| kubernetesClient, | ||
| rpIdToResourceProfile(resourceProfileId)) | ||
| val executorPod = resolvedExecutorSpec.pod | ||
|
|
||
| val podSpecBuilder = executorPod.pod.getSpec match { | ||
| case null => new PodSpecBuilder() | ||
| case s => new PodSpecBuilder(s) | ||
| } | ||
| val podWithAttachedContainer: PodSpec = podSpecBuilder | ||
| .addToContainers(executorPod.container) | ||
| .build() | ||
|
|
||
| val meta = executorPod.pod.getMetadata | ||
| val resources = resolvedExecutorSpec.executorKubernetesResources | ||
| val failureMessage = | ||
| "PersistentVolumeClaims are not supported with the deployment allocator. " + | ||
| "Please remove PVC requirements or choose a different pods allocator." | ||
| val dynamicVolumeClaims = resources.filter(_.getKind == "PersistentVolumeClaim") | ||
| if (dynamicVolumeClaims.nonEmpty) { | ||
| throw new SparkException(failureMessage) | ||
| } | ||
| val staticVolumeClaims = Option(podWithAttachedContainer.getVolumes) | ||
| .map(_.asScala.filter(_.getPersistentVolumeClaim != null)) | ||
| .getOrElse(Seq.empty) | ||
| if (staticVolumeClaims.nonEmpty) { | ||
| throw new SparkException(failureMessage) | ||
| } | ||
|
|
||
| val currentAnnotations = Option(meta.getAnnotations) | ||
| .map(_.asScala).getOrElse(Map.empty[String, String]) | ||
| if (!currentAnnotations.contains(podDeletionCostAnnotation)) { | ||
| val newAnnotations = currentAnnotations.concat(Seq(podDeletionCostAnnotation -> "0")) | ||
| meta.setAnnotations(newAnnotations.asJava) | ||
| } | ||
|
|
||
| val podTemplateSpec = new PodTemplateSpec(meta, podWithAttachedContainer) | ||
|
|
||
| val deployment = new DeploymentBuilder() | ||
| .withNewMetadata() | ||
| .withName(setName(applicationId, resourceProfileId)) | ||
| .withNamespace(namespace) | ||
| .endMetadata() | ||
| .withNewSpec() | ||
| .withReplicas(expected) | ||
| .withNewSelector() | ||
| .addToMatchLabels(SPARK_APP_ID_LABEL, applicationId) | ||
| .addToMatchLabels(SPARK_ROLE_LABEL, SPARK_POD_EXECUTOR_ROLE) | ||
| .addToMatchLabels(SPARK_RESOURCE_PROFILE_ID_LABEL, resourceProfileId.toString) | ||
| .endSelector() | ||
| .withTemplate(podTemplateSpec) | ||
| .endSpec() | ||
| .build() | ||
|
|
||
| addOwnerReference(driverPod.get, Seq(deployment)) | ||
| kubernetesClient.apps().deployments().inNamespace(namespace).resource(deployment).create() | ||
| deploymentsCreated += resourceProfileId | ||
| } | ||
| } | ||
|
|
||
| override def stop(applicationId: String): Unit = { | ||
| deploymentsCreated.foreach { rpid => | ||
| Utils.tryLogNonFatalError { | ||
| kubernetesClient | ||
| .apps() | ||
| .deployments() | ||
| .inNamespace(namespace) | ||
| .withName(setName(applicationId, rpid)) | ||
| .delete() | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we can emphasize that this must be set when dynamic allocation is enabled and the deployment-based allocator is enabled.
Also, do we need this configuration at all? can we just set this to some large number such as 100 when deployment-based allocator and dynamic allocation is enabled?