You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2024/10/09 08:32:52 UTC
[PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
zhengruifeng opened a new pull request, #48391:
URL: https://github.com/apache/spark/pull/48391
### What changes were proposed in this pull request?
1, Introduce a helper class `Lazy` to replace the lazy vals
2, Fix a deadlock in subquery execution
### Why are the changes needed?
we observed a deadlock between `QueryPlan.canonicalized` and `QueryPlan.references`:
The main thread `TakeOrderedAndProject.doExecute` is trying to compute `outputOrdering`, it top-down traverse the tree, and requires the lock of `QueryPlan.canonicalized` in the path.
In this deadlock, it successfully obtained the lock of `WholeStageCodegenExec` and requires the lock of `HashAggregateExec`;
Concurrently, a subquery execution thread is performing code generation and bottom-up traverses the tree via `def consume`, which checks `WholeStageCodegenExec.usedInputs` and refererences a lazy val `QueryPlan.references`. It requires the lock of `QueryPlan.references` in the path.
In this deadlock, it successfully obtained the lock of `HashAggregateExec` and requires the lock of `WholeStageCodegenExec`;
This is due to Scala's lazy val internally calls this.synchronized on the instance that contains the val. This creates a potential for deadlocks.
### Does this PR introduce _any_ user-facing change?
no
### How was this patch tested?
manually test
### Was this patch authored or co-authored using generative AI tooling?
no
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1796190664
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
+@SerialVersionUID(7964587975756091988L)
Review Comment:
I think that this `SerialVersionUID` stems indirectly from the unit test case
```
test("Lazy val serializes the value, even if dereference was never called")
```
which looks like it is testing for some serialization behaviors related to whether or not we serialize the `initializer` closure.
In this PR's current implementation, the `stream.defaultWriteObject()` after field initialization means that we actually have _strict_ / eager semantics when serializing these fields and that we won't serialize the `initializer`.
To see that more clearly, we can use a tool like [`cfr-decompiler`](https://www.benf.org/other/cfr/) to decompile the generated code, from which we see that the `initializer` reference gets cleared after its use in the lazy val evaluation
```java
private Function0<T> initializer;
private volatile boolean bitmap$0;
private T value$lzycompute() {
Lazy lazy = this;
synchronized (lazy) {
if (!this.bitmap$0) {
this.value = this.initializer.apply();
this.bitmap$0 = true;
}
}
this.initializer = null;
return this.value;
}
private T value() {
if (!this.bitmap$0) {
return this.value$lzycompute();
}
return this.value;
}
```
There's a bit of a trade-off space here:
- Eager evaluation _upon serialization_ may reduce the size of the serialized data in case the `initializer` lambda/closure is large in comparison to the value it produces and can avoid problems from non-serializable lambda/closures, or compatibility problems in case wire protocol bidirectional compatibility is needed (e.g. if you are somehow _persisting_ a `Lazy[T]` and deserializing it with a different classpath than the one that generated it, because it then changes the compatibility surface to include compatibility of the lambda / closure serialization rather than the value).
- But this comes at the cost of potentially forcing more evaluations than otherwise would have happened.
In our context of use here, I think that by default it's better to forego the `SerialVersionUID` and "eager serialization" semantics completely and instead just do what `LazyTry` did, as the "eager evaluation upon serialization" semantic may be unwanted in this context of use or may address a set of problems that we don't have in this context.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2402164650
@panbingkun thanks a lot for confirming the SerialVersionUID!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #48391: [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals
URL: https://github.com/apache/spark/pull/48391
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1802756562
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
shall we mark `Lazy#value` as `@transient`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1802250661
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
good point!
according to the description of https://github.com/apache/spark/pull/24866 , it mainly affect the planning time.
~~Maybe we can add `@transient` to `_references`~~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1796265683
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
+@SerialVersionUID(7964587975756091988L)
Review Comment:
sounds good, let me remove the `SerialVersionUID` and "eager serialization"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2421842765
thanks, merging to master!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2418195817
convert to draft for now to avoid merge by mistake
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "panbingkun (via GitHub)" <gi...@apache.org>.
panbingkun commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2402093531
- branch 3.4 + scala 2.12.17
- branch 3.4 + scala 2.13.8
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1802250661
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
good catch!
according to the description of https://github.com/apache/spark/pull/24866 , it mainly affect the planning time.
~~Maybe we can add `@transient` to `_references`~~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2418737719
Decompiling with `cfr-decompiler` generates:
```
/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* scala.Function0
* scala.reflect.ScalaSignature
*/
package org.apache.spark.util;
import java.io.Serializable;
import scala.Function0;
import scala.reflect.ScalaSignature;
@ScalaSignature(bytes="\u0006\u0005}2Q!\u0002\u0004\u0001\u00119A\u0001b\t\u0001\u0003\u0002\u0013\u0006I\u0001\n\u0005\u0006e\u0001!\ta\r\u0005\to\u0001A)\u0019)C\u0005q!)Q\b\u0001C\u0001}\tiAK]1og&,g\u000e\u001e'bufT!a\u0002\u0005\u0002\tU$\u0018\u000e\u001c\u0006\u0003\u0013)\tQa\u001d9be.T!a\u0003\u0007\u0002\r\u0005\u0004\u0018m\u00195f\u0015\u0005i\u0011aA8sOV\u0011q\"K\n\u0004\u0001A1\u0002CA\t\u0015\u001b\u0005\u0011\"\"A\n\u0002\u000bM\u001c\u0017\r\\1\n\u0005U\u0011\"AB!osJ+g\r\u0005\u0002\u0018A9\u0011\u0001D\b\b\u00033ui\u0011A\u0007\u0006\u00037q\ta\u0001\u0010:p_Rt4\u0001A\u0005\u0002'%\u0011qDE\u0001\ba\u0006\u001c7.Y4f\u0013\t\t#E\u0001\u0007TKJL\u0017\r\\5{C\ndWM\u0003\u0002 %\u0005Y\u0011N\\5uS\u0006d\u0017N_3s!\r\tReJ\u0005\u0003MI\u0011\u0001\u0002\u00102z]\u0006lWM\u0010\t\u0003Q%b\u0001\u0001B\u0003+\u0001\t\u00071FA\u0001U#\tas\u0006\u0005\u0002\u0012[%\u0011aF\u0005\u0002\b\u001d>$\b.\u001b8h!\t\t\u0002'\u0003\u00022%\t\u0019\u0011I\\=\u0002\rqJg.\u001b;?)\
t!d\u0007E\u00026\u0001\u001dj\u0011A\u0002\u0005\u0007G\t!\t\u0019\u0001\u0013\u0002\u000bY\fG.^3\u0016\u0003\u001dB#a\u0001\u001e\u0011\u0005EY\u0014B\u0001\u001f\u0013\u0005%!(/\u00198tS\u0016tG/A\u0003baBd\u0017\u0010F\u0001(\u0001")
public class TransientLazy<T>
implements Serializable {
private transient T value;
private final Function0<T> initializer;
private volatile transient boolean bitmap$trans$0;
private T value$lzycompute() {
TransientLazy transientLazy = this;
synchronized (transientLazy) {
if (!this.bitmap$trans$0) {
this.value = this.initializer.apply();
this.bitmap$trans$0 = true;
}
}
return this.value;
}
private T value() {
if (!this.bitmap$trans$0) {
return this.value$lzycompute();
}
return this.value;
}
public T apply() {
return this.value();
}
public TransientLazy(Function0<T> initializer) {
this.initializer = initializer;
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1793276161
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
+@SerialVersionUID(7964587975756091988L)
Review Comment:
generated by:
```
(spark_dev_312) ➜ spark git:(query_plan_lazy_ref) /usr/bin/serialver -classpath /Users/ruifeng.zheng/Dev/spark/core/target/scala-2.13/spark-core_2.13-4.0.0-SNAPSHOT.jar:/Users/ruifeng.zheng/Dev/spark/build/scala-2.12.18/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
(spark_dev_312) ➜ spark git:(query_plan_lazy_ref) /usr/bin/serialver -classpath /Users/ruifeng.zheng/Dev/spark/core/target/scala-2.13/spark-core_2.13-4.0.0-SNAPSHOT.jar:/Users/ruifeng.zheng/Dev/spark/build/scala-2.13.12/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
(spark_dev_312) ➜ spark git:(query_plan_lazy_ref) /usr/bin/serialver -classpath /Users/ruifeng.zheng/Dev/spark/core/target/scala-2.13/spark-core_2.13-4.0.0-SNAPSHOT.jar:/Users/ruifeng.zheng/Dev/spark/build/scala-2.13.13/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "panbingkun (via GitHub)" <gi...@apache.org>.
panbingkun commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1793348358
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
+@SerialVersionUID(7964587975756091988L)
Review Comment:
<del>
- scala-2.12.20
```scala
(base) ➜ spark-community git:(master) ✗ serialver -classpath core/target/spark-core_2.13-4.0.0-SNAPSHOT.jar:build/scala-2.12.20/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
- scala-2.13.12
```scala
(base) ➜ spark-community git:(master) ✗ serialver -classpath core/target/spark-core_2.13-4.0.0-SNAPSHOT.jar:build/scala-2.13.12/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
- scala-2.13.13
```scala
(base) ➜ spark-community git:(master) ✗ serialver -classpath core/target/spark-core_2.13-4.0.0-SNAPSHOT.jar:build/scala-2.13.13/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
</del>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1796194610
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
Review Comment:
Scala 3 uses a different implementation of lazy vals which doesn't have this problem. You can reference SIP-20 / https://docs.scala-lang.org/scala3/reference/changed-features/lazy-vals-init.html which has some pointers to more references on this class of deadlock issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1803480921
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
+1 for `TransientLazy`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1795086341
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
Review Comment:
let's also highlight the difference between this and `LazyTry` in the classdoc.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2412651739
cc @JoshRosen would you mind taking another look? thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1802217903
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
Sorry for only spotting this now, but:
Previously we would recompute these attribute references if they were requested on a QueryPlan that had been serialized and shipped to executors, whereas after this PR's change we'll use attributes sent from the driver.
Are there any potentially adverse side effects of this, other than serialized size? I vaguely recall that there was some attribute JVM ID reference stuff somewhere, but I'm unsure offhand if that's a factor here.
I mention this because the `@transient` was added in https://github.com/apache/spark/pull/24866/commits/4483331586090dc5ddc79c646e1c18eb1aa0742c and I'm wondering if it was somehow load bearing or whether it's just a perf. optimization.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "tedyu (via GitHub)" <gi...@apache.org>.
tedyu commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2423193103
Since the code before change had `@transient` already, I don't think there is behavioral change in terms of caching exceptions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "panbingkun (via GitHub)" <gi...@apache.org>.
panbingkun commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1793348358
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
+@SerialVersionUID(7964587975756091988L)
Review Comment:
- scala-2.12.20
```scala
(base) ➜ spark-community git:(master) ✗ serialver -classpath core/target/spark-core_2.13-4.0.0-SNAPSHOT.jar:build/scala-2.12.20/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
- scala-2.13.12
```scala
(base) ➜ spark-community git:(master) ✗ serialver -classpath core/target/spark-core_2.13-4.0.0-SNAPSHOT.jar:build/scala-2.13.12/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
- scala-2.13.13
```scala
(base) ➜ spark-community git:(master) ✗ serialver -classpath core/target/spark-core_2.13-4.0.0-SNAPSHOT.jar:build/scala-2.13.13/lib/scala-library.jar org.apache.spark.util.Lazy
org.apache.spark.util.Lazy: private static final long serialVersionUID = 7964587975756091988L;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1802250661
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
good catch!
according to the description of https://github.com/apache/spark/pull/24866 , it mainly affect the planning time.
Maybe we can add `@transient` to `_references`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "JoshRosen (via GitHub)" <gi...@apache.org>.
JoshRosen commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1803469928
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala:
##########
@@ -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
Review Comment:
Making `Lazy#value` a `@transient` field is an interesting proposal and seems promising.
If we do this, I think we should consider changing the name to `TransientLazy` or `LazyTransient` for the sake of making the transient aspect more salient to a reader / reviewer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #48391:
URL: https://github.com/apache/spark/pull/48391#discussion_r1795086703
##########
core/src/main/scala/org/apache/spark/util/Lazy.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.ObjectOutputStream
+
+/**
+ * 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.
+ */
+@SerialVersionUID(7964587975756091988L)
Review Comment:
why doesn't `LazyTry` need it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-49808][SQL] Fix a deadlock in subquery execution due to lazy vals [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #48391:
URL: https://github.com/apache/spark/pull/48391#issuecomment-2420886672
based on the [Decompiled code](https://github.com/apache/spark/pull/48391#issuecomment-2418737719), I think the `TransientLazy` works as expected, would you mind taking another look?
@JoshRosen @cloud-fan
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org