Skip to content
Closed
Show file tree
Hide file tree
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 @@ -207,7 +207,7 @@ private[execution] class ValueRowWithKeyIndex {
* A HashedRelation for UnsafeRow, which is backed BytesToBytesMap.
*
* It's serialized in the following format:
* [number of keys]
* [number of keys] [number of fields]
* [size of key] [size of value] [key bytes] [bytes for value]
*/
private[joins] class UnsafeHashedRelation(
Expand Down Expand Up @@ -364,6 +364,7 @@ private[joins] class UnsafeHashedRelation(
writeInt: (Int) => Unit,
writeLong: (Long) => Unit,
writeBuffer: (Array[Byte], Int, Int) => Unit) : Unit = {
writeInt(numKeys)
writeInt(numFields)
// TODO: move these into BytesToBytesMap
writeLong(binaryMap.numKeys())
Expand Down Expand Up @@ -397,6 +398,7 @@ private[joins] class UnsafeHashedRelation(
readInt: () => Int,
readLong: () => Long,
readBuffer: (Array[Byte], Int, Int) => Unit): Unit = {
numKeys = readInt()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did we get the num keys before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field has not been serialized from the beginning.
The reason why this problem was not triggered before is because TorrentBroadcast._value is like this:
private lazy val _value: T = readBroadcastBlock()

It was modified after #22995 and became like this:
private var _value: SoftReference[T] = _

The driver side may deserialize UnsafeHashedRelation, and then cause this problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The executors also need to deserialize the hash relation, why they are fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the numKeys field is only used in the UnsafeHashedRelation.keys() method, and UnsafeHashedRelation.keys() is only used on the driver side, so the executor will not trigger this problem.

And the numKeys field does not affect the execution of the UnsafeHashedRelation.get() method, so there was no problem before.

My personal understanding, you can also help analyze.

numFields = readInt()
resultRow = new UnsafeRow(numFields)
val nKeys = readLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class HashedRelationSuite extends SharedSparkSession {
assert(hashed2.get(toUnsafe(InternalRow(10))) === null)
assert(hashed2.get(unsafeData(2)).toArray === data2)

// SPARK-38542: UnsafeHashedRelation should serialize numKeys out
assert(hashed2.keys().map(_.copy()).forall(_.numFields == 1))

val os2 = new ByteArrayOutputStream()
val out2 = new ObjectOutputStream(os2)
hashed2.writeExternal(out2)
Expand Down