Coverage Report

Created: 2024-11-20 21:05

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