Gabriel39 commented on a change in pull request #8516: URL: https://github.com/apache/incubator-doris/pull/8516#discussion_r830581551
########## File path: be/src/util/jni-util.cpp ########## @@ -0,0 +1,244 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "util/jni-util.h" + +#include <jni.h> +#include <stdlib.h> + +#include "gutil/once.h" +#include "gutil/strings/substitute.h" + +using std::string; + +namespace doris { + +namespace { +JavaVM* g_vm; +GoogleOnceType g_vm_once = GOOGLE_ONCE_INIT; + +void FindOrCreateJavaVM() { + int num_vms; + int rv = JNI_GetCreatedJavaVMs(&g_vm, 1, &num_vms); + if (rv == 0) { + LOG(INFO) << "Create first JVM"; + JNIEnv *env; + JavaVMInitArgs vm_args; + JavaVMOption options[1]; + std::stringstream ss; + char* str = getenv("DORIS_JNI_CLASSPATH_PARAMETER"); + options[0].optionString = str; + vm_args.version = JNI_VERSION_1_8; + vm_args.options = options; + vm_args.nOptions = 1; + vm_args.ignoreUnrecognized = JNI_TRUE; + + int res = JNI_CreateJavaVM(&g_vm, (void **)&env, &vm_args); + DCHECK_LT(res, 0) << "Failed tp create JVM, code= " << res; + } else { + CHECK_EQ(rv, 0) << "Could not find any created Java VM"; + CHECK_EQ(num_vms, 1) << "No VMs returned"; + } +} + +} // anonymous namespace + +bool JniUtil::jvm_inited_ = false; +__thread JNIEnv* JniUtil::tls_env_ = nullptr; +jclass JniUtil::internal_exc_cl_ = NULL; +jclass JniUtil::jni_util_cl_ = NULL; +jmethodID JniUtil::throwable_to_string_id_ = NULL; +jmethodID JniUtil::throwable_to_stack_trace_id_ = NULL; +jmethodID JniUtil::get_jvm_metrics_id_ = NULL; +jmethodID JniUtil::get_jvm_threads_id_ = NULL; +jmethodID JniUtil::get_jmx_json_ = NULL; + +Status JniUtfCharGuard::create(JNIEnv* env, jstring jstr, JniUtfCharGuard* out) { + DCHECK(jstr != nullptr); + DCHECK(!env->ExceptionCheck()); + jboolean is_copy; + const char* utf_chars = env->GetStringUTFChars(jstr, &is_copy); + bool exception_check = static_cast<bool>(env->ExceptionCheck()); + if (utf_chars == nullptr || exception_check) { + if (exception_check) env->ExceptionClear(); + if (utf_chars != nullptr) env->ReleaseStringUTFChars(jstr, utf_chars); + auto fail_message = "GetStringUTFChars failed. Probable OOM on JVM side"; + LOG(ERROR) << fail_message; + return Status::InternalError(fail_message); + } + out->env = env; + out->jstr = jstr; + out->utf_chars = utf_chars; + return Status::OK(); +} + +Status JniLocalFrame::push(JNIEnv* env, int max_local_ref) { + DCHECK(env_ == NULL); + DCHECK_GT(max_local_ref, 0); + if (env->PushLocalFrame(max_local_ref) < 0) { + env->ExceptionClear(); + return Status::InternalError("failed to push frame"); + } + env_ = env; + return Status::OK(); +} + +JNIEnv* JniUtil::GetJNIEnvSlowPath() { + DCHECK(!tls_env_) << "Call GetJNIEnv() fast path"; + + GoogleOnceInit(&g_vm_once, &FindOrCreateJavaVM); + int rc = g_vm->GetEnv(reinterpret_cast<void**>(&tls_env_), JNI_VERSION_1_8); Review comment: JNI will ensure backward compatibility. So use the earliest compatible JNI version is enough for Doris. Btw, for jdk 11, the lastest JNI_VERSION is JNI_VERSION_10 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
