Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/895#discussion_r13516836
  
    --- Diff: core/src/main/scala/org/apache/spark/util/FileAppender.scala ---
    @@ -0,0 +1,410 @@
    +/*
    + * 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.util
    +
    +import java.io.{File, FileFilter, FileOutputStream, InputStream}
    +import java.text.SimpleDateFormat
    +import java.util.Calendar
    +import java.util.concurrent.{Executors, ThreadFactory}
    +
    +import org.apache.commons.io.FileUtils
    +import org.apache.spark.{Logging, SparkConf}
    +import RollingFileAppender._
    +
    +/**
    + * Continuously appends the data from an input stream into the given file.
    + */
    +private[spark] class FileAppender(inputStream: InputStream, file: File, 
bufferSize: Int = 8192)
    +  extends Logging {
    +  @volatile private var outputStream: FileOutputStream = null
    +  @volatile private var markedForStop = false     // has the appender been 
asked to stopped
    +  @volatile private var stopped = false           // has the appender 
stopped
    +
    +  // Thread that reads the input stream and writes to file
    +  private val writingThread = new Thread("File appending thread for " + 
file) {
    +    setDaemon(true)
    +    override def run() {
    +      Utils.logUncaughtExceptions {
    +        appendStreamToFile()
    +      }
    +    }
    +  }
    +  writingThread.start()
    +
    +  /**
    +   * Wait for the appender to stop appending, either because input stream 
is closed
    +   * or because of any error in appending
    +   */
    +  def awaitTermination() {
    +    synchronized {
    +      if (!stopped) {
    +        wait()
    +      }
    +    }
    +  }
    +
    +  /** Stop the appender */
    +  def stop() {
    +    markedForStop = true
    +  }
    +
    +  /** Continuously read chunks from the input stream and append to the 
file */
    +  protected def appendStreamToFile() {
    +    try {
    +      logDebug("Started appending thread")
    +      openFile()
    +      val buf = new Array[Byte](bufferSize)
    +      var n = 0
    +      while (!markedForStop && n != -1) {
    +        n = inputStream.read(buf)
    +        if (n != -1) {
    +          appendToFile(buf, n)
    +        }
    +      }
    +    } catch {
    +      case e: Exception =>
    +        logError(s"Error writing stream to file $file", e)
    +    } finally {
    +      closeFile()
    +      synchronized {
    +        stopped = true
    +        notifyAll()
    +      }
    +    }
    +  }
    +
    +  /** Append bytes to the file output stream */
    +  protected def appendToFile(bytes: Array[Byte], len: Int) {
    +    if (outputStream == null) {
    +      openFile()
    +    }
    +    outputStream.write(bytes, 0, len)
    +  }
    +
    +  /** Open the file output stream */
    +  protected def openFile() {
    +    outputStream = new FileOutputStream(file, true)
    +    logDebug(s"Opened file $file")
    +  }
    +
    +  /** Close the file output stream */
    +  protected def closeFile() {
    +    outputStream.flush()
    +    outputStream.close()
    +    logDebug(s"Closed file $file")
    +  }
    +}
    +
    +/**
    + * Companion object to [[org.apache.log4j.FileAppender]] which has helper
    + * functions to choose the write type of FileAppender based on SparkConf 
configuration.
    --- End diff --
    
    yikes!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to