Skip to content

[SPARK-19306][Core] Fix inconsistent state in DiskBlockObject when expection occurred #16657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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 @@ -128,16 +128,19 @@ private[spark] class DiskBlockObjectWriter(
*/
private def closeResources(): Unit = {
if (initialized) {
mcs.manualClose()
channel = null
mcs = null
bs = null
fos = null
ts = null
objOut = null
initialized = false
streamOpen = false
hasBeenClosed = true
Utils.tryWithSafeFinally {
mcs.manualClose()
} {
channel = null
mcs = null
bs = null
fos = null
ts = null
objOut = null
initialized = false
streamOpen = false
hasBeenClosed = true
}
}
}

Expand Down Expand Up @@ -199,26 +202,29 @@ private[spark] class DiskBlockObjectWriter(
def revertPartialWritesAndClose(): File = {
// Discard current writes. We do this by flushing the outstanding writes and then
// truncating the file to its initial position.
try {
Utils.tryWithSafeFinally {
if (initialized) {
writeMetrics.decBytesWritten(reportedPosition - committedPosition)
writeMetrics.decRecordsWritten(numRecordsWritten)
streamOpen = false
closeResources()
}

val truncateStream = new FileOutputStream(file, true)
} {
var truncateStream: FileOutputStream = null
try {
truncateStream = new FileOutputStream(file, true)
truncateStream.getChannel.truncate(committedPosition)
file
} catch {
case e: Exception =>
logError("Uncaught exception while reverting partial writes to file " + file, e)
} finally {
truncateStream.close()
if (truncateStream != null) {
truncateStream.close()
truncateStream = null
}
}
} catch {
case e: Exception =>
logError("Uncaught exception while reverting partial writes to file " + file, e)
file
}
file
}

/**
Expand Down