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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
