1717
1818#include " vec/functions/function_java_udf.h"
1919
20+ #include < bthread/bthread.h>
21+
2022#include < memory>
2123#include < string>
2224
25+ #include " common/exception.h"
2326#include " jni.h"
2427#include " runtime/user_function_cache.h"
2528#include " util/jni-util.h"
@@ -32,9 +35,22 @@ const char* EXECUTOR_EVALUATE_SIGNATURE = "(Ljava/util/Map;Ljava/util/Map;)J";
3235const char * EXECUTOR_CLOSE_SIGNATURE = " ()V" ;
3336
3437namespace doris ::vectorized {
38+ std::unique_ptr<ThreadPool> JavaFunctionCall::close_workers;
39+ std::once_flag JavaFunctionCall::close_workers_init_once;
40+
3541JavaFunctionCall::JavaFunctionCall (const TFunction& fn, const DataTypes& argument_types,
3642 const DataTypePtr& return_type)
37- : fn_(fn), _argument_types(argument_types), _return_type(return_type) {}
43+ : fn_(fn), _argument_types(argument_types), _return_type(return_type) {
44+ std::call_once (close_workers_init_once, []() {
45+ auto build_st = ThreadPoolBuilder (" UDFCloseWorkers" )
46+ .set_min_threads (4 )
47+ .set_max_threads (std::min (32 , CpuInfo::num_cores ()))
48+ .build (&close_workers);
49+ if (!build_st.ok ()) {
50+ throw doris::Exception (ErrorCode::INTERNAL_ERROR , " Failed to build UDFCloseWorkers" );
51+ }
52+ });
53+ }
3854
3955Status JavaFunctionCall::open (FunctionContext* context, FunctionContext::FunctionStateScope scope) {
4056 JNIEnv* env = nullptr ;
@@ -122,13 +138,27 @@ Status JavaFunctionCall::execute_impl(FunctionContext* context, Block& block,
122138
123139Status JavaFunctionCall::close (FunctionContext* context,
124140 FunctionContext::FunctionStateScope scope) {
125- auto * jni_ctx = reinterpret_cast <JniContext*>(
126- context->get_function_state (FunctionContext::THREAD_LOCAL ));
127- // JNIContext own some resource and its release method depend on JavaFunctionCall
128- // has to release the resource before JavaFunctionCall is deconstructed.
129- if (jni_ctx) {
130- RETURN_IF_ERROR (jni_ctx->close ());
141+ auto close_func = [context]() {
142+ auto * jni_ctx = reinterpret_cast <JniContext*>(
143+ context->get_function_state (FunctionContext::THREAD_LOCAL ));
144+ // JNIContext own some resource and its release method depend on JavaFunctionCall
145+ // has to release the resource before JavaFunctionCall is deconstructed.
146+ if (jni_ctx) {
147+ RETURN_IF_ERROR (jni_ctx->close ());
148+ }
149+ return Status::OK ();
150+ };
151+
152+ if (bthread_self () == 0 ) {
153+ return close_func ();
154+ } else {
155+ DorisMetrics::instance ()->udf_close_bthread_count ->increment (1 );
156+ // Use the close_workers pthread pool to execute the close function
157+ auto task = std::make_shared<std::packaged_task<Status ()>>(std::move (close_func));
158+ auto task_future = task->get_future ();
159+ RETURN_IF_ERROR (close_workers->submit_func ([task]() { (*task)(); }));
160+ RETURN_IF_ERROR (task_future.get ());
161+ return Status::OK ();
131162 }
132- return Status::OK ();
133163}
134164} // namespace doris::vectorized
0 commit comments