-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-17666] Ensure that RecordReaders are closed by data source file scans #15245
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
|
||
package org.apache.spark.sql.execution.datasources | ||
|
||
import java.io.Closeable | ||
|
||
import org.apache.hadoop.mapreduce.RecordReader | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
|
@@ -27,7 +29,8 @@ import org.apache.spark.sql.catalyst.InternalRow | |
* Note that this returns [[Object]]s instead of [[InternalRow]] because we rely on erasure to pass | ||
* column batches by pretending they are rows. | ||
*/ | ||
class RecordReaderIterator[T](rowReader: RecordReader[_, T]) extends Iterator[T] { | ||
class RecordReaderIterator[T]( | ||
private[this] var rowReader: RecordReader[_, T]) extends Iterator[T] with Closeable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By nulling out the |
||
private[this] var havePair = false | ||
private[this] var finished = false | ||
|
||
|
@@ -38,7 +41,7 @@ class RecordReaderIterator[T](rowReader: RecordReader[_, T]) extends Iterator[T] | |
// Close and release the reader here; close() will also be called when the task | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is |
||
// completes, but for tasks that read from many files, it helps to release the | ||
// resources early. | ||
rowReader.close() | ||
close() | ||
} | ||
havePair = !finished | ||
} | ||
|
@@ -52,4 +55,18 @@ class RecordReaderIterator[T](rowReader: RecordReader[_, T]) extends Iterator[T] | |
havePair = false | ||
rowReader.getCurrentValue | ||
} | ||
|
||
override def close(): Unit = { | ||
if (rowReader != null) { | ||
try { | ||
// Close the reader and release it. Note: it's very important that we don't close the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is copied from |
||
// reader more than once, since that exposes us to MAPREDUCE-5918 when running against | ||
// older Hadoop 2.x releases. That bug can lead to non-deterministic corruption issues | ||
// when reading compressed input. | ||
rowReader.close() | ||
} finally { | ||
rowReader = null | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a
RecordReaderIterator
, whose memory footprint should become practically nothing afterclose()
is called, hence my decision to not null things out here.