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

fix(iceberg): fix jni context class loader #17478

Merged
merged 3 commits into from
Jul 4, 2024

Conversation

chenzl25
Copy link
Contributor

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

@wenym1
Copy link
Contributor

wenym1 commented Jun 27, 2024

I am thinking that shall we instead always set the context loader after we do attach_current_thread of jni?

@chenzl25 chenzl25 added the user-facing-changes Contains changes that are visible to users label Jun 27, 2024
@chenzl25
Copy link
Contributor Author

chenzl25 commented Jun 27, 2024

I am thinking that shall we instead always set the context loader after we do attach_current_thread of jni?

It depends on whether the method needs to use ContextClassLoader. Currently, I only find debezium and iceberg catalog will use it.

@chenzl25 chenzl25 added ci/run-e2e-iceberg-sink-tests and removed user-facing-changes Contains changes that are visible to users labels Jun 27, 2024
@wenym1
Copy link
Contributor

wenym1 commented Jun 27, 2024

I am thinking that shall we instead always set the context loader after we do attach_current_thread of jni?

It depends on whether the method needs to use ContextClassLoader. Currently, I only find debezium and iceberg catalog will use it.

Java libraries usually assume that the class loader is correctly set. So I think we'd better just always set the class loader when we do attach_current_thread to create a new JniEnv, so that we don't have to manually set the class loader one by one for these methods and can avoid similar exception thrown again when we use other java libraries in the future.

@chenzl25
Copy link
Contributor Author

I am thinking that shall we instead always set the context loader after we do attach_current_thread of jni?

It depends on whether the method needs to use ContextClassLoader. Currently, I only find debezium and iceberg catalog will use it.

Java libraries usually assume that the class loader is correctly set. So I think we'd better just always set the class loader when we do attach_current_thread to create a new JniEnv, so that we don't have to manually set the class loader one by one for these methods and can avoid similar exception thrown again when we use other java libraries in the future.

Sounds good. Do you have any idea how to implement it?

@wenym1
Copy link
Contributor

wenym1 commented Jun 27, 2024

I am thinking that shall we instead always set the context loader after we do attach_current_thread of jni?

It depends on whether the method needs to use ContextClassLoader. Currently, I only find debezium and iceberg catalog will use it.

Java libraries usually assume that the class loader is correctly set. So I think we'd better just always set the class loader when we do attach_current_thread to create a new JniEnv, so that we don't have to manually set the class loader one by one for these methods and can avoid similar exception thrown again when we use other java libraries in the future.

Sounds good. Do you have any idea how to implement it?

We can unify all our call to jvm.attach_current_thread() to a method like

fn new_jvm_env(jvm: &JavaVM) -> AttachGuard<'_> {
    let env = jvm.attach_current_thread();
    let thread = call_static_method!(env, {java.lang.Thread.currentThread()});
    let system_class_loader = call_static_method!(env, {java.lang.ClassLoader.getSystemClassLoader()});
    call_method!(env, {void setContextClassLoader(), thread, {system_class_loader}});
    env
}

@chenzl25
Copy link
Contributor Author

chenzl25 commented Jul 3, 2024

@wenym1 I finished the refactoring, PTAL again.

Comment on lines +211 to +232
// set context class loader for the thread
// java.lang.Thread.currentThread()
// .setContextClassLoader(java.lang.ClassLoader.getSystemClassLoader());

let thread = crate::call_static_method!(
env,
{Thread},
{Thread currentThread()}
)?;

let system_class_loader = crate::call_static_method!(
env,
{ClassLoader},
{ClassLoader getSystemClassLoader()}
)?;

crate::call_method!(
env,
thread,
{void setContextClassLoader(ClassLoader)},
&system_class_loader
)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

java.lang.Thread.currentThread().setContextClassLoader(java.lang.ClassLoader.getSystemClassLoader()); would be called once after attaching a thread to jvm successfully and any jni related codes should be wrapped in execute_with_jni_env.

Comment on lines +796 to +798
if let Err(e) = result {
let _ = response_tx.blocking_send(Err(e));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wenym1 Do I correctly handle the error in this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@wenym1 wenym1 left a comment

Choose a reason for hiding this comment

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

All tests are passed. LGTM

Comment on lines +796 to +798
if let Err(e) = result {
let _ = response_tx.blocking_send(Err(e));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM

let mut env = jvm.get_env().with_context(|| "Failed to get jni env")?;
// set context class loader for the thread
// java.lang.Thread.currentThread()
// .setContextClassLoader(java.lang.ClassLoader.getSystemClassLoader());
Copy link
Contributor

Choose a reason for hiding this comment

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

dead code?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the equivalent java code, I think it's for reference purpose.

Copy link
Contributor

@StrikeW StrikeW left a comment

Choose a reason for hiding this comment

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

LGTM

@chenzl25 chenzl25 added this pull request to the merge queue Jul 4, 2024
Merged via the queue into main with commit 8fb5606 Jul 4, 2024
59 of 60 checks passed
@chenzl25 chenzl25 deleted the dylan/fix_context_class_loader_for_jni_catalog branch July 4, 2024 12:28
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.

panic when using Iceberg Sink connector from materialized view
3 participants