Skip to content

Commit

Permalink
[SPARK-21696][SS] Fix a potential issue that may generate partial sna…
Browse files Browse the repository at this point in the history
…pshot files

## What changes were proposed in this pull request?

Directly writing a snapshot file may generate a partial file. This PR changes it to write to a temp file then rename to the target file.

## How was this patch tested?

Jenkins.

Author: Shixiong Zhu <shixiong@databricks.com>

Closes apache#18928 from zsxwing/SPARK-21696.
  • Loading branch information
zsxwing authored and tdas committed Aug 14, 2017
1 parent fbc2692 commit 282f00b
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,11 @@ private[state] class HDFSBackedStateStoreProvider extends StateStoreProvider wit

private def writeSnapshotFile(version: Long, map: MapType): Unit = {
val fileToWrite = snapshotFile(version)
val tempFile =
new Path(fileToWrite.getParent, s"${fileToWrite.getName}.temp-${Random.nextLong}")
var output: DataOutputStream = null
Utils.tryWithSafeFinally {
output = compressStream(fs.create(fileToWrite, false))
output = compressStream(fs.create(tempFile, false))
val iter = map.entrySet().iterator()
while(iter.hasNext) {
val entry = iter.next()
Expand All @@ -403,6 +405,12 @@ private[state] class HDFSBackedStateStoreProvider extends StateStoreProvider wit
} {
if (output != null) output.close()
}
if (fs.exists(fileToWrite)) {
// Skip rename if the file is alreayd created.
fs.delete(tempFile, true)
} else if (!fs.rename(tempFile, fileToWrite)) {
throw new IOException(s"Failed to rename $tempFile to $fileToWrite")
}
logInfo(s"Written snapshot file for version $version of $this at $fileToWrite")
}

Expand Down

0 comments on commit 282f00b

Please sign in to comment.