Skip to content
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

[SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals #48391

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions core/src/main/scala/org/apache/spark/util/Lazy.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.util

/**
* Construct to lazily initialize a variable.
* This may be helpful for avoiding deadlocks in certain scenarios. For example,
* a) Thread 1 entered a synchronized method, grabbing a coarse lock on the parent object.
* b) Thread 2 gets spawned off, and tries to initialize a lazy value on the same parent object
* (in our case, this was the logger). This causes scala to also try to grab a coarse lock on
* the parent object.
* c) If thread 1 waits for thread 2 to join, a deadlock occurs.
cloud-fan marked this conversation as resolved.
Show resolved Hide resolved
* The main difference between this and [[LazyTry]] is that this does not cache failures.
*
* @note
* Scala 3 uses a different implementation of lazy vals which doesn't have this problem.
* Please refer to <a
* href="https://docs.scala-lang.org/scala3/reference/changed-features/lazy-vals-init.html">Lazy
* Vals Initialization</a> for more details.
*/
cloud-fan marked this conversation as resolved.
Show resolved Hide resolved
private[spark] class Lazy[T](initializer: => T) extends Serializable {

private[this] lazy val value: T = initializer

def apply(): T = {
value
}
}
60 changes: 60 additions & 0 deletions core/src/test/scala/org/apache/spark/util/LazySuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.util

import java.io.{ByteArrayOutputStream, NotSerializableException, ObjectOutputStream}

import org.apache.spark.SparkFunSuite

class LazySuite extends SparkFunSuite {
test("Lazy val works") {
var test: Option[Object] = None

val lazyval = new Lazy({
test = Some(new Object())
test
})

// Ensure no initialization happened before the lazy value was dereferenced
assert(test.isEmpty)

// Ensure the first invocation creates a new object
assert(lazyval() == test && test.isDefined)

// Ensure the subsequent invocation serves the same object
assert(lazyval() == test && test.isDefined)
}

test("Lazy val is serializable") {
val lazyval = new Lazy({
new Object()
})

// Ensure we are serializable before the dereference
val oos = new ObjectOutputStream(new ByteArrayOutputStream())
oos.writeObject(lazyval)

@SuppressWarnings(Array("never used"))
val dereferenced = lazyval()

// Ensure we are not serializable after the dereference (type "Object" is not serializable)
intercept[NotSerializableException] {
val oos2 = new ObjectOutputStream(new ByteArrayOutputStream())
oos2.writeObject(lazyval)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.apache.spark.sql.catalyst.trees.TreePatternBits
import org.apache.spark.sql.catalyst.types.DataTypeUtils
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{DataType, StructType}
import org.apache.spark.util.Lazy
import org.apache.spark.util.collection.BitSet

/**
Expand Down Expand Up @@ -94,10 +95,11 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]]
* All Attributes that appear in expressions from this operator. Note that this set does not
* include attributes that are implicitly referenced by being passed through to the output tuple.
*/
@transient
lazy val references: AttributeSet = {
AttributeSet.fromAttributeSets(expressions.map(_.references)) -- producedAttributes
}
def references: AttributeSet = _references()

private val _references = new Lazy({
AttributeSet(expressions) -- producedAttributes
})

/**
* Returns true when the all the expressions in the current node as well as all of its children
Expand Down