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-45392][CORE][SQL][SS] Replace Class.newInstance() with Class.getDeclaredConstructor().newInstance() #43193

Closed
wants to merge 4 commits into from

Conversation

LuciferYang
Copy link
Contributor

@LuciferYang LuciferYang commented Oct 1, 2023

What changes were proposed in this pull request?

This PR replaces Class.newInstance() with Class.getDeclaredConstructor().newInstance() to clean up the use of deprecated APIs refer to

https://github.com/openjdk/jdk/blob/dfacda488bfbe2e11e8d607a6d08527710286982/src/java.base/share/classes/java/lang/Class.java#L557-L583

Note: The new API no longer has the cachedConstructor capability that comes with the Class.newInstance(). Currently, I think there are no hotspots in the places that have been fixed. If hotspots are indeed discovered in the future, they can be optimized by adding a Loading Cache.

Why are the changes needed?

Clean up the use of deprecated APIs.

Does this PR introduce any user-facing change?

No

How was this patch tested?

Pass GitHub Actions

Was this patch authored or co-authored using generative AI tooling?

No

@LuciferYang LuciferYang marked this pull request as draft October 1, 2023 03:09
@mridulm
Copy link
Contributor

mridulm commented Oct 1, 2023

Given this is deprecated, we have to move our usage out ... but one caveat is the loss of cached constructor we can no longer leverage - something to watch out for if this is in potentially perf sensitive paths as you work through the draft.

@LuciferYang
Copy link
Contributor Author

LuciferYang commented Oct 1, 2023

Good point~ one way is to add a Guava Cache to cache these no-argument Constructors to alleviate this problem, another way is to maintain the status quo and continue to use the .newInstance() API for now.

I'm testing the addition of a Guava Cache. If the tests pass, I will update the code first.

@mridulm
Copy link
Contributor

mridulm commented Oct 1, 2023

Note - we need the cache only if we think this is in a hot path, else might not be worth it :-)

This reverts commit 00b9bd2.
@LuciferYang
Copy link
Contributor Author

Personally, I don't think they are hotpaths, let's use .getConstructor().newInstance() directly.

@LuciferYang LuciferYang changed the title Replace Class.newInstance() with Class.getDeclaredConstructor().newInstance() [SPARK-45392][CORE][SQL] Replace Class.newInstance() with Class.getDeclaredConstructor().newInstance() Oct 1, 2023
@LuciferYang LuciferYang marked this pull request as ready for review October 1, 2023 13:39
@mridulm
Copy link
Contributor

mridulm commented Oct 1, 2023

There is also use in streaming.sources test suites, can you fix those as well ? Thanks

@LuciferYang
Copy link
Contributor Author

There is also use in streaming.sources test suites, can you fix those as well ? Thanks

done ~

@LuciferYang LuciferYang changed the title [SPARK-45392][CORE][SQL] Replace Class.newInstance() with Class.getDeclaredConstructor().newInstance() [SPARK-45392][CORE][SQL][SS] Replace Class.newInstance() with Class.getDeclaredConstructor().newInstance() Oct 1, 2023
Copy link
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

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

+1, LGTM.

@dongjoon-hyun
Copy link
Member

Merged to master for Apache Spark 4.0.0.

@LuciferYang
Copy link
Contributor Author

Thanks @dongjoon-hyun @mridulm and @srowen ~

@LuciferYang LuciferYang deleted the class-newInstance branch October 18, 2023 05:24
LuciferYang pushed a commit that referenced this pull request Oct 19, 2023
… source

### What changes were proposed in this pull request?

This is a followup of #43193 to fix a behavior change. `Class.getDeclaredConstructor().newInstance()` will wrap the exception with `InvocationTargetException`, and this PR unwraps it to still throw the original exception from the data source

### Why are the changes needed?

restore a behavior change

### Does this PR introduce _any_ user-facing change?

no

### How was this patch tested?

new test

### Was this patch authored or co-authored using generative AI tooling?

no

Closes #43446 from cloud-fan/follow.

Authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants