-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from alexarchambault/spark-logs-in-almond-output
Allow to send Spark logs to the Almond kernel console output
- Loading branch information
Showing
4 changed files
with
140 additions
and
6 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
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
95 changes: 95 additions & 0 deletions
95
...s/almond-spark/src/main/scala/org/apache/spark/sql/almondinternals/SendLogToConsole.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,95 @@ | ||
package org.apache.spark.sql.almondinternals | ||
|
||
import almond.api.JupyterApi | ||
|
||
import java.io.{BufferedReader, File, FileReader, PrintStream} | ||
import java.nio.file.{Files, StandardOpenOption} | ||
|
||
import scala.collection.mutable.ListBuffer | ||
import scala.concurrent.duration.{DurationInt, FiniteDuration} | ||
|
||
final class SendLogToConsole( | ||
f: File, | ||
out: PrintStream, | ||
threadName: String = "send-log-console", | ||
lineBufferSize: Int = 10, | ||
delay: FiniteDuration = 2.seconds | ||
) { | ||
|
||
@volatile private var keepReading = true | ||
|
||
val thread: Thread = | ||
new Thread(threadName) { | ||
override def run(): Unit = { | ||
|
||
var r: FileReader = null | ||
|
||
try { | ||
// https://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f/558262#558262 | ||
|
||
r = new FileReader(f) | ||
val br = new BufferedReader(r) | ||
|
||
var lines = new ListBuffer[String] | ||
while (keepReading) { | ||
val line = br.readLine() | ||
if (line == null) | ||
// wait until there is more in the file | ||
Thread.sleep(delay.toMillis) | ||
else { | ||
lines += line | ||
|
||
while (keepReading && lines.length <= lineBufferSize && br.ready()) | ||
lines += br.readLine() | ||
|
||
val l = lines.result() | ||
lines.clear() | ||
|
||
for (line <- l) | ||
out.println(line) | ||
} | ||
} | ||
} | ||
catch { | ||
case _: InterruptedException => | ||
// normal exit | ||
case e: Throwable => | ||
System.err.println(s"Thread $threadName crashed") | ||
e.printStackTrace(System.err) | ||
} | ||
finally | ||
if (r != null) | ||
r.close() | ||
} | ||
} | ||
|
||
def start(): Unit = { | ||
assert(keepReading, "Already stopped") | ||
if (!thread.isAlive) | ||
synchronized { | ||
if (!thread.isAlive) | ||
thread.start() | ||
} | ||
} | ||
|
||
def stop(): Unit = { | ||
keepReading = false | ||
thread.interrupt() | ||
} | ||
|
||
} | ||
|
||
object SendLogToConsole { | ||
|
||
def start(f: File)(implicit jupyterApi: JupyterApi): SendLogToConsole = { | ||
|
||
// It seems the file must exist for the reader above to get content appended to it. | ||
if (!f.exists()) | ||
Files.write(f.toPath, Array.emptyByteArray, StandardOpenOption.CREATE) | ||
|
||
val sendLog = new SendLogToConsole(f, jupyterApi.consoleErr) | ||
sendLog.start() | ||
sendLog | ||
} | ||
|
||
} |
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