-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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-19644][SQL]Clean up Scala reflection garbage after creating Encoder #19687
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, Project} | |
import org.apache.spark.sql.catalyst.util.ArrayData | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.UTF8String | ||
import org.apache.spark.util.ClosureCleaner | ||
|
||
case class RepeatedStruct(s: Seq[PrimitiveData]) | ||
|
||
|
@@ -114,7 +115,9 @@ object ReferenceValueClass { | |
class ExpressionEncoderSuite extends PlanTest with AnalysisTest { | ||
OuterScopes.addOuterScope(this) | ||
|
||
implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = ExpressionEncoder() | ||
implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = verifyNotLeakingReflectionObjects { | ||
ExpressionEncoder() | ||
} | ||
|
||
// test flat encoders | ||
encodeDecodeTest(false, "primitive boolean") | ||
|
@@ -370,8 +373,12 @@ class ExpressionEncoderSuite extends PlanTest with AnalysisTest { | |
private def encodeDecodeTest[T : ExpressionEncoder]( | ||
input: T, | ||
testName: String): Unit = { | ||
test(s"encode/decode for $testName: $input") { | ||
testAndVerifyNotLeakingReflectionObjects(s"encode/decode for $testName: $input") { | ||
val encoder = implicitly[ExpressionEncoder[T]] | ||
|
||
// Make sure encoder is serializable. | ||
ClosureCleaner.clean((s: String) => encoder.getClass.getName) | ||
|
||
val row = encoder.toRow(input) | ||
val schema = encoder.schema.toAttributes | ||
val boundEncoder = encoder.resolveAndBind() | ||
|
@@ -441,4 +448,28 @@ class ExpressionEncoderSuite extends PlanTest with AnalysisTest { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Verify the size of scala.reflect.runtime.JavaUniverse.undoLog before and after `func` to | ||
* ensure we don't leak Scala reflection garbage. | ||
* | ||
* @see org.apache.spark.sql.catalyst.ScalaReflection.cleanUpReflectionObjects | ||
*/ | ||
private def verifyNotLeakingReflectionObjects[T](func: => T): T = { | ||
def undoLogSize: Int = { | ||
scala.reflect.runtime.universe | ||
.asInstanceOf[scala.reflect.runtime.JavaUniverse].undoLog.log.size | ||
} | ||
|
||
val previousUndoLogSize = undoLogSize | ||
val r = func | ||
assert(previousUndoLogSize == undoLogSize) | ||
r | ||
} | ||
|
||
private def testAndVerifyNotLeakingReflectionObjects(testName: String)(testFun: => Any) { | ||
test(testName) { | ||
verifyNotLeakingReflectionObjects(testFun) | ||
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. Just out of curiosity, do we have to call |
||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
here we will verify the memory leak, seems no need to create
testAndVerifyNotLeakingReflectionObjects
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.
This is to verify that some special objects such as
UnresolvedMapObjects
don't leak. E.g., ScalaReflection creates a function here but the function runs after creating the encoder:spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala
Line 280 in 6327ea5