This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce blocking submit to kubernetes by default (#53)
* Introduce blocking submit to kubernetes by default Two new configuration settings: - spark.kubernetes.submit.waitAppCompletion - spark.kubernetes.report.interval * Minor touchups * More succinct logging for pod state * Fix import order * Switch to watch-based logging * Spaces in comma-joined volumes, labels, and containers * Use CountDownLatch instead of SettableFuture * Match parallel ConfigBuilder style * Disable logging in fire-and-forget mode Which is enabled with spark.kubernetes.submit.waitAppCompletion=false (default: true) * Additional log line for when application is launched * Minor wording changes * More logging * Drop log to DEBUG
- Loading branch information
Showing
3 changed files
with
225 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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
114 changes: 114 additions & 0 deletions
114
...etes/core/src/main/scala/org/apache/spark/deploy/kubernetes/LoggingPodStatusWatcher.scala
This file contains 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,114 @@ | ||
/* | ||
* 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.kubernetes | ||
|
||
import java.util.concurrent.{CountDownLatch, Executors, TimeUnit} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
import io.fabric8.kubernetes.api.model.Pod | ||
import io.fabric8.kubernetes.client.{KubernetesClientException, Watcher} | ||
import io.fabric8.kubernetes.client.Watcher.Action | ||
|
||
import org.apache.spark.internal.Logging | ||
|
||
/** | ||
* A monitor for the running Kubernetes pod of a Spark application. Status logging occurs on | ||
* every state change and also at an interval for liveness. | ||
* | ||
* @param podCompletedFuture a CountDownLatch that is set to true when the watched pod finishes | ||
* @param appId | ||
* @param interval ms between each state request. If set to 0 or a negative number, the periodic | ||
* logging will be disabled. | ||
*/ | ||
private[kubernetes] class LoggingPodStatusWatcher(podCompletedFuture: CountDownLatch, | ||
appId: String, | ||
interval: Long) | ||
extends Watcher[Pod] with Logging { | ||
|
||
// start timer for periodic logging | ||
private val scheduler = Executors.newScheduledThreadPool(1) | ||
private val logRunnable: Runnable = new Runnable { | ||
override def run() = logShortStatus() | ||
} | ||
if (interval > 0) { | ||
scheduler.scheduleWithFixedDelay(logRunnable, 0, interval, TimeUnit.MILLISECONDS) | ||
} | ||
|
||
private var pod: Option[Pod] = Option.empty | ||
private var prevPhase: String = null | ||
private def phase: String = pod.map(_.getStatus().getPhase()).getOrElse("unknown") | ||
|
||
override def eventReceived(action: Action, pod: Pod): Unit = { | ||
this.pod = Option(pod) | ||
|
||
logShortStatus() | ||
if (prevPhase != phase) { | ||
logLongStatus() | ||
} | ||
prevPhase = phase | ||
|
||
if (phase == "Succeeded" || phase == "Failed") { | ||
podCompletedFuture.countDown() | ||
} | ||
} | ||
|
||
override def onClose(e: KubernetesClientException): Unit = { | ||
scheduler.shutdown() | ||
logDebug(s"Stopped watching application $appId with last-observed phase $phase") | ||
} | ||
|
||
private def logShortStatus() = { | ||
logInfo(s"Application status for $appId (phase: $phase)") | ||
} | ||
|
||
private def logLongStatus() = { | ||
logInfo("Phase changed, new state: " + pod.map(formatPodState(_)).getOrElse("unknown")) | ||
} | ||
|
||
private def formatPodState(pod: Pod): String = { | ||
|
||
val details = Seq[(String, String)]( | ||
// pod metadata | ||
("pod name", pod.getMetadata.getName()), | ||
("namespace", pod.getMetadata.getNamespace()), | ||
("labels", pod.getMetadata.getLabels().asScala.mkString(", ")), | ||
("pod uid", pod.getMetadata.getUid), | ||
("creation time", pod.getMetadata.getCreationTimestamp()), | ||
|
||
// spec details | ||
("service account name", pod.getSpec.getServiceAccountName()), | ||
("volumes", pod.getSpec.getVolumes().asScala.map(_.getName).mkString(", ")), | ||
("node name", pod.getSpec.getNodeName()), | ||
|
||
// status | ||
("start time", pod.getStatus.getStartTime), | ||
("container images", | ||
pod.getStatus.getContainerStatuses() | ||
.asScala | ||
.map(_.getImage) | ||
.mkString(", ")), | ||
("phase", pod.getStatus.getPhase()) | ||
) | ||
|
||
// Use more loggable format if value is null or empty | ||
details.map { case (k, v) => | ||
val newValue = Option(v).filter(_.nonEmpty).getOrElse("N/A") | ||
s"\n\t $k: $newValue" | ||
}.mkString("") | ||
} | ||
} |
This file contains 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