Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.util.logging

import java.io.{File, FileFilter, InputStream}

import org.apache.commons.io.FileUtils
import com.google.common.io.Files
import org.apache.spark.SparkConf
import RollingFileAppender._

Expand Down Expand Up @@ -83,7 +83,7 @@ private[spark] class RollingFileAppender(
logDebug(s"Attempting to rollover file $activeFile to file $rolloverFile")
if (activeFile.exists) {
if (!rolloverFile.exists) {
FileUtils.moveFile(activeFile, rolloverFile)
Files.move(activeFile, rolloverFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using java.nio.file.Files.move(Path source, Path target, CopyOption... options)? Does com.google.common.io.Files have better performance than the built-in Files in jdk?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, it's because it doesn't exist in Java 6 :) http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

logInfo(s"Rolled over $activeFile to $rolloverFile")
} else {
// In case the rollover file name clashes, make a unique file name.
Expand All @@ -100,7 +100,7 @@ private[spark] class RollingFileAppender(

logWarning(s"Rollover file $rolloverFile already exists, " +
s"rolled over $activeFile to file $altRolloverFile")
FileUtils.moveFile(activeFile, altRolloverFile)
Files.move(activeFile, altRolloverFile)
}
} else {
logWarning(s"File $activeFile does not exist")
Expand Down
17 changes: 10 additions & 7 deletions core/src/test/scala/org/apache/spark/util/FileAppenderSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
package org.apache.spark.util

import java.io._
import java.nio.charset.Charset

import scala.collection.mutable.HashSet
import scala.reflect._

import org.apache.commons.io.{FileUtils, IOUtils}
import org.apache.spark.{Logging, SparkConf}
import org.scalatest.{BeforeAndAfter, FunSuite}

import com.google.common.io.Files

import org.apache.spark.{Logging, SparkConf}
import org.apache.spark.util.logging.{RollingFileAppender, SizeBasedRollingPolicy, TimeBasedRollingPolicy, FileAppender}

class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
Expand All @@ -41,11 +44,11 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {

test("basic file appender") {
val testString = (1 to 1000).mkString(", ")
val inputStream = IOUtils.toInputStream(testString)
val inputStream = new ByteArrayInputStream(testString.getBytes(Charset.forName("UTF-8")))
val appender = new FileAppender(inputStream, testFile)
inputStream.close()
appender.awaitTermination()
assert(FileUtils.readFileToString(testFile) === testString)
assert(Files.toString(testFile, Charset.forName("UTF-8")) === testString)
}

test("rolling file appender - time-based rolling") {
Expand Down Expand Up @@ -93,7 +96,7 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
val allGeneratedFiles = new HashSet[String]()
val items = (1 to 10).map { _.toString * 10000 }
for (i <- 0 until items.size) {
testOutputStream.write(items(i).getBytes("UTF8"))
testOutputStream.write(items(i).getBytes(Charset.forName("UTF-8")))
testOutputStream.flush()
allGeneratedFiles ++= RollingFileAppender.getSortedRolledOverFiles(
testFile.getParentFile.toString, testFile.getName).map(_.toString)
Expand Down Expand Up @@ -197,7 +200,7 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
// send data to appender through the input stream, and wait for the data to be written
val expectedText = textToAppend.mkString("")
for (i <- 0 until textToAppend.size) {
outputStream.write(textToAppend(i).getBytes("UTF8"))
outputStream.write(textToAppend(i).getBytes(Charset.forName("UTF-8")))
outputStream.flush()
Thread.sleep(sleepTimeBetweenTexts)
}
Expand All @@ -212,7 +215,7 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
logInfo("Filtered files: \n" + generatedFiles.mkString("\n"))
assert(generatedFiles.size > 1)
val allText = generatedFiles.map { file =>
FileUtils.readFileToString(file)
Files.toString(file, Charset.forName("UTF-8"))
}.mkString("")
assert(allText === expectedText)
generatedFiles
Expand Down