[fix](jni) avoid BE crash and NPE when close paimon reader (#27129)

1. Do not use FATAL log when jni encounter error, to avoid crash.
2. Fix NPE when closing PaimonReader, the reader may not be assigned if PaimonReader open failed.
This commit is contained in:
Mingyu Chen
2023-11-17 20:01:08 +08:00
committed by GitHub
parent 635a339bce
commit c459408580
3 changed files with 8 additions and 6 deletions

View File

@ -95,6 +95,7 @@ Status JniConnector::open(RuntimeState* state, RuntimeProfile* profile) {
RETURN_IF_ERROR(_init_jni_scanner(env, batch_size));
// Call org.apache.doris.common.jni.JniScanner#open
env->CallVoidMethod(_jni_scanner_obj, _jni_scanner_open);
_scanner_opened = true;
RETURN_ERROR_IF_EXC(env);
return Status::OK();
}
@ -167,7 +168,7 @@ Status JniConnector::close() {
if (!_closed) {
JNIEnv* env = nullptr;
RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env));
if (_scanner_initialized) {
if (_scanner_opened) {
// update scanner metrics
for (const auto& metric : get_statistics(env)) {
std::vector<std::string> type_and_name = split(metric.first, ":");
@ -204,8 +205,8 @@ Status JniConnector::close() {
_closed = true;
jthrowable exc = (env)->ExceptionOccurred();
if (exc != nullptr) {
LOG(FATAL) << "Failed to release jni resource: "
<< JniUtil::GetJniExceptionMsg(env).to_string();
LOG(WARNING) << "Failed to release jni resource: "
<< JniUtil::GetJniExceptionMsg(env).to_string();
}
}
return Status::OK();
@ -241,7 +242,6 @@ Status JniConnector::_init_jni_scanner(JNIEnv* env, int batch_size) {
_jni_scanner_get_statistics =
env->GetMethodID(_jni_scanner_cls, "getStatistics", "()Ljava/util/Map;");
RETURN_IF_ERROR(JniUtil::LocalToGlobalRef(env, jni_scanner_obj, &_jni_scanner_obj));
_scanner_initialized = true;
env->DeleteLocalRef(jni_scanner_obj);
RETURN_ERROR_IF_EXC(env);
return Status::OK();

View File

@ -292,7 +292,7 @@ private:
size_t _has_read = 0;
bool _closed = false;
bool _scanner_initialized = false;
bool _scanner_opened = false;
jclass _jni_scanner_cls;
jobject _jni_scanner_obj;
jmethodID _jni_scanner_open;

View File

@ -124,7 +124,9 @@ public class PaimonJniScanner extends JniScanner {
@Override
public void close() throws IOException {
reader.close();
if (reader != null) {
reader.close();
}
}
@Override