Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -327,12 +327,14 @@ class FileContextBasedCheckpointFileManager(path: Path, hadoopConf: Configuratio
override def renameTempFile(srcPath: Path, dstPath: Path, overwriteIfPossible: Boolean): Unit = {
import Options.Rename._
fc.rename(srcPath, dstPath, if (overwriteIfPossible) OVERWRITE else NONE)
mayRemoveCrcFile(srcPath)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ideal behavior would be renaming crc file to be matched with dstPath, but in SPARK-17475 seems like we just decided to remove the crc file, so I guess this is OK.

Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to detect whether checksum is enabled? If it's disabled, we can save an exists call.

Copy link
Member

Choose a reason for hiding this comment

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

NVM. Just saw your latest comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, please feel free to let me know if we prefer alternative approach - I can make a change.

}


override def delete(path: Path): Unit = {
try {
fc.delete(path, true)
mayRemoveCrcFile(path)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like org.apache.hadoop.fs.ChecksumFs.delete handles the file deletion correctly, so we don't need this change.

Copy link
Contributor Author

@HeartSaVioR HeartSaVioR Aug 23, 2019

Choose a reason for hiding this comment

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

Ah, thanks for pointing out.

I just realized I analyzed incorrectly. I confused classes in stack trace hence confused the root reason of bug. I thought FileContext doesn't seem to pick LocalFs (extends ChecksumFs), but turned out that's not the case. The root reason is, there're two renameInternal methods:

public void renameInternal(Path src, Path dst)
public void renameInternal(final Path src, final Path dst, boolean overwrite)

which should be overridden to handle all cases but ChecksumFs only overrides method with 2 params, so when latter is called FilterFs.renameInternal(...) is called instead, and it will do rename with RawLocalFs as underlying filesystem.

I'll update the description of PR and remove this line.

} catch {
case e: FileNotFoundException =>
// ignore if file has already been deleted
Expand All @@ -343,5 +345,17 @@ class FileContextBasedCheckpointFileManager(path: Path, hadoopConf: Configuratio
case _: LocalFs | _: RawLocalFs => true // LocalFs = RawLocalFs + ChecksumFs
case _ => false
}

private def mayRemoveCrcFile(path: Path): Unit = {
val checksumFile = new Path(path.getParent, s".${path.getName}.crc")
Copy link
Member

Choose a reason for hiding this comment

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

I just found one bug in this Path API: https://issues.apache.org/jira/browse/HDFS-14762
How about wrap the whole block like this

try {
  val checksumFile = new Path(path.getParent, s".${path.getName}.crc")
  if (exists(checksumFile)) {
        // checksum file exists, deleting it
        delete(checksumFile)
      }
} catch {
  case NonFatal(_) => ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice finding! HDFS-14762 looks odd but we have to live with it. The suggestion looks great.

try {
if (exists(checksumFile)) {
// checksum file exists, deleting it
delete(checksumFile)
}
} catch {
case _: IOException => // ignore, we are removing crc file as "best-effort"
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ abstract class CheckpointFileManagerTests extends SparkFunSuite with SQLHelper {
assert(fm.exists(path))
fm.createAtomic(path, overwriteIfPossible = true).close() // should not throw exception

// crc file should not be leaked when origin file doesn't exist.
// The implementation of Hadoop filesystem may filter out checksum file, so
// listing files from local filesystem.
val fileNames = new File(path.getParent.toString).listFiles().toSeq
.filter(p => p.isFile).map(p => p.getName)
val crcFiles = fileNames.filter(n => n.startsWith(".") && n.endsWith(".crc"))
val originFileNamesForExistingCrcFiles = crcFiles.map { name =>
// remove first "." and last ".crc"
name.substring(1, name.length - 4)
}

// Check all origin files exist for all crc files.
assert(originFileNamesForExistingCrcFiles.toSet.subsetOf(fileNames.toSet),
s"Some of origin files for crc files don't exist - crc files: $crcFiles / " +
s"expected origin files: $originFileNamesForExistingCrcFiles / actual files: $fileNames")

// Open and delete
fm.open(path).close()
fm.delete(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scala.language.implicitConversions
import org.scalatest.concurrent.Waiters._
import org.scalatest.time.SpanSugar._

import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.util.UninterruptibleThread

Expand Down Expand Up @@ -58,6 +59,21 @@ class HDFSMetadataLogSuite extends SharedSparkSession {
}

test("HDFSMetadataLog: purge") {
testPurge()
}

Seq(
classOf[FileSystemBasedCheckpointFileManager],
classOf[FileContextBasedCheckpointFileManager]
).map(_.getCanonicalName).foreach { cls =>
test(s"HDFSMetadataLog: purge - explicit file manager - $cls") {
withSQLConf(SQLConf.STREAMING_CHECKPOINT_FILE_MANAGER_CLASS.parent.key -> cls) {
testPurge()
}
}
}

private def testPurge(): Unit = {
withTempDir { temp =>
val metadataLog = new HDFSMetadataLog[String](spark, temp.getAbsolutePath)
assert(metadataLog.add(0, "batch0"))
Expand All @@ -74,12 +90,16 @@ class HDFSMetadataLogSuite extends SharedSparkSession {
assert(metadataLog.get(2).isDefined)
assert(metadataLog.getLatest().get._1 == 2)

// There should be exactly one file, called "2", in the metadata directory.
// There should be at most two files, called "2", and optionally crc file,
// in the metadata directory.
// This check also tests for regressions of SPARK-17475
val allFiles = new File(metadataLog.metadataPath.toString).listFiles()
.filter(!_.getName.startsWith(".")).toSeq
Copy link
Contributor Author

@HeartSaVioR HeartSaVioR Aug 18, 2019

Choose a reason for hiding this comment

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

This filter broke the check of regression on SPARK-17475, hence regression came in.

assert(allFiles.size == 1)
assert(allFiles(0).getName() == "2")
val allFiles = new File(metadataLog.metadataPath.toString).listFiles().toSeq
assert(allFiles.size <= 2)
assert(allFiles.exists(_.getName == "2"))
if (allFiles.size == 2) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FileSystemBasedCheckpointFileManager is the case of crc file being left: it's not a leak because the origin file exists. It correctly deletes crc file when origin file is requested to be deleted, hence crc files for 0 and 1 don't exist.

// there's possibly crc file being left as well
assert(allFiles.exists(_.getName == ".2.crc"))
}
}
}

Expand Down