|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.deploy |
| 19 | + |
| 20 | +import java.io.{ByteArrayOutputStream, PrintStream} |
| 21 | +import java.util.{Collections, Map => JMap} |
| 22 | + |
| 23 | +import org.apache.spark.SparkContext |
| 24 | +import org.apache.spark.api.plugin.{DriverPlugin, ExecutorPlugin, PluginContext, SparkPlugin} |
| 25 | +import org.apache.spark.internal.{Logging, SparkLoggerFactory} |
| 26 | +import org.apache.spark.internal.config._ |
| 27 | + |
| 28 | +/** |
| 29 | + * A built-in plugin to allow redirecting stdout/stderr to logging system (SLF4J). |
| 30 | + */ |
| 31 | +class RedirectConsolePlugin extends SparkPlugin { |
| 32 | + override def driverPlugin(): DriverPlugin = new DriverRedirectConsolePlugin() |
| 33 | + |
| 34 | + override def executorPlugin(): ExecutorPlugin = new ExecRedirectConsolePlugin() |
| 35 | +} |
| 36 | + |
| 37 | +object RedirectConsolePlugin { |
| 38 | + |
| 39 | + def redirectStdoutToLog(): Unit = { |
| 40 | + val stdoutLogger = SparkLoggerFactory.getLogger("stdout") |
| 41 | + System.setOut(new LoggingPrintStream(stdoutLogger.info)) |
| 42 | + } |
| 43 | + |
| 44 | + def redirectStderrToLog(): Unit = { |
| 45 | + val stderrLogger = SparkLoggerFactory.getLogger("stderr") |
| 46 | + System.setErr(new LoggingPrintStream(stderrLogger.error)) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class DriverRedirectConsolePlugin extends DriverPlugin with Logging { |
| 51 | + |
| 52 | + override def init(sc: SparkContext, ctx: PluginContext): JMap[String, String] = { |
| 53 | + val outputs = sc.conf.get(DRIVER_REDIRECT_CONSOLE_OUTPUTS) |
| 54 | + if (outputs.contains("stdout")) { |
| 55 | + logInfo("Redirect driver's stdout to logging system.") |
| 56 | + RedirectConsolePlugin.redirectStdoutToLog() |
| 57 | + } |
| 58 | + if (outputs.contains("stderr")) { |
| 59 | + logInfo("Redirect driver's stderr to logging system.") |
| 60 | + RedirectConsolePlugin.redirectStderrToLog() |
| 61 | + } |
| 62 | + Collections.emptyMap |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class ExecRedirectConsolePlugin extends ExecutorPlugin with Logging { |
| 67 | + |
| 68 | + override def init(ctx: PluginContext, extraConf: JMap[String, String]): Unit = { |
| 69 | + val outputs = ctx.conf.get(EXEC_REDIRECT_CONSOLE_OUTPUTS) |
| 70 | + if (outputs.contains("stdout")) { |
| 71 | + logInfo("Redirect executor's stdout to logging system.") |
| 72 | + RedirectConsolePlugin.redirectStdoutToLog() |
| 73 | + } |
| 74 | + if (outputs.contains("stderr")) { |
| 75 | + logInfo("Redirect executor's stderr to logging system.") |
| 76 | + RedirectConsolePlugin.redirectStderrToLog() |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +private[spark] class LoggingPrintStream(redirect: String => Unit) |
| 82 | + extends PrintStream(new LineBuffer(4 * 1024 * 1024)) { |
| 83 | + |
| 84 | + override def write(b: Int): Unit = { |
| 85 | + super.write(b) |
| 86 | + tryLogCurrentLine() |
| 87 | + } |
| 88 | + |
| 89 | + override def write(buf: Array[Byte], off: Int, len: Int): Unit = { |
| 90 | + super.write(buf, off, len) |
| 91 | + tryLogCurrentLine() |
| 92 | + } |
| 93 | + |
| 94 | + private def tryLogCurrentLine(): Unit = this.synchronized { |
| 95 | + out.asInstanceOf[LineBuffer].tryGenerateContext.foreach { logContext => |
| 96 | + redirect(logContext) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Cache bytes before line ending. When current line is ended or the bytes size reaches the |
| 103 | + * threshold, it can generate the line. |
| 104 | + */ |
| 105 | +private[spark] object LineBuffer { |
| 106 | + private val LF_BYTES = System.lineSeparator.getBytes |
| 107 | + private val LF_LENGTH = LF_BYTES.length |
| 108 | +} |
| 109 | + |
| 110 | +private[spark] class LineBuffer(lineMaxBytes: Long) extends ByteArrayOutputStream { |
| 111 | + |
| 112 | + import LineBuffer._ |
| 113 | + |
| 114 | + def tryGenerateContext: Option[String] = |
| 115 | + if (isLineEnded) { |
| 116 | + try Some(new String(buf, 0, count - LF_LENGTH)) finally reset() |
| 117 | + } else if (count >= lineMaxBytes) { |
| 118 | + try Some(new String(buf, 0, count)) finally reset() |
| 119 | + } else { |
| 120 | + None |
| 121 | + } |
| 122 | + |
| 123 | + private def isLineEnded: Boolean = { |
| 124 | + if (count < LF_LENGTH) return false |
| 125 | + // fast return in UNIX-like OS when LF is single char '\n' |
| 126 | + if (LF_LENGTH == 1) return LF_BYTES(0) == buf(count - 1) |
| 127 | + |
| 128 | + var i = 0 |
| 129 | + do { |
| 130 | + if (LF_BYTES(i) != buf(count - LF_LENGTH + i)) { |
| 131 | + return false |
| 132 | + } |
| 133 | + i = i + 1 |
| 134 | + } while (i < LF_LENGTH) |
| 135 | + true |
| 136 | + } |
| 137 | +} |
0 commit comments