Skip to content

Commit da6fa38

Browse files
rvessemccheah
authored andcommitted
[SPARK-25262][K8S] Allow SPARK_LOCAL_DIRS to be tmpfs backed on K8S
## What changes were proposed in this pull request? The default behaviour of Spark on K8S currently is to create `emptyDir` volumes to back `SPARK_LOCAL_DIRS`. In some environments e.g. diskless compute nodes this may actually hurt performance because these are backed by the Kubelet's node storage which on a diskless node will typically be some remote network storage. Even if this is enterprise grade storage connected via a high speed interconnect the way Spark uses these directories as scratch space (lots of relatively small short lived files) has been observed to cause serious performance degradation. Therefore we would like to provide the option to use K8S's ability to instead back these `emptyDir` volumes with `tmpfs`. Therefore this PR adds a configuration option that enables `SPARK_LOCAL_DIRS` to be backed by Memory backed `emptyDir` volumes rather than the default. Documentation is added to describe both the default behaviour plus this new option and its implications. One of which is that scratch space then counts towards your pods memory limits and therefore users will need to adjust their memory requests accordingly. *NB* - This is an alternative version of PR #22256 reduced to just the `tmpfs` piece ## How was this patch tested? Ran with this option in our diskless compute environments to verify functionality Author: Rob Vesse <rvesse@dotnetrdf.org> Closes #22323 from rvesse/SPARK-25262-tmpfs.
1 parent 27d3b0a commit da6fa38

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

docs/running-on-kubernetes.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ spark.kubernetes.driver.volumes.persistentVolumeClaim.checkpointpvc.options.clai
215215

216216
The configuration properties for mounting volumes into the executor pods use prefix `spark.kubernetes.executor.` instead of `spark.kubernetes.driver.`. For a complete list of available options for each supported type of volumes, please refer to the [Spark Properties](#spark-properties) section below.
217217

218+
## Local Storage
219+
220+
Spark uses temporary scratch space to spill data to disk during shuffles and other operations. When using Kubernetes as the resource manager the pods will be created with an [emptyDir](https://kubernetes.io/docs/concepts/storage/volumes/#emptydir) volume mounted for each directory listed in `SPARK_LOCAL_DIRS`. If no directories are explicitly specified then a default directory is created and configured appropriately.
221+
222+
`emptyDir` volumes use the ephemeral storage feature of Kubernetes and do not persist beyond the life of the pod.
223+
224+
### Using RAM for local storage
225+
226+
`emptyDir` volumes use the nodes backing storage for ephemeral storage by default, this behaviour may not be appropriate for some compute environments. For example if you have diskless nodes with remote storage mounted over a network, having lots of executors doing IO to this remote storage may actually degrade performance.
227+
228+
In this case it may be desirable to set `spark.kubernetes.local.dirs.tmpfs=true` in your configuration which will cause the `emptyDir` volumes to be configured as `tmpfs` i.e. RAM backed volumes. When configured like this Sparks local storage usage will count towards your pods memory usage therefore you may wish to increase your memory requests by increasing the value of `spark.kubernetes.memoryOverheadFactor` as appropriate.
229+
230+
218231
## Introspection and Debugging
219232

220233
These are the different ways in which you can investigate a running/completed Spark application, monitor progress, and
@@ -784,6 +797,14 @@ specific to Spark on Kubernetes.
784797
<code>spark.kubernetes.executor.volumes.persistentVolumeClaim.checkpointpvc.options.claimName=spark-pvc-claim</code>.
785798
</td>
786799
</tr>
800+
<tr>
801+
<td><code>spark.kubernetes.local.dirs.tmpfs</code></td>
802+
<td><code>false</code>
803+
<td>
804+
Configure the <code>emptyDir</code> volumes used to back <code>SPARK_LOCAL_DIRS</code> within the Spark driver and executor pods to use <code>tmpfs</code> backing i.e. RAM. See <a href="#local-storage">Local Storage</a> earlier on this page
805+
for more discussion of this.
806+
</td>
807+
</tr>
787808
<tr>
788809
<td><code>spark.kubernetes.memoryOverheadFactor</code></td>
789810
<td><code>0.1</code></td>

resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/Config.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ private[spark] object Config extends Logging {
225225
"Ensure that major Python version is either Python2 or Python3")
226226
.createWithDefault("2")
227227

228+
val KUBERNETES_LOCAL_DIRS_TMPFS =
229+
ConfigBuilder("spark.kubernetes.local.dirs.tmpfs")
230+
.doc("If set to true then emptyDir volumes created to back SPARK_LOCAL_DIRS will have " +
231+
"their medium set to Memory so that they will be created as tmpfs (i.e. RAM) backed " +
232+
"volumes. This may improve performance but scratch space usage will count towards " +
233+
"your pods memory limit so you may wish to request more memory.")
234+
.booleanConf
235+
.createWithDefault(false)
236+
228237
val KUBERNETES_AUTH_SUBMISSION_CONF_PREFIX =
229238
"spark.kubernetes.authenticate.submission"
230239

resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/features/LocalDirsFeatureStep.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.util.UUID
2222
import io.fabric8.kubernetes.api.model.{ContainerBuilder, HasMetadata, PodBuilder, VolumeBuilder, VolumeMountBuilder}
2323

2424
import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesDriverSpecificConf, KubernetesRoleSpecificConf, SparkPod}
25+
import org.apache.spark.deploy.k8s.Config._
2526

2627
private[spark] class LocalDirsFeatureStep(
2728
conf: KubernetesConf[_ <: KubernetesRoleSpecificConf],
@@ -37,6 +38,7 @@ private[spark] class LocalDirsFeatureStep(
3738
.orElse(conf.getOption("spark.local.dir"))
3839
.getOrElse(defaultLocalDir)
3940
.split(",")
41+
private val useLocalDirTmpFs = conf.get(KUBERNETES_LOCAL_DIRS_TMPFS)
4042

4143
override def configurePod(pod: SparkPod): SparkPod = {
4244
val localDirVolumes = resolvedLocalDirs
@@ -45,6 +47,7 @@ private[spark] class LocalDirsFeatureStep(
4547
new VolumeBuilder()
4648
.withName(s"spark-local-dir-${index + 1}")
4749
.withNewEmptyDir()
50+
.withMedium(if (useLocalDirTmpFs) "Memory" else null)
4851
.endEmptyDir()
4952
.build()
5053
}

resource-managers/kubernetes/core/src/test/scala/org/apache/spark/deploy/k8s/features/LocalDirsFeatureStepSuite.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package org.apache.spark.deploy.k8s.features
1818

1919
import io.fabric8.kubernetes.api.model.{EnvVarBuilder, VolumeBuilder, VolumeMountBuilder}
2020
import org.mockito.Mockito
21+
import org.scalatest._
2122
import org.scalatest.BeforeAndAfter
2223

2324
import org.apache.spark.{SparkConf, SparkFunSuite}
2425
import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesDriverSpecificConf, KubernetesRoleSpecificConf, SparkPod}
26+
import org.apache.spark.deploy.k8s.Config._
2527

2628
class LocalDirsFeatureStepSuite extends SparkFunSuite with BeforeAndAfter {
2729
private val defaultLocalDir = "/var/data/default-local-dir"
@@ -111,4 +113,32 @@ class LocalDirsFeatureStepSuite extends SparkFunSuite with BeforeAndAfter {
111113
.withValue("/var/data/my-local-dir-1,/var/data/my-local-dir-2")
112114
.build())
113115
}
116+
117+
test("Use tmpfs to back default local dir") {
118+
Mockito.doReturn(null).when(sparkConf).get("spark.local.dir")
119+
Mockito.doReturn(null).when(sparkConf).getenv("SPARK_LOCAL_DIRS")
120+
Mockito.doReturn(true).when(sparkConf).get(KUBERNETES_LOCAL_DIRS_TMPFS)
121+
val stepUnderTest = new LocalDirsFeatureStep(kubernetesConf, defaultLocalDir)
122+
val configuredPod = stepUnderTest.configurePod(SparkPod.initialPod())
123+
assert(configuredPod.pod.getSpec.getVolumes.size === 1)
124+
assert(configuredPod.pod.getSpec.getVolumes.get(0) ===
125+
new VolumeBuilder()
126+
.withName(s"spark-local-dir-1")
127+
.withNewEmptyDir()
128+
.withMedium("Memory")
129+
.endEmptyDir()
130+
.build())
131+
assert(configuredPod.container.getVolumeMounts.size === 1)
132+
assert(configuredPod.container.getVolumeMounts.get(0) ===
133+
new VolumeMountBuilder()
134+
.withName(s"spark-local-dir-1")
135+
.withMountPath(defaultLocalDir)
136+
.build())
137+
assert(configuredPod.container.getEnv.size === 1)
138+
assert(configuredPod.container.getEnv.get(0) ===
139+
new EnvVarBuilder()
140+
.withName("SPARK_LOCAL_DIRS")
141+
.withValue(defaultLocalDir)
142+
.build())
143+
}
114144
}

0 commit comments

Comments
 (0)