This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch branch-1.2-lts in repository https://gitbox.apache.org/repos/asf/doris.git
commit df986136c6f7db8707322f630ec2832bb2a4f84c Author: Adonis Ling <adonis0...@gmail.com> AuthorDate: Wed Apr 26 12:06:38 2023 +0800 [enhancement](JNI) Provide default environment variables if it is unset (#19041) --- be/src/util/jni-util.cpp | 65 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/be/src/util/jni-util.cpp b/be/src/util/jni-util.cpp index 01558cb2a9..2b1b82983e 100644 --- a/be/src/util/jni-util.cpp +++ b/be/src/util/jni-util.cpp @@ -41,33 +41,53 @@ namespace { JavaVM* g_vm; [[maybe_unused]] std::once_flag g_vm_once; -const std::string GetDorisJNIClasspath() { +const std::string GetDorisJNIDefaultClasspath() { + const auto* doris_home = getenv("DORIS_HOME"); + DCHECK(doris_home) << "Environment variable DORIS_HOME is not set."; + + std::ostringstream out; + std::string path(doris_home); + path += "/lib"; + for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) { + if (entry.path().extension() != ".jar") { + continue; + } + if (out.str().empty()) { + out << entry.path().string(); + } else { + out << ":" << entry.path().string(); + } + } + + DCHECK(!out.str().empty()) << "Empty classpath is invalid."; + return out.str(); +} + +const std::string GetDorisJNIClasspathOption() { const auto* classpath = getenv("DORIS_CLASSPATH"); if (classpath) { return classpath; } else { - const auto* doris_home = getenv("DORIS_HOME"); - DCHECK(doris_home) << "Environment variable DORIS_HOME is not set."; - - std::ostringstream out; - std::string path(doris_home); - path += "/lib"; - for (const auto& entry : std::filesystem::directory_iterator(path)) { - if (entry.path().extension() != ".jar") { - continue; - } - if (out.str().empty()) { - out << "-Djava.class.path=" << entry.path().string(); - } else { - out << ":" << entry.path().string(); - } - } - - DCHECK(!out.str().empty()) << "Empty classpath is invalid."; - return out.str(); + return "-Djava.class.path=" + GetDorisJNIDefaultClasspath(); } } +[[maybe_unused]] void SetEnvIfNecessary() { + const auto* doris_home = getenv("DORIS_HOME"); + DCHECK(doris_home) << "Environment variable DORIS_HOME is not set."; + + // CLASSPATH + static const std::string classpath = + fmt::format("{}/conf:{}", doris_home, GetDorisJNIDefaultClasspath()); + setenv("CLASSPATH", classpath.c_str(), 0); + + // LIBHDFS_OPTS + setenv("LIBHDFS_OPTS", + fmt::format("-Djava.library.path={}/lib/hadoop_hdfs/native", getenv("DORIS_HOME")) + .c_str(), + 0); +} + // Only used on non-x86 platform [[maybe_unused]] void FindOrCreateJavaVM() { int num_vms; @@ -78,7 +98,7 @@ const std::string GetDorisJNIClasspath() { char* java_opts = getenv("JAVA_OPTS"); if (java_opts == nullptr) { options = { - GetDorisJNIClasspath(), fmt::format("-Xmx{}", "1g"), + GetDorisJNIClasspathOption(), fmt::format("-Xmx{}", "1g"), fmt::format("-DlogPath={}/log/jni.log", getenv("DORIS_HOME")), fmt::format("-Dsun.java.command={}", "DorisBE"), "-XX:-CriticalJNINatives", #ifdef __APPLE__ @@ -93,7 +113,7 @@ const std::string GetDorisJNIClasspath() { std::istringstream stream(java_opts); options = std::vector<std::string>(std::istream_iterator<std::string> {stream}, std::istream_iterator<std::string>()); - options.push_back(GetDorisJNIClasspath()); + options.push_back(GetDorisJNIClasspathOption()); } std::unique_ptr<JavaVMOption[]> jvm_options(new JavaVMOption[options.size()]); for (int i = 0; i < options.size(); ++i) { @@ -175,6 +195,7 @@ Status JniUtil::GetJNIEnvSlowPath(JNIEnv** env) { } #else // the hadoop libhdfs will do all the stuff + SetEnvIfNecessary(); tls_env_ = getJNIEnv(); #endif *env = tls_env_; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org