-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| 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()) | ||
| } | ||
| } |
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.
oops, I deleted a comment here accidentally. @rxin said that we could remove this warning about Spark 1.0.