-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24137][K8s] Mount local directories as empty dir volumes. #21238
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
[SPARK-24137] Mount local directories as empty dir volumes.
Dramatically improves performance and won't cause Spark applications to fail because they write too much data to the Docker image's specific file system. The file system's directories that back emptydir volumes are generally larger and more performant.
- Loading branch information
commit 736674bd2baef34e8d55fb7a7cd684e9f467f4ea
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
79 changes: 79 additions & 0 deletions
79
...netes/core/src/main/scala/org/apache/spark/deploy/k8s/features/LocalDirsFeatureStep.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,79 @@ | ||
| /* | ||
| * 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.deploy.k8s.features | ||
|
|
||
| import java.nio.file.Paths | ||
| import java.util.UUID | ||
|
|
||
| import io.fabric8.kubernetes.api.model.{ContainerBuilder, HasMetadata, PodBuilder, VolumeBuilder, VolumeMountBuilder} | ||
|
|
||
| import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesDriverSpecificConf, KubernetesRoleSpecificConf, SparkPod} | ||
|
|
||
| private[spark] class LocalDirsFeatureStep( | ||
| conf: KubernetesConf[_ <: KubernetesRoleSpecificConf], | ||
| defaultLocalDir: String = s"/var/data/spark-${UUID.randomUUID}") | ||
| extends KubernetesFeatureConfigStep { | ||
|
|
||
| // Cannot use Utils.getConfiguredLocalDirs because that will default to the Java system | ||
| // property - we want to instead default to mounting an emptydir volume that doesn't already | ||
| // exist in the image. | ||
| // We could make utils.getConfiguredLocalDirs opinionated about Kubernetes, as it is already | ||
| // a bit opinionated about YARN and Mesos. | ||
| private val resolvedLocalDirs = Option(conf.sparkConf.getenv("SPARK_LOCAL_DIRS")) | ||
| .orElse(conf.getOption("spark.local.dir")) | ||
| .getOrElse(defaultLocalDir) | ||
| .split(",") | ||
|
|
||
| override def configurePod(pod: SparkPod): SparkPod = { | ||
| val localDirVolumes = resolvedLocalDirs | ||
| .zipWithIndex | ||
| .map { | ||
| case (localDir, index) => | ||
| new VolumeBuilder() | ||
| .withName(s"spark-local-dir-${index + 1}-${Paths.get(localDir).getFileName.toString}") | ||
|
||
| .withNewEmptyDir() | ||
| .endEmptyDir() | ||
| .build() | ||
| } | ||
| val localDirVolumeMounts = localDirVolumes | ||
| .zip(resolvedLocalDirs) | ||
| .map { | ||
| case (localDirVolume, localDirPath) => | ||
|
||
| new VolumeMountBuilder() | ||
| .withName(localDirVolume.getName) | ||
| .withMountPath(localDirPath) | ||
| .build() | ||
| } | ||
| val podWithLocalDirVolumes = new PodBuilder(pod.pod) | ||
| .editSpec() | ||
| .addToVolumes(localDirVolumes: _*) | ||
| .endSpec() | ||
| .build() | ||
| val containerWithLocalDirVolumeMounts = new ContainerBuilder(pod.container) | ||
| .addNewEnv() | ||
| .withName("SPARK_LOCAL_DIRS") | ||
| .withValue(resolvedLocalDirs.mkString(",")) | ||
| .endEnv() | ||
| .addToVolumeMounts(localDirVolumeMounts: _*) | ||
| .build() | ||
| SparkPod(podWithLocalDirVolumes, containerWithLocalDirVolumeMounts) | ||
| } | ||
|
|
||
| override def getAdditionalPodSystemProperties(): Map[String, String] = Map.empty | ||
|
|
||
| override def getAdditionalKubernetesResources(): Seq[HasMetadata] = Seq.empty | ||
| } | ||
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
111 changes: 111 additions & 0 deletions
111
.../core/src/test/scala/org/apache/spark/deploy/k8s/features/LocalDirsFeatureStepSuite.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,111 @@ | ||
| /* | ||
| * 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.deploy.k8s.features | ||
|
|
||
| import io.fabric8.kubernetes.api.model.{EnvVarBuilder, VolumeBuilder, VolumeMountBuilder} | ||
| import org.mockito.Mockito | ||
| import org.scalatest.BeforeAndAfter | ||
|
|
||
| import org.apache.spark.{SparkConf, SparkFunSuite} | ||
| import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesDriverSpecificConf, KubernetesExecutorSpecificConf, KubernetesRoleSpecificConf, SparkPod} | ||
|
|
||
| class LocalDirsFeatureStepSuite extends SparkFunSuite with BeforeAndAfter { | ||
| private val defaultLocalDir = "/var/data/default-local-dir" | ||
| private var sparkConf: SparkConf = _ | ||
| private var kubernetesConf: KubernetesConf[_ <: KubernetesRoleSpecificConf] = _ | ||
|
|
||
| before { | ||
| val realSparkConf = new SparkConf(false) | ||
| sparkConf = Mockito.spy(realSparkConf) | ||
| kubernetesConf = KubernetesConf( | ||
| sparkConf, | ||
| KubernetesDriverSpecificConf( | ||
| None, | ||
| "app-name", | ||
| "main", | ||
| Seq.empty), | ||
| "resource", | ||
| "app-id", | ||
| Map.empty, | ||
| Map.empty, | ||
| Map.empty, | ||
| Map.empty) | ||
| } | ||
|
|
||
| test("Resolve to default local dir if neither env nor configuration are set") { | ||
| Mockito.doReturn(null).when(sparkConf).get("spark.local.dir") | ||
| Mockito.doReturn(null).when(sparkConf).getenv("SPARK_LOCAL_DIRS") | ||
| val stepUnderTest = new LocalDirsFeatureStep(kubernetesConf, defaultLocalDir) | ||
| val configuredPod = stepUnderTest.configurePod(SparkPod.initialPod()) | ||
| assert(configuredPod.pod.getSpec.getVolumes.size === 1) | ||
| assert(configuredPod.pod.getSpec.getVolumes.get(0) === | ||
| new VolumeBuilder() | ||
| .withName(s"spark-local-dir-1-default-local-dir") | ||
| .withNewEmptyDir() | ||
| .endEmptyDir() | ||
| .build()) | ||
| assert(configuredPod.container.getVolumeMounts.size === 1) | ||
| assert(configuredPod.container.getVolumeMounts.get(0) === | ||
| new VolumeMountBuilder() | ||
| .withName(s"spark-local-dir-1-default-local-dir") | ||
| .withMountPath(defaultLocalDir) | ||
| .build()) | ||
| assert(configuredPod.container.getEnv.size === 1) | ||
| assert(configuredPod.container.getEnv.get(0) === | ||
| new EnvVarBuilder() | ||
| .withName("SPARK_LOCAL_DIRS") | ||
| .withValue(defaultLocalDir) | ||
| .build()) | ||
| } | ||
|
|
||
| test("Use configured local dirs split on comma if provided.") { | ||
| Mockito.doReturn("/var/data/my-local-dir-1,/var/data/my-local-dir-2") | ||
| .when(sparkConf).getenv("SPARK_LOCAL_DIRS") | ||
| val stepUnderTest = new LocalDirsFeatureStep(kubernetesConf, defaultLocalDir) | ||
| val configuredPod = stepUnderTest.configurePod(SparkPod.initialPod()) | ||
| assert(configuredPod.pod.getSpec.getVolumes.size === 2) | ||
| assert(configuredPod.pod.getSpec.getVolumes.get(0) === | ||
| new VolumeBuilder() | ||
| .withName(s"spark-local-dir-1-my-local-dir-1") | ||
| .withNewEmptyDir() | ||
| .endEmptyDir() | ||
| .build()) | ||
| assert(configuredPod.pod.getSpec.getVolumes.get(1) === | ||
| new VolumeBuilder() | ||
| .withName(s"spark-local-dir-2-my-local-dir-2") | ||
| .withNewEmptyDir() | ||
| .endEmptyDir() | ||
| .build()) | ||
| assert(configuredPod.container.getVolumeMounts.size === 2) | ||
| assert(configuredPod.container.getVolumeMounts.get(0) === | ||
| new VolumeMountBuilder() | ||
| .withName(s"spark-local-dir-1-my-local-dir-1") | ||
| .withMountPath("/var/data/my-local-dir-1") | ||
| .build()) | ||
| assert(configuredPod.container.getVolumeMounts.get(1) === | ||
| new VolumeMountBuilder() | ||
| .withName(s"spark-local-dir-2-my-local-dir-2") | ||
| .withMountPath("/var/data/my-local-dir-2") | ||
| .build()) | ||
| assert(configuredPod.container.getEnv.size === 1) | ||
| assert(configuredPod.container.getEnv.get(0) === | ||
| new EnvVarBuilder() | ||
| .withName("SPARK_LOCAL_DIRS") | ||
| .withValue("/var/data/my-local-dir-1,/var/data/my-local-dir-2") | ||
| .build()) | ||
| } | ||
| } |
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
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.
I think the convention is to put
caseon the same line asmap {.