-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-10466][SQL] UnsafeRow SerDe exception with data spill #8635
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,13 +17,17 @@ | |
|
|
||
| package org.apache.spark.sql.execution | ||
|
|
||
| import java.io.{DataOutputStream, ByteArrayInputStream, ByteArrayOutputStream} | ||
| import java.io.{File, DataOutputStream, ByteArrayInputStream, ByteArrayOutputStream} | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.executor.ShuffleWriteMetrics | ||
| import org.apache.spark.storage.ShuffleBlockId | ||
| import org.apache.spark.util.collection.ExternalSorter | ||
| import org.apache.spark.util.Utils | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow} | ||
| import org.apache.spark.sql.catalyst.expressions.{UnsafeProjection, UnsafeRow} | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark._ | ||
|
|
||
|
|
||
| /** | ||
|
|
@@ -40,9 +44,15 @@ class ClosableByteArrayInputStream(buf: Array[Byte]) extends ByteArrayInputStrea | |
| class UnsafeRowSerializerSuite extends SparkFunSuite { | ||
|
|
||
| private def toUnsafeRow(row: Row, schema: Array[DataType]): UnsafeRow = { | ||
| val internalRow = CatalystTypeConverters.convertToCatalyst(row).asInstanceOf[InternalRow] | ||
| val converter = unsafeRowConverter(schema) | ||
| converter(row) | ||
| } | ||
|
|
||
| private def unsafeRowConverter(schema: Array[DataType]): Row => UnsafeRow = { | ||
|
Contributor
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 method seems strictly unnecessary... we can just remove it in the future.
Contributor
Author
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. Actually
Contributor
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. I mean we can just inline it in
Contributor
Author
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. Yes, I got your mean, if we inline that in Probably we'd better to remove the function |
||
| val converter = UnsafeProjection.create(schema) | ||
| converter.apply(internalRow) | ||
| (row: Row) => { | ||
| converter(CatalystTypeConverters.convertToCatalyst(row).asInstanceOf[InternalRow]) | ||
| } | ||
| } | ||
|
|
||
| test("toUnsafeRow() test helper method") { | ||
|
|
@@ -87,4 +97,50 @@ class UnsafeRowSerializerSuite extends SparkFunSuite { | |
| assert(!deserializerIter.hasNext) | ||
| assert(input.closed) | ||
| } | ||
|
|
||
| test("SPARK-10466: external sorter spilling with unsafe row serializer") { | ||
| var sc: SparkContext = null | ||
| var outputFile: File = null | ||
| val oldEnv = SparkEnv.get // save the old SparkEnv, as it will be overwritten | ||
| Utils.tryWithSafeFinally { | ||
| val conf = new SparkConf() | ||
| .set("spark.shuffle.spill.initialMemoryThreshold", "1024") | ||
| .set("spark.shuffle.sort.bypassMergeThreshold", "0") | ||
| .set("spark.shuffle.memoryFraction", "0.0001") | ||
|
|
||
| sc = new SparkContext("local", "test", conf) | ||
| outputFile = File.createTempFile("test-unsafe-row-serializer-spill", "") | ||
| // prepare data | ||
| val converter = unsafeRowConverter(Array(IntegerType)) | ||
| val data = (1 to 1000).iterator.map { i => | ||
| (i, converter(Row(i))) | ||
| } | ||
| val sorter = new ExternalSorter[Int, UnsafeRow, UnsafeRow]( | ||
| partitioner = Some(new HashPartitioner(10)), | ||
| serializer = Some(new UnsafeRowSerializer(numFields = 1))) | ||
|
|
||
| // Ensure we spilled something and have to merge them later | ||
| assert(sorter.numSpills === 0) | ||
| sorter.insertAll(data) | ||
| assert(sorter.numSpills > 0) | ||
|
|
||
| // Merging spilled files should not throw assertion error | ||
| val taskContext = | ||
| new TaskContextImpl(0, 0, 0, 0, null, null, InternalAccumulator.create(sc)) | ||
| taskContext.taskMetrics.shuffleWriteMetrics = Some(new ShuffleWriteMetrics) | ||
| sorter.writePartitionedFile(ShuffleBlockId(0, 0, 0), taskContext, outputFile) | ||
| } { | ||
| // Clean up | ||
| if (sc != null) { | ||
| sc.stop() | ||
| } | ||
|
|
||
| // restore the spark env | ||
| SparkEnv.set(oldEnv) | ||
|
|
||
| if (outputFile != null) { | ||
| outputFile.delete() | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
keyis possible null, see https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/UnsafeRowSerializer.scala#L146This will happens with external sorting (with data spill).
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.
wouldn't the right thing to do here be to allow nulls as well? In general it's a bad idea to remove assertions
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.
How about change the dummy value to a number (-1) instead of
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.
yeah I like that better. We can't have a partition ID of
-1, whereasnull.asInstanceOf[Int]may be confused with the partition ID of0