-
Notifications
You must be signed in to change notification settings - Fork 582
[GLUTEN-10797][CH] Reject all JNI calls after native destroy is invoked #10815
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| #include <Poco/Logger.h> | ||
| #include <Common/Exception.h> | ||
| #include <Common/logger_useful.h> | ||
| #include <mutex> | ||
|
|
||
| namespace DB | ||
| { | ||
|
|
@@ -46,6 +47,108 @@ jstring charTojstring(JNIEnv * env, const char * pat); | |
|
|
||
| jbyteArray stringTojbyteArray(JNIEnv * env, const std::string & str); | ||
|
|
||
| // A watcher to monitor JNIEnv status: it's active or not | ||
| class JniEnvStatusWatcher | ||
|
Contributor
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. Better add and set to private to avoid compiler autogenerate. Same as above other singleton classes. |
||
| { | ||
| public: | ||
| static JniEnvStatusWatcher & instance(); | ||
| void setActive() { is_active.store(true, std::memory_order_release); } | ||
| void setInactive() { is_active.store(false, std::memory_order_release); } | ||
| bool isActive() const { return is_active.load(std::memory_order_acquire); } | ||
| protected: | ||
| JniEnvStatusWatcher() = default; | ||
| ~JniEnvStatusWatcher() = default; | ||
| std::atomic<bool> is_active = false; | ||
| }; | ||
|
|
||
| // A counter to track ongoing JNI method calls | ||
| // It help to ensure that no JNI method is running when we are destroying the JNI resources | ||
| class JniMethodCallCounter | ||
| { | ||
| public: | ||
| static JniMethodCallCounter & instance(); | ||
| void increment() { counter.fetch_add(1, std::memory_order_acq_rel); } | ||
| void decrement() { | ||
| auto old_val = counter.fetch_sub(1, std::memory_order_acq_rel); | ||
| if (old_val == 1) | ||
| { | ||
| std::lock_guard<std::mutex> lock(mutex); | ||
| cv.notify_all(); | ||
| } | ||
| else if (old_val < 1) [[unlikely]] | ||
| { | ||
| // This should never happen | ||
| LOG_ERROR(getLogger("jni"), "JniMethodCallCounter counter underflow: {}", old_val); | ||
| } | ||
| } | ||
| // This will only be called in the destroy JNI method to wait for all ongoing JNI method calls to finish | ||
| void waitForZero() | ||
| { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| cv.wait(lock, [this] { | ||
| return counter.load(std::memory_order_acquire) <= 0; | ||
| }); | ||
| } | ||
| protected: | ||
| JniMethodCallCounter() = default; | ||
| ~JniMethodCallCounter() = default; | ||
| private: | ||
| std::atomic<int64_t> counter{0}; | ||
| std::mutex mutex; | ||
| std::condition_variable cv; | ||
| }; | ||
|
|
||
| class JniMethodGuard | ||
| { | ||
| public: | ||
| JniMethodGuard(); | ||
| ~JniMethodGuard(); | ||
| bool couldInvoke(); | ||
| }; | ||
|
|
||
| #define LOCAL_ENGINE_JNI_DESTROY_METHOD_START \ | ||
| local_engine::JniEnvStatusWatcher::instance().setInactive(); \ | ||
| local_engine::JniMethodCallCounter::instance().waitForZero(); \ | ||
| do \ | ||
| { \ | ||
| try \ | ||
| { | ||
|
|
||
| #define LOCAL_ENGINE_JNI_METHOD_START \ | ||
| local_engine::JniMethodGuard jni_method_guard; \ | ||
| do \ | ||
| { \ | ||
| try \ | ||
| { \ | ||
| if (!jni_method_guard.couldInvoke()) \ | ||
| { \ | ||
| LOG_ERROR(getLogger("jni"), "Call JNI method {} when JNIEnv is not active!", __FUNCTION__); \ | ||
| break; \ | ||
| } | ||
|
|
||
| #define LOCAL_ENGINE_JNI_METHOD_END(env, ret) \ | ||
| } \ | ||
| catch (DB::Exception & e) \ | ||
| { \ | ||
| local_engine::JniErrorsGlobalState::instance().throwException(env, e); \ | ||
| break; \ | ||
| } \ | ||
| catch (std::exception & e) \ | ||
| { \ | ||
| local_engine::JniErrorsGlobalState::instance().throwException(env, e); \ | ||
| break; \ | ||
| } \ | ||
| catch (...) \ | ||
| { \ | ||
| DB::WriteBufferFromOwnString ostr; \ | ||
| auto trace = boost::stacktrace::stacktrace(); \ | ||
| boost::stacktrace::detail::to_string(&trace.as_vector()[0], trace.size()); \ | ||
| local_engine::JniErrorsGlobalState::instance().throwRuntimeException(env, "Unknown Exception", ostr.str().c_str()); \ | ||
| break; \ | ||
| } \ | ||
| } while (0); \ | ||
| return ret; | ||
|
|
||
| #define LOCAL_ENGINE_JNI_JMETHOD_START | ||
| #define LOCAL_ENGINE_JNI_JMETHOD_END(env) \ | ||
| if ((env)->ExceptionCheck()) \ | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.