Coverage Report

Created: 2025-11-18 23:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/jni-util.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/jni-util.h"
19
20
#include <fmt/format.h>
21
#include <glog/logging.h>
22
#include <jni.h>
23
#include <jni_md.h>
24
25
#include <cstdlib>
26
#include <filesystem>
27
#include <iterator>
28
#include <memory>
29
#include <mutex>
30
#include <sstream>
31
#include <string>
32
#include <vector>
33
34
#include "absl/strings/substitute.h"
35
#include "common/cast_set.h"
36
#include "common/config.h"
37
#include "util/doris_metrics.h"
38
#include "util/jni_native_method.h"
39
// #include "util/libjvm_loader.h"
40
41
using std::string;
42
43
namespace doris {
44
#include "common/compile_check_begin.h"
45
namespace {
46
JavaVM* g_vm;
47
[[maybe_unused]] std::once_flag g_vm_once;
48
[[maybe_unused]] std::once_flag g_jvm_conf_once;
49
50
0
const std::string GetDorisJNIDefaultClasspath() {
51
0
    const auto* doris_home = getenv("DORIS_HOME");
52
0
    DCHECK(doris_home) << "Environment variable DORIS_HOME is not set.";
53
0
54
0
    std::ostringstream out;
55
0
56
0
    auto add_jars_from_path = [&](const std::string& base_path) {
57
0
        if (!std::filesystem::exists(base_path)) {
58
0
            return;
59
0
        }
60
0
        for (const auto& entry : std::filesystem::recursive_directory_iterator(base_path)) {
61
0
            if (entry.path().extension() == ".jar") {
62
0
                if (!out.str().empty()) {
63
0
                    out << ":";
64
0
                }
65
0
                out << entry.path().string();
66
0
            }
67
0
        }
68
0
    };
69
0
70
0
    add_jars_from_path(std::string(doris_home) + "/lib");
71
0
    add_jars_from_path(std::string(doris_home) + "/custom_lib");
72
0
73
0
    // Check and add HADOOP_CONF_DIR if it's set
74
0
    const auto* hadoop_conf_dir = getenv("HADOOP_CONF_DIR");
75
0
    if (hadoop_conf_dir != nullptr && strlen(hadoop_conf_dir) > 0) {
76
0
        if (!out.str().empty()) {
77
0
            out << ":";
78
0
        }
79
0
        out << hadoop_conf_dir;
80
0
    }
81
0
82
0
    DCHECK(!out.str().empty()) << "Empty classpath is invalid.";
83
0
    return out.str();
84
0
}
85
86
0
const std::string GetDorisJNIClasspathOption() {
87
0
    const auto* classpath = getenv("DORIS_CLASSPATH");
88
0
    if (classpath) {
89
0
        return classpath;
90
0
    } else {
91
0
        return "-Djava.class.path=" + GetDorisJNIDefaultClasspath();
92
0
    }
93
0
}
94
95
0
const std::string GetKerb5ConfPath() {
96
0
    return "-Djava.security.krb5.conf=" + config::kerberos_krb5_conf_path;
97
0
}
98
99
0
[[maybe_unused]] void SetEnvIfNecessary() {
100
0
    std::string libhdfs_opts = getenv("LIBHDFS_OPTS") ? getenv("LIBHDFS_OPTS") : "";
101
0
    CHECK(libhdfs_opts != "") << "LIBHDFS_OPTS is not set";
102
0
    libhdfs_opts += fmt::format(" {} ", GetKerb5ConfPath());
103
0
    libhdfs_opts += fmt::format(" -Djdk.lang.processReaperUseDefaultStackSize={}",
104
0
                                config::jdk_process_reaper_use_default_stack_size);
105
0
    setenv("LIBHDFS_OPTS", libhdfs_opts.c_str(), 1);
106
0
    LOG(INFO) << "set final LIBHDFS_OPTS: " << libhdfs_opts;
107
0
}
108
109
// Only used on non-x86 platform
110
0
[[maybe_unused]] void FindOrCreateJavaVM() {
111
0
    int num_vms;
112
0
    int rv = JNI_GetCreatedJavaVMs(&g_vm, 1, &num_vms);
113
0
    if (rv == 0) {
114
0
        std::vector<std::string> options;
115
0
116
0
        char* java_opts = getenv("JAVA_OPTS");
117
0
        if (java_opts == nullptr) {
118
0
            options = {
119
0
                    GetDorisJNIClasspathOption(), fmt::format("-Xmx{}", "1g"),
120
0
                    fmt::format("-DlogPath={}/log/jni.log", getenv("DORIS_HOME")),
121
0
                    fmt::format("-Dsun.java.command={}", "DorisBE"), "-XX:-CriticalJNINatives",
122
0
                    fmt::format("-Djdk.lang.processReaperUseDefaultStackSize={}",
123
0
                                config::jdk_process_reaper_use_default_stack_size),
124
0
#ifdef __APPLE__
125
0
                    // On macOS, we should disable MaxFDLimit, otherwise the RLIMIT_NOFILE
126
0
                    // will be assigned the minimum of OPEN_MAX (10240) and rlim_cur (See src/hotspot/os/bsd/os_bsd.cpp)
127
0
                    // and it can not pass the check performed by storage engine.
128
0
                    // The newer JDK has fixed this issue.
129
0
                    "-XX:-MaxFDLimit"
130
0
#endif
131
0
            };
132
0
        } else {
133
0
            std::istringstream stream(java_opts);
134
0
            options = std::vector<std::string>(std::istream_iterator<std::string> {stream},
135
0
                                               std::istream_iterator<std::string>());
136
0
            options.push_back(GetDorisJNIClasspathOption());
137
0
        }
138
0
        options.push_back(GetKerb5ConfPath());
139
0
        std::unique_ptr<JavaVMOption[]> jvm_options(new JavaVMOption[options.size()]);
140
0
        for (int i = 0; i < options.size(); ++i) {
141
0
            // To convert a string to a char*, const_cast is used.
142
0
            jvm_options[i] = {const_cast<char*>(options[i].c_str()), nullptr};
143
0
        }
144
0
145
0
        JNIEnv* env = nullptr;
146
0
        JavaVMInitArgs vm_args;
147
0
        vm_args.version = JNI_VERSION_1_8;
148
0
        vm_args.options = jvm_options.get();
149
0
        vm_args.nOptions = cast_set<int>(options.size());
150
0
        // Set it to JNI_FALSE because JNI_TRUE will let JVM ignore the max size config.
151
0
        vm_args.ignoreUnrecognized = JNI_FALSE;
152
0
153
0
        jint res = JNI_CreateJavaVM(&g_vm, (void**)&env, &vm_args);
154
0
        if (JNI_OK != res) {
155
0
            DCHECK(false) << "Failed to create JVM, code= " << res;
156
0
        }
157
0
158
0
    } else {
159
0
        CHECK_EQ(rv, 0) << "Could not find any created Java VM";
160
0
        CHECK_EQ(num_vms, 1) << "No VMs returned";
161
0
    }
162
0
}
163
164
} // anonymous namespace
165
166
bool JniUtil::jvm_inited_ = false;
167
__thread JNIEnv* JniUtil::tls_env_ = nullptr;
168
jclass JniUtil::internal_exc_cl_ = nullptr;
169
jclass JniUtil::jni_util_cl_ = nullptr;
170
jclass JniUtil::jni_native_method_exc_cl_ = nullptr;
171
jmethodID JniUtil::throwable_to_string_id_ = nullptr;
172
jmethodID JniUtil::throwable_to_stack_trace_id_ = nullptr;
173
jmethodID JniUtil::get_jvm_metrics_id_ = nullptr;
174
jmethodID JniUtil::get_jvm_threads_id_ = nullptr;
175
jmethodID JniUtil::get_jmx_json_ = nullptr;
176
jobject JniUtil::jni_scanner_loader_obj_ = nullptr;
177
jmethodID JniUtil::jni_scanner_loader_method_ = nullptr;
178
jlong JniUtil::max_jvm_heap_memory_size_ = 0;
179
jmethodID JniUtil::_clean_udf_cache_method_id = nullptr;
180
181
0
Status JniUtfCharGuard::create(JNIEnv* env, jstring jstr, JniUtfCharGuard* out) {
182
0
    DCHECK(jstr != nullptr);
183
0
    DCHECK(!env->ExceptionCheck());
184
0
    jboolean is_copy;
185
0
    const char* utf_chars = env->GetStringUTFChars(jstr, &is_copy);
186
0
    bool exception_check = static_cast<bool>(env->ExceptionCheck());
187
0
    if (utf_chars == nullptr || exception_check) {
188
0
        if (exception_check) {
189
0
            env->ExceptionClear();
190
0
        }
191
0
        if (utf_chars != nullptr) {
192
0
            env->ReleaseStringUTFChars(jstr, utf_chars);
193
0
        }
194
0
        const auto* fail_message = "GetStringUTFChars failed. Probable OOM on JVM side";
195
0
        LOG(WARNING) << fail_message;
196
0
        return Status::JniError(fail_message);
197
0
    }
198
0
    out->env = env;
199
0
    out->jstr = jstr;
200
0
    out->utf_chars = utf_chars;
201
0
    return Status::OK();
202
0
}
203
204
0
Status JniLocalFrame::push(JNIEnv* env, int max_local_ref) {
205
0
    DCHECK(env_ == nullptr);
206
0
    DCHECK_GT(max_local_ref, 0);
207
0
    if (env->PushLocalFrame(max_local_ref) < 0) {
208
0
        env->ExceptionClear();
209
0
        return Status::JniError("failed to push frame");
210
0
    }
211
0
    env_ = env;
212
0
    return Status::OK();
213
0
}
214
215
0
void JniUtil::parse_max_heap_memory_size_from_jvm(JNIEnv* env) {
216
    // The start_be.sh would set JAVA_OPTS inside LIBHDFS_OPTS
217
0
    std::string java_opts = getenv("LIBHDFS_OPTS") ? getenv("LIBHDFS_OPTS") : "";
218
0
    std::istringstream iss(java_opts);
219
0
    std::string opt;
220
0
    while (iss >> opt) {
221
0
        if (opt.find("-Xmx") == 0) {
222
0
            std::string xmxValue = opt.substr(4);
223
0
            LOG(INFO) << "The max heap vaule is " << xmxValue;
224
0
            char unit = xmxValue.back();
225
0
            xmxValue.pop_back();
226
0
            long long value = std::stoll(xmxValue);
227
0
            switch (unit) {
228
0
            case 'g':
229
0
            case 'G':
230
0
                max_jvm_heap_memory_size_ = value * 1024 * 1024 * 1024;
231
0
                break;
232
0
            case 'm':
233
0
            case 'M':
234
0
                max_jvm_heap_memory_size_ = value * 1024 * 1024;
235
0
                break;
236
0
            case 'k':
237
0
            case 'K':
238
0
                max_jvm_heap_memory_size_ = value * 1024;
239
0
                break;
240
0
            default:
241
0
                max_jvm_heap_memory_size_ = value;
242
0
                break;
243
0
            }
244
0
        }
245
0
    }
246
0
    if (0 == max_jvm_heap_memory_size_) {
247
0
        LOG(FATAL) << "the max_jvm_heap_memory_size_ is " << max_jvm_heap_memory_size_;
248
0
    }
249
0
    LOG(INFO) << "the max_jvm_heap_memory_size_ is " << max_jvm_heap_memory_size_;
250
0
}
251
252
0
size_t JniUtil::get_max_jni_heap_memory_size() {
253
0
#if defined(USE_LIBHDFS3) || defined(BE_TEST)
254
0
    return std::numeric_limits<size_t>::max();
255
#else
256
    static std::once_flag parse_max_heap_memory_size_from_jvm_flag;
257
    std::call_once(parse_max_heap_memory_size_from_jvm_flag, parse_max_heap_memory_size_from_jvm,
258
                   tls_env_);
259
    return max_jvm_heap_memory_size_;
260
#endif
261
0
}
262
263
0
Status JniUtil::GetJNIEnvSlowPath(JNIEnv** env) {
264
0
    DCHECK(!tls_env_) << "Call GetJNIEnv() fast path";
265
266
#ifdef USE_LIBHDFS3
267
    std::call_once(g_vm_once, FindOrCreateJavaVM);
268
    int rc = g_vm->GetEnv(reinterpret_cast<void**>(&tls_env_), JNI_VERSION_1_8);
269
    if (rc == JNI_EDETACHED) {
270
        rc = g_vm->AttachCurrentThread((void**)&tls_env_, nullptr);
271
    }
272
    if (rc != 0 || tls_env_ == nullptr) {
273
        return Status::JniError("Unable to get JVM: {}", rc);
274
    }
275
#else
276
    // the hadoop libhdfs will do all the stuff
277
0
    std::call_once(g_jvm_conf_once, SetEnvIfNecessary);
278
0
    tls_env_ = getJNIEnv();
279
0
#endif
280
0
    *env = tls_env_;
281
0
    return Status::OK();
282
0
}
283
284
0
Status JniUtil::GetJniExceptionMsg(JNIEnv* env, bool log_stack, const string& prefix) {
285
0
    jthrowable exc = env->ExceptionOccurred();
286
0
    if (exc == nullptr) {
287
0
        return Status::OK();
288
0
    }
289
0
    env->ExceptionClear();
290
0
    DCHECK(throwable_to_string_id() != nullptr);
291
0
    const char* oom_msg_template =
292
0
            "$0 threw an unchecked exception. The JVM is likely out "
293
0
            "of memory (OOM).";
294
0
    jstring msg = static_cast<jstring>(
295
0
            env->CallStaticObjectMethod(jni_util_class(), throwable_to_string_id(), exc));
296
0
    if (env->ExceptionOccurred()) {
297
0
        env->ExceptionClear();
298
0
        string oom_msg = absl::Substitute(oom_msg_template, "throwableToString");
299
0
        LOG(WARNING) << oom_msg;
300
0
        return Status::JniError(oom_msg);
301
0
    }
302
0
    JniUtfCharGuard msg_str_guard;
303
0
    RETURN_IF_ERROR(JniUtfCharGuard::create(env, msg, &msg_str_guard));
304
0
    if (log_stack) {
305
0
        jstring stack = static_cast<jstring>(
306
0
                env->CallStaticObjectMethod(jni_util_class(), throwable_to_stack_trace_id(), exc));
307
0
        if (env->ExceptionOccurred()) {
308
0
            env->ExceptionClear();
309
0
            string oom_msg = absl::Substitute(oom_msg_template, "throwableToStackTrace");
310
0
            LOG(WARNING) << oom_msg;
311
0
            return Status::JniError(oom_msg);
312
0
        }
313
0
        JniUtfCharGuard c_stack_guard;
314
0
        RETURN_IF_ERROR(JniUtfCharGuard::create(env, stack, &c_stack_guard));
315
0
        LOG(WARNING) << c_stack_guard.get();
316
0
    }
317
318
0
    env->DeleteLocalRef(exc);
319
0
    return Status::JniError("{}{}", prefix, msg_str_guard.get());
320
0
}
321
322
Status JniUtil::convert_to_java_map(JNIEnv* env, const std::map<std::string, std::string>& map,
323
0
                                    jobject* hashmap_object) {
324
0
    jclass hashmap_class = env->FindClass("java/util/HashMap");
325
0
    RETURN_ERROR_IF_EXC(env);
326
0
    jmethodID hashmap_constructor = env->GetMethodID(hashmap_class, "<init>", "(I)V");
327
0
    RETURN_ERROR_IF_EXC(env);
328
0
    jobject hashmap_local_object = env->NewObject(hashmap_class, hashmap_constructor, map.size());
329
0
    RETURN_ERROR_IF_EXC(env);
330
0
    jmethodID hashmap_put = env->GetMethodID(
331
0
            hashmap_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
332
0
    RETURN_ERROR_IF_EXC(env);
333
0
    for (const auto& it : map) {
334
0
        jstring key = env->NewStringUTF(it.first.c_str());
335
0
        jstring value = env->NewStringUTF(it.second.c_str());
336
0
        env->CallObjectMethod(hashmap_local_object, hashmap_put, key, value);
337
0
        RETURN_ERROR_IF_EXC(env);
338
0
        env->DeleteLocalRef(key);
339
0
        env->DeleteLocalRef(value);
340
0
    }
341
0
    env->DeleteLocalRef(hashmap_class);
342
0
    RETURN_IF_ERROR(LocalToGlobalRef(env, hashmap_local_object, hashmap_object));
343
0
    env->DeleteLocalRef(hashmap_local_object);
344
0
    return Status::OK();
345
0
}
346
347
Status JniUtil::convert_to_cpp_map(JNIEnv* env, jobject map,
348
0
                                   std::map<std::string, std::string>* resultMap) {
349
    // Get the class and method ID of the java.util.Map interface
350
0
    jclass mapClass = env->FindClass("java/util/Map");
351
0
    RETURN_ERROR_IF_EXC(env);
352
0
    jmethodID entrySetMethod = env->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;");
353
354
    // Get the class and method ID of the java.util.Set interface
355
0
    jclass setClass = env->FindClass("java/util/Set");
356
0
    RETURN_ERROR_IF_EXC(env);
357
0
    jmethodID iteratorSetMethod = env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;");
358
359
    // Get the class and method ID of the java.util.Iterator interface
360
0
    jclass iteratorClass = env->FindClass("java/util/Iterator");
361
0
    RETURN_ERROR_IF_EXC(env);
362
0
    jmethodID hasNextMethod = env->GetMethodID(iteratorClass, "hasNext", "()Z");
363
0
    jmethodID nextMethod = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
364
365
    // Get the class and method ID of the java.util.Map.Entry interface
366
0
    jclass entryClass = env->FindClass("java/util/Map$Entry");
367
0
    RETURN_ERROR_IF_EXC(env);
368
0
    jmethodID getKeyMethod = env->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;");
369
0
    jmethodID getValueMethod = env->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;");
370
371
    // Call the entrySet method to get the set of key-value pairs
372
0
    jobject entrySet = env->CallObjectMethod(map, entrySetMethod);
373
0
    RETURN_ERROR_IF_EXC(env);
374
375
    // Call the iterator method on the set to iterate over the key-value pairs
376
0
    jobject iteratorSet = env->CallObjectMethod(entrySet, iteratorSetMethod);
377
0
    RETURN_ERROR_IF_EXC(env);
378
379
    // Iterate over the key-value pairs
380
0
    while (env->CallBooleanMethod(iteratorSet, hasNextMethod)) {
381
0
        RETURN_ERROR_IF_EXC(env);
382
383
        // Get the current entry
384
0
        jobject entry = env->CallObjectMethod(iteratorSet, nextMethod);
385
0
        RETURN_ERROR_IF_EXC(env);
386
387
        // Get the key and value from the entry
388
0
        jobject javaKey = env->CallObjectMethod(entry, getKeyMethod);
389
0
        RETURN_ERROR_IF_EXC(env);
390
391
0
        jobject javaValue = env->CallObjectMethod(entry, getValueMethod);
392
0
        RETURN_ERROR_IF_EXC(env);
393
394
        // Convert the key and value to C++ strings
395
0
        const char* key = env->GetStringUTFChars(static_cast<jstring>(javaKey), nullptr);
396
0
        const char* value = env->GetStringUTFChars(static_cast<jstring>(javaValue), nullptr);
397
398
        // Store the key-value pair in the map
399
0
        (*resultMap)[key] = value;
400
401
        // Release the string references
402
0
        env->ReleaseStringUTFChars(static_cast<jstring>(javaKey), key);
403
0
        env->ReleaseStringUTFChars(static_cast<jstring>(javaValue), value);
404
405
        // Delete local references
406
0
        env->DeleteLocalRef(entry);
407
0
        env->DeleteLocalRef(javaKey);
408
0
        env->DeleteLocalRef(javaValue);
409
0
    }
410
411
    // Delete local references
412
0
    env->DeleteLocalRef(iteratorSet);
413
0
    env->DeleteLocalRef(entrySet);
414
0
    env->DeleteLocalRef(mapClass);
415
0
    env->DeleteLocalRef(setClass);
416
0
    env->DeleteLocalRef(iteratorClass);
417
0
    env->DeleteLocalRef(entryClass);
418
419
0
    return Status::OK();
420
0
}
421
422
0
Status JniUtil::GetGlobalClassRef(JNIEnv* env, const char* class_str, jclass* class_ref) {
423
0
    *class_ref = nullptr;
424
0
    JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF(jclass, local_cl, env, FindClass(class_str));
425
0
    RETURN_IF_ERROR(LocalToGlobalRef(env, local_cl, reinterpret_cast<jobject*>(class_ref)));
426
0
    return Status::OK();
427
0
}
428
429
0
Status JniUtil::LocalToGlobalRef(JNIEnv* env, jobject local_ref, jobject* global_ref) {
430
0
    *global_ref = env->NewGlobalRef(local_ref);
431
    // NewGlobalRef:
432
    // Returns a global reference to the given obj.
433
    //
434
    //May return NULL if:
435
    //  obj refers to null
436
    //  the system has run out of memory
437
    //  obj was a weak global reference and has already been garbage collected
438
0
    if (*global_ref == nullptr) {
439
0
        return Status::JniError(
440
0
                "LocalToGlobalRef fail,global ref is NULL,maybe the system has run out of memory.");
441
0
    }
442
443
    //NewGlobalRef not throw exception,maybe we just need check NULL.
444
0
    RETURN_ERROR_IF_EXC(env);
445
0
    return Status::OK();
446
0
}
447
448
0
Status JniUtil::init_jni_scanner_loader(JNIEnv* env) {
449
    // Get scanner loader;
450
0
    jclass jni_scanner_loader_cls;
451
0
    std::string jni_scanner_loader_str = "org/apache/doris/common/classloader/ScannerLoader";
452
0
    RETURN_IF_ERROR(JniUtil::GetGlobalClassRef(env, jni_scanner_loader_str.c_str(),
453
0
                                               &jni_scanner_loader_cls));
454
0
    jmethodID jni_scanner_loader_constructor =
455
0
            env->GetMethodID(jni_scanner_loader_cls, "<init>", "()V");
456
0
    RETURN_ERROR_IF_EXC(env);
457
0
    jni_scanner_loader_method_ = env->GetMethodID(jni_scanner_loader_cls, "getLoadedClass",
458
0
                                                  "(Ljava/lang/String;)Ljava/lang/Class;");
459
0
    if (jni_scanner_loader_method_ == nullptr) {
460
0
        if (env->ExceptionOccurred()) {
461
0
            env->ExceptionDescribe();
462
0
        }
463
0
        return Status::JniError("Failed to find ScannerLoader.getLoadedClass method.");
464
0
    }
465
0
    RETURN_ERROR_IF_EXC(env);
466
0
    jmethodID load_jni_scanner =
467
0
            env->GetMethodID(jni_scanner_loader_cls, "loadAllScannerJars", "()V");
468
0
    RETURN_ERROR_IF_EXC(env);
469
470
0
    jobject jni_scanner_loader_local_obj =
471
0
            env->NewObject(jni_scanner_loader_cls, jni_scanner_loader_constructor);
472
0
    jni_scanner_loader_obj_ = env->NewGlobalRef(jni_scanner_loader_local_obj);
473
0
    RETURN_ERROR_IF_EXC(env);
474
0
    env->DeleteLocalRef(jni_scanner_loader_local_obj);
475
0
    RETURN_ERROR_IF_EXC(env);
476
0
    env->CallVoidMethod(jni_scanner_loader_obj_, load_jni_scanner);
477
0
    RETURN_ERROR_IF_EXC(env);
478
479
0
    _clean_udf_cache_method_id = env->GetMethodID(jni_scanner_loader_cls, "cleanUdfClassLoader",
480
0
                                                  "(Ljava/lang/String;)V");
481
0
    if (_clean_udf_cache_method_id == nullptr) {
482
0
        if (env->ExceptionOccurred()) {
483
0
            env->ExceptionDescribe();
484
0
        }
485
0
        return Status::JniError("Failed to find removeUdfClassLoader method.");
486
0
    }
487
0
    RETURN_ERROR_IF_EXC(env);
488
0
    return Status::OK();
489
0
}
490
491
0
Status JniUtil::clean_udf_class_load_cache(const std::string& function_signature) {
492
0
    JNIEnv* env = nullptr;
493
0
    RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env));
494
0
    jstring function_signature_jstr = env->NewStringUTF(function_signature.c_str());
495
0
    RETURN_ERROR_IF_EXC(env);
496
0
    env->CallVoidMethod(jni_scanner_loader_obj_, _clean_udf_cache_method_id,
497
0
                        function_signature_jstr);
498
0
    RETURN_ERROR_IF_EXC(env);
499
0
    env->DeleteLocalRef(function_signature_jstr);
500
0
    RETURN_ERROR_IF_EXC(env);
501
0
    return Status::OK();
502
0
}
503
504
Status JniUtil::get_jni_scanner_class(JNIEnv* env, const char* classname,
505
0
                                      jclass* jni_scanner_class) {
506
    // Get JNI scanner class by class name;
507
0
    jstring class_name_str = env->NewStringUTF(classname);
508
0
    RETURN_ERROR_IF_EXC(env);
509
510
0
    jobject loaded_class_obj = env->CallObjectMethod(jni_scanner_loader_obj_,
511
0
                                                     jni_scanner_loader_method_, class_name_str);
512
0
    RETURN_ERROR_IF_EXC(env);
513
514
0
    *jni_scanner_class = reinterpret_cast<jclass>(env->NewGlobalRef(loaded_class_obj));
515
0
    RETURN_ERROR_IF_EXC(env);
516
517
0
    env->DeleteLocalRef(loaded_class_obj);
518
0
    RETURN_ERROR_IF_EXC(env);
519
0
    env->DeleteLocalRef(class_name_str);
520
0
    RETURN_ERROR_IF_EXC(env);
521
0
    return Status::OK();
522
0
}
523
524
0
Status JniUtil::Init() {
525
    // RETURN_IF_ERROR(LibJVMLoader::instance().load());
526
527
    // Get the JNIEnv* corresponding to current thread.
528
0
    JNIEnv* env = nullptr;
529
0
    RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env));
530
531
0
    if (env == nullptr) {
532
0
        return Status::JniError("Failed to get/create JVM");
533
0
    }
534
    // Find JniUtil class and create a global ref.
535
0
    jclass local_jni_util_cl = env->FindClass("org/apache/doris/common/jni/utils/JniUtil");
536
0
    if (local_jni_util_cl == nullptr) {
537
0
        if (env->ExceptionOccurred()) {
538
0
            env->ExceptionDescribe();
539
0
        }
540
0
        return Status::JniError("Failed to find JniUtil class.");
541
0
    }
542
0
    jni_util_cl_ = reinterpret_cast<jclass>(env->NewGlobalRef(local_jni_util_cl));
543
0
    if (jni_util_cl_ == nullptr) {
544
0
        if (env->ExceptionOccurred()) {
545
0
            env->ExceptionDescribe();
546
0
        }
547
0
        return Status::JniError("Failed to create global reference to JniUtil class.");
548
0
    }
549
0
    env->DeleteLocalRef(local_jni_util_cl);
550
0
    if (env->ExceptionOccurred()) {
551
0
        return Status::JniError("Failed to delete local reference to JniUtil class.");
552
0
    }
553
554
    // Find InternalException class and create a global ref.
555
0
    jclass local_internal_exc_cl =
556
0
            env->FindClass("org/apache/doris/common/exception/InternalException");
557
0
    if (local_internal_exc_cl == nullptr) {
558
0
        if (env->ExceptionOccurred()) {
559
0
            env->ExceptionDescribe();
560
0
        }
561
0
        return Status::JniError("Failed to find JniUtil class.");
562
0
    }
563
0
    internal_exc_cl_ = reinterpret_cast<jclass>(env->NewGlobalRef(local_internal_exc_cl));
564
0
    if (internal_exc_cl_ == nullptr) {
565
0
        if (env->ExceptionOccurred()) {
566
0
            env->ExceptionDescribe();
567
0
        }
568
0
        return Status::JniError("Failed to create global reference to JniUtil class.");
569
0
    }
570
0
    env->DeleteLocalRef(local_internal_exc_cl);
571
0
    if (env->ExceptionOccurred()) {
572
0
        return Status::JniError("Failed to delete local reference to JniUtil class.");
573
0
    }
574
575
    // Find JNINativeMethod class and create a global ref.
576
0
    jclass local_jni_native_exc_cl =
577
0
            env->FindClass("org/apache/doris/common/jni/utils/JNINativeMethod");
578
0
    if (local_jni_native_exc_cl == nullptr) {
579
0
        if (env->ExceptionOccurred()) {
580
0
            env->ExceptionDescribe();
581
0
        }
582
0
        return Status::JniError("Failed to find JNINativeMethod class.");
583
0
    }
584
0
    jni_native_method_exc_cl_ =
585
0
            reinterpret_cast<jclass>(env->NewGlobalRef(local_jni_native_exc_cl));
586
0
    if (jni_native_method_exc_cl_ == nullptr) {
587
0
        if (env->ExceptionOccurred()) {
588
0
            env->ExceptionDescribe();
589
0
        }
590
0
        return Status::JniError("Failed to create global reference to JNINativeMethod class.");
591
0
    }
592
0
    env->DeleteLocalRef(local_jni_native_exc_cl);
593
0
    if (env->ExceptionOccurred()) {
594
0
        return Status::JniError("Failed to delete local reference to JNINativeMethod class.");
595
0
    }
596
597
0
    static char memory_alloc_name[] = "memoryTrackerMalloc";
598
0
    static char memory_alloc_sign[] = "(J)J";
599
0
    static char memory_free_name[] = "memoryTrackerFree";
600
0
    static char memory_free_sign[] = "(J)V";
601
0
    static char memory_alloc_batch_name[] = "memoryTrackerMallocBatch";
602
0
    static char memory_alloc_batch_sign[] = "([I)[J";
603
0
    static char memory_free_batch_name[] = "memoryTrackerFreeBatch";
604
0
    static char memory_free_batch_sign[] = "([J)V";
605
0
    static JNINativeMethod java_native_methods[] = {
606
0
            {memory_alloc_name, memory_alloc_sign, (void*)&JavaNativeMethods::memoryMalloc},
607
0
            {memory_free_name, memory_free_sign, (void*)&JavaNativeMethods::memoryFree},
608
0
            {memory_alloc_batch_name, memory_alloc_batch_sign,
609
0
             (void*)&JavaNativeMethods::memoryMallocBatch},
610
0
            {memory_free_batch_name, memory_free_batch_sign,
611
0
             (void*)&JavaNativeMethods::memoryFreeBatch},
612
0
    };
613
614
0
    int res = env->RegisterNatives(jni_native_method_exc_cl_, java_native_methods,
615
0
                                   sizeof(java_native_methods) / sizeof(java_native_methods[0]));
616
0
    DCHECK_EQ(res, 0);
617
618
    // Throwable toString()
619
0
    throwable_to_string_id_ = env->GetStaticMethodID(jni_util_cl_, "throwableToString",
620
0
                                                     "(Ljava/lang/Throwable;)Ljava/lang/String;");
621
0
    if (throwable_to_string_id_ == nullptr) {
622
0
        if (env->ExceptionOccurred()) {
623
0
            env->ExceptionDescribe();
624
0
        }
625
0
        return Status::JniError("Failed to find JniUtil.throwableToString method.");
626
0
    }
627
628
    // throwableToStackTrace()
629
0
    throwable_to_stack_trace_id_ = env->GetStaticMethodID(
630
0
            jni_util_cl_, "throwableToStackTrace", "(Ljava/lang/Throwable;)Ljava/lang/String;");
631
0
    if (throwable_to_stack_trace_id_ == nullptr) {
632
0
        if (env->ExceptionOccurred()) {
633
0
            env->ExceptionDescribe();
634
0
        }
635
0
        return Status::JniError("Failed to find JniUtil.throwableToFullStackTrace method.");
636
0
    }
637
638
0
    get_jvm_metrics_id_ = env->GetStaticMethodID(jni_util_cl_, "getJvmMemoryMetrics", "()[B");
639
0
    if (get_jvm_metrics_id_ == nullptr) {
640
0
        if (env->ExceptionOccurred()) {
641
0
            env->ExceptionDescribe();
642
0
        }
643
0
        return Status::JniError("Failed to find JniUtil.getJvmMemoryMetrics method.");
644
0
    }
645
646
0
    get_jvm_threads_id_ = env->GetStaticMethodID(jni_util_cl_, "getJvmThreadsInfo", "([B)[B");
647
0
    if (get_jvm_threads_id_ == nullptr) {
648
0
        if (env->ExceptionOccurred()) {
649
0
            env->ExceptionDescribe();
650
0
        }
651
0
        return Status::JniError("Failed to find JniUtil.getJvmThreadsInfo method.");
652
0
    }
653
654
0
    get_jmx_json_ = env->GetStaticMethodID(jni_util_cl_, "getJMXJson", "()[B");
655
0
    if (get_jmx_json_ == nullptr) {
656
0
        if (env->ExceptionOccurred()) {
657
0
            env->ExceptionDescribe();
658
0
        }
659
0
        return Status::JniError("Failed to find JniUtil.getJMXJson method.");
660
0
    }
661
0
    RETURN_IF_ERROR(init_jni_scanner_loader(env));
662
0
    jvm_inited_ = true;
663
0
    DorisMetrics::instance()->init_jvm_metrics(env);
664
0
    return Status::OK();
665
0
}
666
#include "common/compile_check_end.h"
667
} // namespace doris