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-42539][SQL][HIVE] Eliminate separate classloader when using 'builtin' Hive version for metadata client #40224

Conversation

xkrogen
Copy link
Contributor

@xkrogen xkrogen commented Feb 28, 2023

What changes were proposed in this pull request?

When using the 'builtin' Hive version for the Hive metadata client, do not create a separate classloader, and rather continue to use the overall user/application classloader (regardless of Java version). This standardizes the behavior for all Java versions with that of Java 9+. See SPARK-42539 for more details on why this approach was chosen.

Please note that this is a re-submit of #40144. That one introduced test failures, and potentially a real issue, because the PR works by setting isolationOn = false for builtin mode. In addition to adjusting the classloader, HiveClientImpl relies on isolationOn to determine if it should use an isolated copy of SessionState, so the PR inadvertently switched to using a shared SessionState object. I think we do want to continue to have the isolated session state even in builtin mode, so this adds a new flag sessionStateIsolationOn which controls whether the session state should be isolated, separately from the isolationOn flag which controls whether the classloader should be isolated. Default behavior is for sessionStateIsolationOn to be set equal to isolationOn, but for builtin mode, we override it to enable session state isolated even though classloader isolation is turned off.

Why are the changes needed?

Please see a much more detailed description in SPARK-42539. The tl;dr is that user-provided JARs (such as hive-exec-2.3.8.jar) take precedence over Spark/system JARs when constructing the classloader used by IsolatedClientLoader on Java 8 in 'builtin' mode, which can cause unexpected behavior and/or breakages. This violates the expectation that, unless user-first classloader mode is used, Spark JARs should be prioritized over user JARs. It also seems that this separate classloader was unnecessary from the start, since the intent of 'builtin' mode is to use the JARs already existing on the regular classloader (as alluded to here). The isolated clientloader was originally added in #5876 in 2015. This bit in the PR description is the only mention of the behavior for "builtin":

attempt to discover the jars that were used to load Spark SQL and use those. This option is only valid when using the execution version of Hive.

I can't follow the logic here; the user classloader clearly has all of the necessary Hive JARs, since that's where we're getting the JAR URLs from, so we could just use that directly instead of grabbing the URLs. When this was initially added, it only used the JARs from the user classloader, not any of its parents, which I suspect was the motivating factor (to try to avoid more Spark classes being duplicated inside of the isolated classloader, I guess). But that was changed a month later anyway in #6435 / #6459, so I think this may have basically been deadcode from the start. It has also caused at least one issue over the years, e.g. SPARK-21428, which disables the new-classloader behavior in the case of running inside of a CLI session.

Does this PR introduce any user-facing change?

No, except to protect Spark itself from potentially being broken by bad user JARs.

How was this patch tested?

This includes a new unit test in HiveUtilsSuite which demonstrates the issue and shows that this approach resolves it. It has also been tested on a live cluster running Java 8 and Hive communication functionality continues to work as expected.

Unit tests failing in #40144 have been locally tested (HiveUtilsSuite, HiveSharedStateSuite, HiveCliSessionStateSuite, JsonHadoopFsRelationSuite).

…uiltin' Hive version for metadata client

When using the 'builtin' Hive version for the Hive metadata client, do not create a separate classloader, and rather continue to use the overall user/application classloader (regardless of Java version). This standardizes the behavior for all Java versions with that of Java 9+. See SPARK-42539 for more details on why this approach was chosen.

Please see a much more detailed description in SPARK-42539. The tl;dr is that user-provided JARs (such as `hive-exec-2.3.8.jar`) take precedence over Spark/system JARs when constructing the classloader used by `IsolatedClientLoader` on Java 8 in 'builtin' mode, which can cause unexpected behavior and/or breakages. This violates the expectation that, unless user-first classloader mode is used, Spark JARs should be prioritized over user JARs. It also seems that this separate classloader was unnecessary from the start, since the intent of 'builtin' mode is to use the JARs already existing on the regular classloader (as alluded to [here](apache#24057 (comment))). The isolated clientloader was originally added in apache#5876 in 2015. This bit in the PR description is the only mention of the behavior for "builtin":
> attempt to discover the jars that were used to load Spark SQL and use those. This option is only valid when using the execution version of Hive.

I can't follow the logic here; the user classloader clearly has all of the necessary Hive JARs, since that's where we're getting the JAR URLs from, so we could just use that directly instead of grabbing the URLs. When this was initially added, it only used the JARs from the user classloader, not any of its parents, which I suspect was the motivating factor (to try to avoid more Spark classes being duplicated inside of the isolated classloader, I guess). But that was changed a month later anyway in apache#6435 / apache#6459, so I think this may have basically been deadcode from the start. It has also caused at least one issue over the years, e.g. SPARK-21428, which disables the new-classloader behavior in the case of running inside of a CLI session.

No, except to protect Spark itself from potentially being broken by bad user JARs.

This includes a new unit test in `HiveUtilsSuite` which demonstrates the issue and shows that this approach resolves it. It has also been tested on a live cluster running Java 8 and Hive communication functionality continues to work as expected.
…onOn (for classpath isolation) vs sessionStateIsolationOn (for SessionState isolation)
@xkrogen
Copy link
Contributor Author

xkrogen commented Feb 28, 2023

cc @sunchao @cloud-fan @dongjoon-hyun

Note that this commit represents the delta from #40144

Copy link
Member

@sunchao sunchao left a comment

Choose a reason for hiding this comment

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

LGTM

@sunchao sunchao closed this in 2e34427 Mar 1, 2023
@sunchao
Copy link
Member

sunchao commented Mar 1, 2023

Merged to master, thanks @xkrogen !

@xkrogen xkrogen deleted the xkrogen/SPARK-42539/hive-isolatedclientloader-builtin-user-jar-conflict-fix/take2 branch March 1, 2023 22:29
@dongjoon-hyun
Copy link
Member

Thank you, @xkrogen and @sunchao !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants