-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-52426][CORE] Support redirecting stdout/stderr to logging system #51130
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b0c4474
[SPARK-52426][CORE] Support redirecting stdout/stderr to logging system
pan3793 6cf48f7
nit
pan3793 c66f4d3
address comments
pan3793 9461673
nit
pan3793 1593faa
extract common code
pan3793 110e7b1
document affect shell console progress bar
pan3793 ed76488
nit
pan3793 8ccae63
Ensure ConsoleProgressBar always print to console stderr
pan3793 28222ec
revert docs changes
pan3793 5a32078
nit
pan3793 a3326a6
address comments
pan3793 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
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
core/src/main/scala/org/apache/spark/deploy/RedirectConsolePlugin.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,137 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import java.io.{ByteArrayOutputStream, PrintStream} | ||
| import java.util.{Collections, Map => JMap} | ||
|
|
||
| import org.apache.spark.SparkContext | ||
| import org.apache.spark.api.plugin.{DriverPlugin, ExecutorPlugin, PluginContext, SparkPlugin} | ||
| import org.apache.spark.internal.{Logging, SparkLoggerFactory} | ||
| import org.apache.spark.internal.config._ | ||
|
|
||
| /** | ||
| * A built-in plugin to allow redirecting stdout/stderr to logging system (SLF4J). | ||
| */ | ||
| class RedirectConsolePlugin extends SparkPlugin { | ||
| override def driverPlugin(): DriverPlugin = new DriverRedirectConsolePlugin() | ||
|
|
||
| override def executorPlugin(): ExecutorPlugin = new ExecRedirectConsolePlugin() | ||
| } | ||
|
|
||
| object RedirectConsolePlugin { | ||
|
|
||
| def redirectStdoutToLog(): Unit = { | ||
| val stdoutLogger = SparkLoggerFactory.getLogger("stdout") | ||
| System.setOut(new LoggingPrintStream(stdoutLogger.info)) | ||
| } | ||
|
|
||
| def redirectStderrToLog(): Unit = { | ||
| val stderrLogger = SparkLoggerFactory.getLogger("stderr") | ||
| System.setErr(new LoggingPrintStream(stderrLogger.error)) | ||
| } | ||
| } | ||
|
|
||
| class DriverRedirectConsolePlugin extends DriverPlugin with Logging { | ||
|
|
||
| override def init(sc: SparkContext, ctx: PluginContext): JMap[String, String] = { | ||
| val outputs = sc.conf.get(DRIVER_REDIRECT_CONSOLE_OUTPUTS) | ||
| if (outputs.contains("stdout")) { | ||
| logInfo("Redirect driver's stdout to logging system.") | ||
| RedirectConsolePlugin.redirectStdoutToLog() | ||
| } | ||
| if (outputs.contains("stderr")) { | ||
| logInfo("Redirect driver's stderr to logging system.") | ||
| RedirectConsolePlugin.redirectStderrToLog() | ||
| } | ||
| Collections.emptyMap | ||
| } | ||
| } | ||
|
|
||
| class ExecRedirectConsolePlugin extends ExecutorPlugin with Logging { | ||
|
|
||
| override def init(ctx: PluginContext, extraConf: JMap[String, String]): Unit = { | ||
| val outputs = ctx.conf.get(EXEC_REDIRECT_CONSOLE_OUTPUTS) | ||
| if (outputs.contains("stdout")) { | ||
| logInfo("Redirect executor's stdout to logging system.") | ||
| RedirectConsolePlugin.redirectStdoutToLog() | ||
| } | ||
| if (outputs.contains("stderr")) { | ||
| logInfo("Redirect executor's stderr to logging system.") | ||
| RedirectConsolePlugin.redirectStderrToLog() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[spark] class LoggingPrintStream(redirect: String => Unit) | ||
| extends PrintStream(new LineBuffer(4 * 1024 * 1024)) { | ||
|
|
||
| override def write(b: Int): Unit = { | ||
| super.write(b) | ||
| tryLogCurrentLine() | ||
| } | ||
|
|
||
| override def write(buf: Array[Byte], off: Int, len: Int): Unit = { | ||
| super.write(buf, off, len) | ||
| tryLogCurrentLine() | ||
| } | ||
|
|
||
| private def tryLogCurrentLine(): Unit = this.synchronized { | ||
| out.asInstanceOf[LineBuffer].tryGenerateContext.foreach { logContext => | ||
| redirect(logContext) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cache bytes before line ending. When current line is ended or the bytes size reaches the | ||
| * threshold, it can generate the line. | ||
| */ | ||
| private[spark] object LineBuffer { | ||
| private val LF_BYTES = System.lineSeparator.getBytes | ||
| private val LF_LENGTH = LF_BYTES.length | ||
| } | ||
|
|
||
| private[spark] class LineBuffer(lineMaxBytes: Long) extends ByteArrayOutputStream { | ||
|
|
||
| import LineBuffer._ | ||
|
|
||
| def tryGenerateContext: Option[String] = | ||
| if (isLineEnded) { | ||
| try Some(new String(buf, 0, count - LF_LENGTH)) finally reset() | ||
| } else if (count >= lineMaxBytes) { | ||
| try Some(new String(buf, 0, count)) finally reset() | ||
| } else { | ||
| None | ||
| } | ||
|
|
||
| private def isLineEnded: Boolean = { | ||
| if (count < LF_LENGTH) return false | ||
| // fast return in UNIX-like OS when LF is single char '\n' | ||
| if (LF_LENGTH == 1) return LF_BYTES(0) == buf(count - 1) | ||
|
|
||
| var i = 0 | ||
| do { | ||
| if (LF_BYTES(i) != buf(count - LF_LENGTH + i)) { | ||
| return false | ||
| } | ||
| i = i + 1 | ||
| } while (i < LF_LENGTH) | ||
| true | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,8 @@ private[spark] class ConsoleProgressBar(sc: SparkContext) extends Logging { | |
| private val updatePeriodMSec = sc.conf.get(UI_CONSOLE_PROGRESS_UPDATE_INTERVAL) | ||
| // Delay to show up a progress bar, in milliseconds | ||
| private val firstDelayMSec = 500L | ||
| // Get the stderr (which is console for spark-shell) before installing RedirectConsolePlugin | ||
| private val console = System.err | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yaooqinn then it does not affect shell console progress bar, I also pasted the manual test result in PR description. |
||
|
|
||
| // The width of terminal | ||
| private val TerminalWidth = sys.env.getOrElse("COLUMNS", "80").toInt | ||
|
|
@@ -92,7 +94,7 @@ private[spark] class ConsoleProgressBar(sc: SparkContext) extends Logging { | |
| // only refresh if it's changed OR after 1 minute (or the ssh connection will be closed | ||
| // after idle some time) | ||
| if (bar != lastProgressBar || now - lastUpdateTime > 60 * 1000L) { | ||
| System.err.print(s"$CR$bar$CR") | ||
| console.print(s"$CR$bar$CR") | ||
| lastUpdateTime = now | ||
| } | ||
| lastProgressBar = bar | ||
|
|
@@ -103,7 +105,7 @@ private[spark] class ConsoleProgressBar(sc: SparkContext) extends Logging { | |
| */ | ||
| private def clear(): Unit = { | ||
| if (!lastProgressBar.isEmpty) { | ||
| System.err.printf(s"$CR${" " * TerminalWidth}$CR") | ||
| console.printf(s"$CR${" " * TerminalWidth}$CR") | ||
| lastProgressBar = "" | ||
| } | ||
| } | ||
|
|
||
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.
the user-facing error message would be like