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-19644][SQL]Clean up Scala reflection garbage after creating Encoder #19687

Closed
wants to merge 2 commits into from

Conversation

zsxwing
Copy link
Member

@zsxwing zsxwing commented Nov 8, 2017

What changes were proposed in this pull request?

Because of the memory leak issue in scala.reflect.api.Types.TypeApi.<:< (scala/bug#8302), creating an encoder may leak memory.

This PR adds cleanUpReflectionObjects to clean up these leaking objects for methods calling scala.reflect.api.Types.TypeApi.<:<.

How was this patch tested?

The updated unit tests.

@zsxwing
Copy link
Member Author

zsxwing commented Nov 8, 2017

cc @cloud-fan

@SparkQA
Copy link

SparkQA commented Nov 8, 2017

Test build #83571 has finished for PR 19687 at commit c03811f.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@ManchesterUnited16
Copy link

there is something wrong in my program

@ManchesterUnited16
Copy link

java.io.NotSerializableException: scala.reflect.api.TypeTags$PredefTypeCreator

@cloud-fan
Copy link
Contributor

LGTM, is it targeted for branch 2.2 too?

*/
private def verifyNotLeakingReflectionObjects[T](func: => T): T = {
def undoLogSize: Int = {
import scala.reflect.runtime.{JavaUniverse, universe}
Copy link
Member

Choose a reason for hiding this comment

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

Just out of curiosity why import here, and for one usage in the line below?

Copy link
Member Author

Choose a reason for hiding this comment

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

No special reason. I changed to use the fully qualified class name now.


private def testAndVerifyNotLeakingReflectionObjects(testName: String)(testFun: => Any) {
test(testName) {
verifyNotLeakingReflectionObjects(testFun)
Copy link
Member

@kiszk kiszk Nov 8, 2017

Choose a reason for hiding this comment

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

Just out of curiosity, do we have to call verifyNotLeakingReflectionObjects twice? One is to call implicit. The other is here.

@@ -370,7 +372,7 @@ 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]]
Copy link
Contributor

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

Copy link
Member Author

@zsxwing zsxwing Nov 8, 2017

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:

val mapFunction: Expression => Expression = element => {

@zsxwing
Copy link
Member Author

zsxwing commented Nov 8, 2017

java.io.NotSerializableException: scala.reflect.api.TypeTags$PredefTypeCreator

@ManchesterUnited16 Probably this change may capture some unnecessary objects. Could you provide your codes to help me investigate it, or provide the full stack trace outputted by Spark?

@zsxwing
Copy link
Member Author

zsxwing commented Nov 8, 2017

@ManchesterUnited16 cleanUpReflectionObjects introduces new closures but those are created in object ScalaReflection so should be safe. Did you backport my PR to an old Spark version?
If so, you probably hit #17639.

Anyway, I added some defensive codes in the test to make sure encoders are serializable.

@zsxwing
Copy link
Member Author

zsxwing commented Nov 8, 2017

LGTM, is it targeted for branch 2.2 too?

Yeah, I will backport this to 2.2 once this PR gets merged.

@SparkQA
Copy link

SparkQA commented Nov 9, 2017

Test build #83608 has finished for PR 19687 at commit f88fb50.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@ManchesterUnited16
Copy link

ManchesterUnited16 commented Nov 9, 2017 via email

@zsxwing
Copy link
Member Author

zsxwing commented Nov 9, 2017

@ManchesterUnited16 I ran your codes and didn't see NotSerializableException. How did you patch Spark with my PR?

@ManchesterUnited16
Copy link

ManchesterUnited16 commented Nov 9, 2017 via email

@zsxwing
Copy link
Member Author

zsxwing commented Nov 9, 2017

can you show me you maven dependency when you ran the program,thank you very much!

maven dependency is used only when I compiled the codes. I used the official Spark 2.2.0 to compile the codes, and built Spark based on this PR (commit hash: f88fb50) and ran my problem using it. Could you show how did you compile the codes, how did you build Spark, and how did you run the codes?

@zsxwing
Copy link
Member Author

zsxwing commented Nov 10, 2017

@ManchesterUnited16 Since I tested your codes and it works for me, I'm going to merge this PR and backport it to 2.2. If it's a real issue, feel free to open a ticket with steps to reproduce it.

@asfgit asfgit closed this in 24ea781 Nov 10, 2017
@zsxwing zsxwing deleted the SPARK-19644 branch November 10, 2017 19:44
zsxwing added a commit to zsxwing/spark that referenced this pull request Nov 10, 2017
…ncoder

Because of the memory leak issue in `scala.reflect.api.Types.TypeApi.<:<` (scala/bug#8302), creating an encoder may leak memory.

This PR adds `cleanUpReflectionObjects` to clean up these leaking objects for methods calling `scala.reflect.api.Types.TypeApi.<:<`.

The updated unit tests.

Author: Shixiong Zhu <zsxwing@gmail.com>

Closes apache#19687 from zsxwing/SPARK-19644.
asfgit pushed a commit that referenced this pull request Nov 10, 2017
…ncoder (branch-2.2)

## What changes were proposed in this pull request?

Backport #19687 to branch-2.2. The major difference is `cleanUpReflectionObjects` is protected by `ScalaReflectionLock.synchronized` in this PR for Scala 2.10.

## How was this patch tested?

Jenkins

Author: Shixiong Zhu <zsxwing@gmail.com>

Closes #19718 from zsxwing/SPARK-19644-2.2.
@yoelb
Copy link

yoelb commented Jun 17, 2018

I have the same issue @ManchesterUnited16 , did you find a solution?

MatthewRBruce pushed a commit to Shopify/spark that referenced this pull request Jul 31, 2018
…ncoder (branch-2.2)

## What changes were proposed in this pull request?

Backport apache#19687 to branch-2.2. The major difference is `cleanUpReflectionObjects` is protected by `ScalaReflectionLock.synchronized` in this PR for Scala 2.10.

## How was this patch tested?

Jenkins

Author: Shixiong Zhu <zsxwing@gmail.com>

Closes apache#19718 from zsxwing/SPARK-19644-2.2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants