Skip to content

Commit 8454f20

Browse files
committed
Adds automatic cleanup of Spark work directory
Spark uses the 'work' directory to save files (job-jar, ...) required to run. Per default theses files are never deleted, which can cause quite heavy memory consumption. This commit enables a periodic cleanup of the folder during the execution of an suite/exp (set in the reference.spark.conf) plus an manual cleanup on startup.
1 parent 0ac37a9 commit 8454f20

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

peel-extensions/src/main/resources/reference.spark.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ system {
88
home = ${app.path.systems}"/spark"
99
config = ${system.spark.path.home}"/conf"
1010
log = ${system.spark.path.home}"/logs"
11+
work = ${system.spark.path.home}"/work"
1112
}
1213
startup {
1314
max.attempts = ${system.default.startup.max.attempts}
@@ -28,6 +29,8 @@ system {
2829
SPARK_WORKER_CORES = ${system.default.config.parallelism.per-node}
2930
SPARK_EXECUTOR_CORES = ${system.default.config.parallelism.per-node}
3031
SPARK_EXECUTOR_MEMORY = "512m"
32+
# Enables periodic cleanup of worker / application dirs every 5 min. for data older than 1 hour.
33+
SPARK_WORKER_OPTS = """"-Dspark.worker.cleanup.enabled=true -Dspark.worker.cleanup.interval=300 -Dspark.worker.cleanup.appDataTtl=3600""""
3134
}
3235
# spark-defaults.conf entries
3336
defaults {

peel-extensions/src/main/resources/templates/spark/conf/spark-env.sh.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{{#SPARK_WORKER_WEBUI_PORT}}SPARK_WORKER_WEBUI_PORT={{SPARK_WORKER_WEBUI_PORT}}{{/SPARK_WORKER_WEBUI_PORT}}{{^SPARK_WORKER_WEBUI_PORT}}# - SPARK_WORKER_WEBUI_PORT, to use non-default ports for the worker{{/SPARK_WORKER_WEBUI_PORT}}
4343
{{#SPARK_WORKER_INSTANCES}}SPARK_WORKER_INSTANCES={{SPARK_WORKER_INSTANCES}}{{/SPARK_WORKER_INSTANCES}}{{^SPARK_WORKER_INSTANCES}}# - SPARK_WORKER_INSTANCES, to set the number of worker processes per node{{/SPARK_WORKER_INSTANCES}}
4444
{{#SPARK_WORKER_DIR}}SPARK_WORKER_DIR={{SPARK_WORKER_DIR}}{{/SPARK_WORKER_DIR}}{{^SPARK_WORKER_DIR}}# - SPARK_WORKER_DIR, to set the working directory of worker processes{{/SPARK_WORKER_DIR}}
45-
{{#SPARK_WORKER_OPTS}}SPARK_WORKER_OPTS={{SPARK_WORKER_OPTS}}{{/SPARK_WORKER_OPTS}}{{^SPARK_WORKER_OPTS}}# - SPARK_WORKER_OPTS, to set config properties only for the worker (e.g. "-Dx=y"){{/SPARK_WORKER_OPTS}}
45+
{{#SPARK_WORKER_OPTS}}SPARK_WORKER_OPTS={{{SPARK_WORKER_OPTS}}}{{/SPARK_WORKER_OPTS}}{{^SPARK_WORKER_OPTS}}# - SPARK_WORKER_OPTS, to set config properties only for the worker (e.g. "-Dx=y"){{/SPARK_WORKER_OPTS}}
4646
{{#SPARK_HISTORY_OPTS}}SPARK_HISTORY_OPTS={{SPARK_HISTORY_OPTS}}{{/SPARK_HISTORY_OPTS}}{{^SPARK_HISTORY_OPTS}}# - SPARK_HISTORY_OPTS, to set config properties only for the history server (e.g. "-Dx=y"){{/SPARK_HISTORY_OPTS}}
4747
{{#SPARK_DAEMON_JAVA_OPTS}}SPARK_DAEMON_JAVA_OPTS={{SPARK_DAEMON_JAVA_OPTS}}{{/SPARK_DAEMON_JAVA_OPTS}}{{^SPARK_DAEMON_JAVA_OPTS}}# - SPARK_DAEMON_JAVA_OPTS, to set config properties for all daemons (e.g. "-Dx=y"){{/SPARK_DAEMON_JAVA_OPTS}}
4848
{{#SPARK_PUBLIC_DNS}}SPARK_PUBLIC_DNS={{SPARK_PUBLIC_DNS}}{{/SPARK_PUBLIC_DNS}}{{^SPARK_PUBLIC_DNS}}# - SPARK_PUBLIC_DNS, to set the public dns name of the master or workers{{/SPARK_PUBLIC_DNS}}

peel-extensions/src/main/scala/org/peelframework/spark/beans/system/Spark.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,27 @@ class Spark(
140140
s""" ssh $user@$host "$cmd" """
141141
}
142142

143+
val rmWorkDir = (host: String, workDir: String) => {
144+
val cmd = s""" rm -Rf $workDir/* """
145+
s""" ssh $user@$host "$cmd" """
146+
}
147+
143148
val hosts = config.getStringList(s"system.$configKey.config.slaves").asScala
144149
val paths = config.getString(s"system.$configKey.config.defaults.spark.local.dir").split(',')
150+
val workDir = config.getString(s"system.$configKey.path.work")
145151

146-
val futureInitOps = Future.traverse(hosts)(host => Future {
147-
logger.info(s"Initializing Spark tmp directories '${paths.mkString(",")}' at $host")
148-
shell ! (init(host, paths), s"Unable to initialize Spark tmp directories '${paths.mkString(",")}' at $host.")
149-
})
152+
val futureInitOps = Future.traverse(hosts){ host =>
153+
for {
154+
_ <- Future {
155+
logger.info(s"Initializing Spark tmp directories '${paths.mkString(",")}' at $host")
156+
shell ! (init(host, paths), s"Unable to initialize Spark tmp directories '${paths.mkString(",")}' at $host.")
157+
}
158+
f <- Future {
159+
logger.debug(s"Removing Spark work directory content '$workDir' at $host")
160+
shell ! (rmWorkDir(host, workDir), s"Unable to remove Spark work directory content '$workDir' at $host.", fatal = false)
161+
}
162+
} yield f
163+
}
150164

151165
// await for all futureInitOps to finish
152166
Await.result(futureInitOps, Math.max(30, 5 * hosts.size).seconds)

0 commit comments

Comments
 (0)