@@ -27,6 +27,7 @@ import scala.util.control.NonFatal
2727import com .google .common .io .ByteStreams
2828import org .apache .hadoop .conf .Configuration
2929import org .apache .hadoop .fs .{FileStatus , Path }
30+ import org .apache .hadoop .hdfs .protocol .HdfsConstants
3031
3132import org .apache .spark .{SparkConf , SparkEnv }
3233import org .apache .spark .internal .Logging
@@ -274,8 +275,19 @@ private[state] class HDFSBackedStateStoreProvider(
274275 private def commitUpdates (newVersion : Long , map : MapType , tempDeltaFile : Path ): Path = {
275276 synchronized {
276277 val finalDeltaFile = deltaFile(newVersion)
277- if (! fs.rename(tempDeltaFile, finalDeltaFile)) {
278- throw new IOException (s " Failed to rename $tempDeltaFile to $finalDeltaFile" )
278+ /** When job restart, delta file may be already generated before on account of offsets WAL. */
279+ if (fs.getScheme == HdfsConstants .HDFS_URI_SCHEME ) {
280+ if (! fs.exists(finalDeltaFile)) {
281+ if (! fs.rename(tempDeltaFile, finalDeltaFile)) {
282+ throw new IOException (s " Failed to rename $tempDeltaFile to $finalDeltaFile" )
283+ }
284+ } else {
285+ fs.delete(tempDeltaFile, true )
286+ }
287+ } else {
288+ if (! fs.rename(tempDeltaFile, finalDeltaFile)) {
289+ throw new IOException (s " Failed to rename $tempDeltaFile to $finalDeltaFile" )
290+ }
279291 }
280292 loadedMaps.put(newVersion, map)
281293 finalDeltaFile
0 commit comments