Skip to content
Snippets Groups Projects
Commit 282f00b4 authored by Shixiong Zhu's avatar Shixiong Zhu Committed by Tathagata Das
Browse files

[SPARK-21696][SS] Fix a potential issue that may generate partial snapshot 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 #18928 from zsxwing/SPARK-21696.
parent fbc26925
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......@@ -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")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment