Coverage Report

Created: 2025-09-10 18:13

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
            jvm_options[i] = {const_cast<char*>(options[i].c_str()), nullptr};
142
0
        }
143
0
144
0
        JNIEnv* env = nullptr;
145
0
        JavaVMInitArgs vm_args;
146
0
        vm_args.version = JNI_VERSION_1_8;
147
0
        vm_args.options = jvm_options.get();
148
0
        vm_args.nOptions = cast_set<int>(options.size());
149
0
        // Set it to JNI_FALSE because JNI_TRUE will let JVM ignore the max size config.
150
0
        vm_args.ignoreUnrecognized = JNI_FALSE;
151
0
152
0
        jint res = JNI_CreateJavaVM(&g_vm, (void**)&env, &vm_args);
153
0
        if (JNI_OK != res) {
154
0
            DCHECK(false) << "Failed to create JVM, code= " << res;
155
0
        }
156
0
157
0
    } else {
158
0
        CHECK_EQ(rv, 0) << "Could not find any created Java VM";
159
0
        CHECK_EQ(num_vms, 1) << "No VMs returned";
160
0
    }
161
0
}
162
163
} // anonymous namespace
164
165
bool JniUtil::jvm_inited_ = false;
166
__thread JNIEnv* JniUtil::tls_env_ = nullptr;
167
jclass JniUtil::internal_exc_cl_ = nullptr;
168
jclass JniUtil::jni_util_cl_ = nullptr;
169
jclass JniUtil::jni_native_method_exc_cl_ = nullptr;
170
jmethodID JniUtil::throwable_to_string_id_ = nullptr;
171
jmethodID JniUtil::throwable_to_stack_trace_id_ = nullptr;
172
jmethodID JniUtil::get_jvm_metrics_id_ = nullptr;
173
jmethodID JniUtil::get_jvm_threads_id_ = nullptr;
174
jmethodID JniUtil::get_jmx_json_ = nullptr;
175
jobject JniUtil::jni_scanner_loader_obj_ = nullptr;
176
jmethodID JniUtil::jni_scanner_loader_method_ = nullptr;
177
jlong JniUtil::max_jvm_heap_memory_size_ = 0;
178
jmethodID JniUtil::_clean_udf_cache_method_id = nullptr;
179
180
0
Status JniUtfCharGuard::create(JNIEnv* env, jstring jstr, JniUtfCharGuard* out) {
181
0
    DCHECK(jstr != nullptr);
182
0
    DCHECK(!env->ExceptionCheck());
183
0
    jboolean is_copy;
184
0
    const char* utf_chars = env->GetStringUTFChars(jstr, &is_copy);
185
0
    bool exception_check = static_cast<bool>(env->ExceptionCheck());
186
0
    if (utf_chars == nullptr || exception_check) {
187
0
        if (exception_check) {
188
0
            env->ExceptionClear();
189
0
        }
190
0
        if (utf_chars != nullptr) {
191
0
            env->ReleaseStringUTFChars(jstr, utf_chars);
192
0
        }
193
0
        const auto* fail_message = "GetStringUTFChars failed. Probable OOM on JVM side";
194
0
        LOG(WARNING) << fail_message;
195
0
        return Status::JniError(fail_message);
196
0
    }
197
0
    out->env = env;
198
0
    out->jstr = jstr;
199
0
    out->utf_chars = utf_chars;
200
0
    return Status::OK();
201
0
}
202
203
0
Status JniLocalFrame::push(JNIEnv* env, int max_local_ref) {
204
0
    DCHECK(env_ == nullptr);
205
0
    DCHECK_GT(max_local_ref, 0);
206
0
    if (env->PushLocalFrame(max_local_ref) < 0) {
207
0
        env->ExceptionClear();
208
0
        return Status::JniError("failed to push frame");
209
0
    }
210
0
    env_ = env;
211
0
    return Status::OK();
212
0
}
213
214
0
void JniUtil::parse_max_heap_memory_size_from_jvm(JNIEnv* env) {
215
    // The start_be.sh would set JAVA_OPTS inside LIBHDFS_OPTS
216
0
    std::string java_opts = getenv("LIBHDFS_OPTS") ? getenv("LIBHDFS_OPTS") : "";
217
0
    std::istringstream iss(java_opts);
218
0
    std::string opt;
219
0
    while (iss >> opt) {
220
0
        if (opt.find("-Xmx") == 0) {
221
0
            std::string xmxValue = opt.substr(4);
222
0
            LOG(INFO) << "The max heap vaule is " << xmxValue;
223
0
            char unit = xmxValue.back();
224
0
            xmxValue.pop_back();
225
0
            long long value = std::stoll(xmxValue);
226
0
            switch (unit) {
227
0
            case 'g':
228
0
            case 'G':
229
0
                max_jvm_heap_memory_size_ = value * 1024 * 1024 * 1024;
230
0
                break;
231
0
            case 'm':
232
0
            case 'M':
233
0
                max_jvm_heap_memory_size_ = value * 1024 * 1024;
234
0
                break;
235
0
            case 'k':
236
0
            case 'K':
237
0
                max_jvm_heap_memory_size_ = value * 1024;
238
0
                break;
239
0
            default:
240
0
                max_jvm_heap_memory_size_ = value;
241
0
                break;
242
0
            }
243
0
        }
244
0
    }
245
0
    if (0 == max_jvm_heap_memory_size_) {
246
0
        LOG(FATAL) << "the max_jvm_heap_memory_size_ is " << max_jvm_heap_memory_size_;
247
0
    }
248
0
    LOG(INFO) << "the max_jvm_heap_memory_size_ is " << max_jvm_heap_memory_size_;
249
0
}
250
251
0
size_t JniUtil::get_max_jni_heap_memory_size() {
252
0
#if defined(USE_LIBHDFS3) || defined(BE_TEST)
253
0
    return std::numeric_limits<size_t>::max();
254
#else
255
    static std::once_flag parse_max_heap_memory_size_from_jvm_flag;
256
    std::call_once(parse_max_heap_memory_size_from_jvm_flag, parse_max_heap_memory_size_from_jvm,
257
                   tls_env_);
258
    return max_jvm_heap_memory_size_;
259
#endif
260
0
}
261
262
0
Status JniUtil::GetJNIEnvSlowPath(JNIEnv** env) {
263
0
    DCHECK(!tls_env_) << "Call GetJNIEnv() fast path";
264
265
#ifdef USE_LIBHDFS3
266
    std::call_once(g_vm_once, FindOrCreateJavaVM);
267
    int rc = g_vm->GetEnv(reinterpret_cast<void**>(&tls_env_), JNI_VERSION_1_8);
268
    if (rc == JNI_EDETACHED) {
269
        rc = g_vm->AttachCurrentThread((void**)&tls_env_, nullptr);
270
    }
271
    if (rc != 0 || tls_env_ == nullptr) {
272
        return Status::JniError("Unable to get JVM: {}", rc);
273
    }
274
#else
275
    // the hadoop libhdfs will do all the stuff
276
0
    std::call_once(g_jvm_conf_once, SetEnvIfNecessary);
277
0
    tls_env_ = getJNIEnv();
278
0
#endif
279
0
    *env = tls_env_;
280
0
    return Status::OK();
281
0
}
282
283
0
Status JniUtil::GetJniExceptionMsg(JNIEnv* env, bool log_stack, const string& prefix) {
284
0
    jthrowable exc = env->ExceptionOccurred();
285
0
    if (exc == nullptr) {
286
0
        return Status::OK();
287
0
    }
288
0
    env->ExceptionClear();
289
0
    DCHECK(throwable_to_string_id() != nullptr);
290
0
    const char* oom_msg_template =
291
0
            "$0 threw an unchecked exception. The JVM is likely out "
292
0
            "of memory (OOM).";
293
0
    jstring msg = static_cast<jstring>(
294
0
            env->CallStaticObjectMethod(jni_util_class(), throwable_to_string_id(), exc));
295
0
    if (env->ExceptionOccurred()) {
296
0
        env->ExceptionClear();
297
0
        string oom_msg = absl::Substitute(oom_msg_template, "throwableToString");
298
0
        LOG(WARNING) << oom_msg;
299
0
        return Status::JniError(oom_msg);
300
0
    }
301
0
    JniUtfCharGuard msg_str_guard;
302
0
    RETURN_IF_ERROR(JniUtfCharGuard::create(env, msg, &msg_str_guard));
303
0
    if (log_stack) {
304
0
        jstring stack = static_cast<jstring>(
305
0
                env->CallStaticObjectMethod(jni_util_class(), throwable_to_stack_trace_id(), exc));
306
0
        if (env->ExceptionOccurred()) {
307
0
            env->ExceptionClear();
308
0
            string oom_msg = absl::Substitute(oom_msg_template, "throwableToStackTrace");
309
0
            LOG(WARNING) << oom_msg;
310
0
            return Status::JniError(oom_msg);
311
0
        }
312
0
        JniUtfCharGuard c_stack_guard;
313
0
        RETURN_IF_ERROR(JniUtfCharGuard::create(env, stack, &c_stack_guard));
314
0
        LOG(WARNING) << c_stack_guard.get();
315
0
    }
316
317
0
    env->DeleteLocalRef(exc);
318
0
    return Status::JniError("{}{}", prefix, msg_str_guard.get());
319
0
}
320
321
Status JniUtil::convert_to_java_map(JNIEnv* env, const std::map<std::string, std::string>& map,
322
0
                                    jobject* hashmap_object) {
323
0
    jclass hashmap_class = env->FindClass("java/util/HashMap");
324
0
    RETURN_ERROR_IF_EXC(env);
325
0
    jmethodID hashmap_constructor = env->GetMethodID(hashmap_class, "<init>", "(I)V");
326
0
    RETURN_ERROR_IF_EXC(env);
327
0
    jobject hashmap_local_object = env->NewObject(hashmap_class, hashmap_constructor, map.size());
328
0
    RETURN_ERROR_IF_EXC(env);
329
0
    jmethodID hashmap_put = env->GetMethodID(
330
0
            hashmap_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
331
0
    RETURN_ERROR_IF_EXC(env);
332
0
    for (const auto& it : map) {
333
0
        jstring key = env->NewStringUTF(it.first.c_str());
334
0
        jstring value = env->NewStringUTF(it.second.c_str());
335
0
        env->CallObjectMethod(hashmap_local_object, hashmap_put, key, value);
336
0
        RETURN_ERROR_IF_EXC(env);
337
0
        env->DeleteLocalRef(key);
338
0
        env->DeleteLocalRef(value);
339
0
    }
340
0
    env->DeleteLocalRef(hashmap_class);
341
0
    RETURN_IF_ERROR(LocalToGlobalRef(env, hashmap_local_object, hashmap_object));
342
0
    return Status::OK();
343
0
}
344
345
Status JniUtil::convert_to_cpp_map(JNIEnv* env, jobject map,
346
0
                                   std::map<std::string, std::string>* resultMap) {
347
    // Get the class and method ID of the java.util.Map interface
348
0
    jclass mapClass = env->FindClass("java/util/Map");
349
0
    RETURN_ERROR_IF_EXC(env);
350
0
    jmethodID entrySetMethod = env->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;");
351
352
    // Get the class and method ID of the java.util.Set interface
353
0
    jclass setClass = env->FindClass("java/util/Set");
354
0
    RETURN_ERROR_IF_EXC(env);
355
0
    jmethodID iteratorSetMethod = env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;");
356
357
    // Get the class and method ID of the java.util.Iterator interface
358
0
    jclass iteratorClass = env->FindClass("java/util/Iterator");
359
0
    RETURN_ERROR_IF_EXC(env);
360
0
    jmethodID hasNextMethod = env->GetMethodID(iteratorClass, "hasNext", "()Z");
361
0
    jmethodID nextMethod = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
362
363
    // Get the class and method ID of the java.util.Map.Entry interface
364
0
    jclass entryClass = env->FindClass("java/util/Map$Entry");
365
0
    RETURN_ERROR_IF_EXC(env);
366
0
    jmethodID getKeyMethod = env->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;");
367
0
    jmethodID getValueMethod = env->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;");
368
369
    // Call the entrySet method to get the set of key-value pairs
370
0
    jobject entrySet = env->CallObjectMethod(map, entrySetMethod);
371
0
    RETURN_ERROR_IF_EXC(env);
372
373
    // Call the iterator method on the set to iterate over the key-value pairs
374
0
    jobject iteratorSet = env->CallObjectMethod(entrySet, iteratorSetMethod);
375
0
    RETURN_ERROR_IF_EXC(env);
376
377
    // Iterate over the key-value pairs
378
0
    while (env->CallBooleanMethod(iteratorSet, hasNextMethod)) {
379
0
        RETURN_ERROR_IF_EXC(env);
380
381
        // Get the current entry
382
0
        jobject entry = env->CallObjectMethod(iteratorSet, nextMethod);
383
0
        RETURN_ERROR_IF_EXC(env);
384
385
        // Get the key and value from the entry
386
0
        jobject javaKey = env->CallObjectMethod(entry, getKeyMethod);
387
0
        RETURN_ERROR_IF_EXC(env);
388
389
0
        jobject javaValue = env->CallObjectMethod(entry, getValueMethod);
390
0
        RETURN_ERROR_IF_EXC(env);
391
392
        // Convert the key and value to C++ strings
393
0
        const char* key = env->GetStringUTFChars(static_cast<jstring>(javaKey), nullptr);
394
0
        const char* value = env->GetStringUTFChars(static_cast<jstring>(javaValue), nullptr);
395
396
        // Store the key-value pair in the map
397
0
        (*resultMap)[key] = value;
398
399
        // Release the string references
400
0
        env->ReleaseStringUTFChars(static_cast<jstring>(javaKey), key);
401
0
        env->ReleaseStringUTFChars(static_cast<jstring>(javaValue), value);
402
403
        // Delete local references
404
0
        env->DeleteLocalRef(entry);
405
0
        env->DeleteLocalRef(javaKey);
406
0
        env->DeleteLocalRef(javaValue);
407
0
    }
408
409
    // Delete local references
410
0
    env->DeleteLocalRef(iteratorSet);
411
0
    env->DeleteLocalRef(entrySet);
412
0
    env->DeleteLocalRef(mapClass);
413
0
    env->DeleteLocalRef(setClass);
414
0
    env->DeleteLocalRef(iteratorClass);
415
0
    env->DeleteLocalRef(entryClass);
416
417
0
    return Status::OK();
418
0
}
419
420
0
Status JniUtil::GetGlobalClassRef(JNIEnv* env, const char* class_str, jclass* class_ref) {
421
0
    *class_ref = nullptr;
422
0
    JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF(jclass, local_cl, env, FindClass(class_str));
423
0
    RETURN_IF_ERROR(LocalToGlobalRef(env, local_cl, reinterpret_cast<jobject*>(class_ref)));
424
0
    return Status::OK();
425
0
}
426
427
0
Status JniUtil::LocalToGlobalRef(JNIEnv* env, jobject local_ref, jobject* global_ref) {
428
0
    *global_ref = env->NewGlobalRef(local_ref);
429
    // NewGlobalRef:
430
    // Returns a global reference to the given obj.
431
    //
432
    //May return NULL if:
433
    //  obj refers to null
434
    //  the system has run out of memory
435
    //  obj was a weak global reference and has already been garbage collected
436
0
    if (*global_ref == nullptr) {
437
0
        return Status::JniError(
438
0
                "LocalToGlobalRef fail,global ref is NULL,maybe the system has run out of memory.");
439
0
    }
440
441
    //NewGlobalRef not throw exception,maybe we just need check NULL.
442
0
    RETURN_ERROR_IF_EXC(env);
443
0
    return Status::OK();
444
0
}
445
446
0
Status JniUtil::init_jni_scanner_loader(JNIEnv* env) {
447
    // Get scanner loader;
448
0
    jclass jni_scanner_loader_cls;
449
0
    std::string jni_scanner_loader_str = "org/apache/doris/common/classloader/ScannerLoader";
450
0
    RETURN_IF_ERROR(JniUtil::GetGlobalClassRef(env, jni_scanner_loader_str.c_str(),
451
0
                                               &jni_scanner_loader_cls));
452
0
    jmethodID jni_scanner_loader_constructor =
453
0
            env->GetMethodID(jni_scanner_loader_cls, "<init>", "()V");
454
0
    RETURN_ERROR_IF_EXC(env);
455
0
    jni_scanner_loader_method_ = env->GetMethodID(jni_scanner_loader_cls, "getLoadedClass",
456
0
                                                  "(Ljava/lang/String;)Ljava/lang/Class;");
457
0
    if (jni_scanner_loader_method_ == nullptr) {
458
0
        if (env->ExceptionOccurred()) {
459
0
            env->ExceptionDescribe();
460
0
        }
461
0
        return Status::JniError("Failed to find ScannerLoader.getLoadedClass method.");
462
0
    }
463
0
    RETURN_ERROR_IF_EXC(env);
464
0
    jmethodID load_jni_scanner =
465
0
            env->GetMethodID(jni_scanner_loader_cls, "loadAllScannerJars", "()V");
466
0
    RETURN_ERROR_IF_EXC(env);
467
468
0
    jobject jni_scanner_loader_local_obj =
469
0
            env->NewObject(jni_scanner_loader_cls, jni_scanner_loader_constructor);
470
0
    jni_scanner_loader_obj_ = env->NewGlobalRef(jni_scanner_loader_local_obj);
471
0
    RETURN_ERROR_IF_EXC(env);
472
0
    env->DeleteLocalRef(jni_scanner_loader_local_obj);
473
0
    RETURN_ERROR_IF_EXC(env);
474
0
    env->CallVoidMethod(jni_scanner_loader_obj_, load_jni_scanner);
475
0
    RETURN_ERROR_IF_EXC(env);
476
477
0
    _clean_udf_cache_method_id = env->GetMethodID(jni_scanner_loader_cls, "cleanUdfClassLoader",
478
0
                                                  "(Ljava/lang/String;)V");
479
0
    if (_clean_udf_cache_method_id == nullptr) {
480
0
        if (env->ExceptionOccurred()) {
481
0
            env->ExceptionDescribe();
482
0
        }
483
0
        return Status::JniError("Failed to find removeUdfClassLoader method.");
484
0
    }
485
0
    RETURN_ERROR_IF_EXC(env);
486
0
    return Status::OK();
487
0
}
488
489
0
Status JniUtil::clean_udf_class_load_cache(const std::string& function_signature) {
490
0
    JNIEnv* env = nullptr;
491
0
    RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env));
492
0
    jstring function_signature_jstr = env->NewStringUTF(function_signature.c_str());
493
0
    RETURN_ERROR_IF_EXC(env);
494
0
    env->CallVoidMethod(jni_scanner_loader_obj_, _clean_udf_cache_method_id,
495
0
                        function_signature_jstr);
496
0
    RETURN_ERROR_IF_EXC(env);
497
0
    env->DeleteLocalRef(function_signature_jstr);
498
0
    RETURN_ERROR_IF_EXC(env);
499
0
    return Status::OK();
500
0
}
501
502
Status JniUtil::get_jni_scanner_class(JNIEnv* env, const char* classname,
503
0
                                      jclass* jni_scanner_class) {
504
    // Get JNI scanner class by class name;
505
0
    jstring class_name_str = env->NewStringUTF(classname);
506
0
    RETURN_ERROR_IF_EXC(env);
507
508
0
    jobject loaded_class_obj = env->CallObjectMethod(jni_scanner_loader_obj_,
509
0
                                                     jni_scanner_loader_method_, class_name_str);
510
0
    RETURN_ERROR_IF_EXC(env);
511
512
0
    *jni_scanner_class = reinterpret_cast<jclass>(env->NewGlobalRef(loaded_class_obj));
513
0
    RETURN_ERROR_IF_EXC(env);
514
515
0
    env->DeleteLocalRef(loaded_class_obj);
516
0
    RETURN_ERROR_IF_EXC(env);
517
0
    env->DeleteLocalRef(class_name_str);
518
0
    RETURN_ERROR_IF_EXC(env);
519
0
    return Status::OK();
520
0
}
521
522
0
Status JniUtil::Init() {
523
    // RETURN_IF_ERROR(LibJVMLoader::instance().load());
524
525
    // Get the JNIEnv* corresponding to current thread.
526
0
    JNIEnv* env = nullptr;
527
0
    RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env));
528
529
0
    if (env == nullptr) {
530
0
        return Status::JniError("Failed to get/create JVM");
531
0
    }
532
    // Find JniUtil class and create a global ref.
533
0
    jclass local_jni_util_cl = env->FindClass("org/apache/doris/common/jni/utils/JniUtil");
534
0
    if (local_jni_util_cl == nullptr) {
535
0
        if (env->ExceptionOccurred()) {
536
0
            env->ExceptionDescribe();
537
0
        }
538
0
        return Status::JniError("Failed to find JniUtil class.");
539
0
    }
540
0
    jni_util_cl_ = reinterpret_cast<jclass>(env->NewGlobalRef(local_jni_util_cl));
541
0
    if (jni_util_cl_ == nullptr) {
542
0
        if (env->ExceptionOccurred()) {
543
0
            env->ExceptionDescribe();
544
0
        }
545
0
        return Status::JniError("Failed to create global reference to JniUtil class.");
546
0
    }
547
0
    env->DeleteLocalRef(local_jni_util_cl);
548
0
    if (env->ExceptionOccurred()) {
549
0
        return Status::JniError("Failed to delete local reference to JniUtil class.");
550
0
    }
551
552
    // Find InternalException class and create a global ref.
553
0
    jclass local_internal_exc_cl =
554
0
            env->FindClass("org/apache/doris/common/exception/InternalException");
555
0
    if (local_internal_exc_cl == nullptr) {
556
0
        if (env->ExceptionOccurred()) {
557
0
            env->ExceptionDescribe();
558
0
        }
559
0
        return Status::JniError("Failed to find JniUtil class.");
560
0
    }
561
0
    internal_exc_cl_ = reinterpret_cast<jclass>(env->NewGlobalRef(local_internal_exc_cl));
562
0
    if (internal_exc_cl_ == nullptr) {
563
0
        if (env->ExceptionOccurred()) {
564
0
            env->ExceptionDescribe();
565
0
        }
566
0
        return Status::JniError("Failed to create global reference to JniUtil class.");
567
0
    }
568
0
    env->DeleteLocalRef(local_internal_exc_cl);
569
0
    if (env->ExceptionOccurred()) {
570
0
        return Status::JniError("Failed to delete local reference to JniUtil class.");
571
0
    }
572
573
    // Find JNINativeMethod class and create a global ref.
574
0
    jclass local_jni_native_exc_cl =
575
0
            env->FindClass("org/apache/doris/common/jni/utils/JNINativeMethod");
576
0
    if (local_jni_native_exc_cl == nullptr) {
577
0
        if (env->ExceptionOccurred()) {
578
0
            env->ExceptionDescribe();
579
0
        }
580
0
        return Status::JniError("Failed to find JNINativeMethod class.");
581
0
    }
582
0
    jni_native_method_exc_cl_ =
583
0
            reinterpret_cast<jclass>(env->NewGlobalRef(local_jni_native_exc_cl));
584
0
    if (jni_native_method_exc_cl_ == nullptr) {
585
0
        if (env->ExceptionOccurred()) {
586
0
            env->ExceptionDescribe();
587
0
        }
588
0
        return Status::JniError("Failed to create global reference to JNINativeMethod class.");
589
0
    }
590
0
    env->DeleteLocalRef(local_jni_native_exc_cl);
591
0
    if (env->ExceptionOccurred()) {
592
0
        return Status::JniError("Failed to delete local reference to JNINativeMethod class.");
593
0
    }
594
0
    std::string resize_column_name = "resizeStringColumn";
595
0
    std::string resize_column_sign = "(JI)J";
596
0
    std::string memory_alloc_name = "memoryTrackerMalloc";
597
0
    std::string memory_alloc_sign = "(J)J";
598
0
    std::string memory_free_name = "memoryTrackerFree";
599
0
    std::string memory_free_sign = "(J)V";
600
0
    static JNINativeMethod java_native_methods[] = {
601
0
            {const_cast<char*>(resize_column_name.c_str()),
602
0
             const_cast<char*>(resize_column_sign.c_str()),
603
0
             (void*)&JavaNativeMethods::resizeStringColumn},
604
0
            {const_cast<char*>(memory_alloc_name.c_str()),
605
0
             const_cast<char*>(memory_alloc_sign.c_str()), (void*)&JavaNativeMethods::memoryMalloc},
606
0
            {const_cast<char*>(memory_free_name.c_str()),
607
0
             const_cast<char*>(memory_free_sign.c_str()), (void*)&JavaNativeMethods::memoryFree},
608
0
    };
609
610
0
    int res = env->RegisterNatives(jni_native_method_exc_cl_, java_native_methods,
611
0
                                   sizeof(java_native_methods) / sizeof(java_native_methods[0]));
612
0
    DCHECK_EQ(res, 0);
613
614
    // Throwable toString()
615
0
    throwable_to_string_id_ = env->GetStaticMethodID(jni_util_cl_, "throwableToString",
616
0
                                                     "(Ljava/lang/Throwable;)Ljava/lang/String;");
617
0
    if (throwable_to_string_id_ == nullptr) {
618
0
        if (env->ExceptionOccurred()) {
619
0
            env->ExceptionDescribe();
620
0
        }
621
0
        return Status::JniError("Failed to find JniUtil.throwableToString method.");
622
0
    }
623
624
    // throwableToStackTrace()
625
0
    throwable_to_stack_trace_id_ = env->GetStaticMethodID(
626
0
            jni_util_cl_, "throwableToStackTrace", "(Ljava/lang/Throwable;)Ljava/lang/String;");
627
0
    if (throwable_to_stack_trace_id_ == nullptr) {
628
0
        if (env->ExceptionOccurred()) {
629
0
            env->ExceptionDescribe();
630
0
        }
631
0
        return Status::JniError("Failed to find JniUtil.throwableToFullStackTrace method.");
632
0
    }
633
634
0
    get_jvm_metrics_id_ = env->GetStaticMethodID(jni_util_cl_, "getJvmMemoryMetrics", "()[B");
635
0
    if (get_jvm_metrics_id_ == nullptr) {
636
0
        if (env->ExceptionOccurred()) {
637
0
            env->ExceptionDescribe();
638
0
        }
639
0
        return Status::JniError("Failed to find JniUtil.getJvmMemoryMetrics method.");
640
0
    }
641
642
0
    get_jvm_threads_id_ = env->GetStaticMethodID(jni_util_cl_, "getJvmThreadsInfo", "([B)[B");
643
0
    if (get_jvm_threads_id_ == nullptr) {
644
0
        if (env->ExceptionOccurred()) {
645
0
            env->ExceptionDescribe();
646
0
        }
647
0
        return Status::JniError("Failed to find JniUtil.getJvmThreadsInfo method.");
648
0
    }
649
650
0
    get_jmx_json_ = env->GetStaticMethodID(jni_util_cl_, "getJMXJson", "()[B");
651
0
    if (get_jmx_json_ == nullptr) {
652
0
        if (env->ExceptionOccurred()) {
653
0
            env->ExceptionDescribe();
654
0
        }
655
0
        return Status::JniError("Failed to find JniUtil.getJMXJson method.");
656
0
    }
657
0
    RETURN_IF_ERROR(init_jni_scanner_loader(env));
658
0
    jvm_inited_ = true;
659
0
    DorisMetrics::instance()->init_jvm_metrics(env);
660
0
    return Status::OK();
661
0
}
662
#include "common/compile_check_end.h"
663
} // namespace doris