Coverage Report

Created: 2025-04-22 23:04

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