You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/05 13:22:16 UTC

[GitHub] [arrow] lidavidm commented on a diff in pull request #13246: ARROW-16329: [Java][C++] Keep more context when marshalling errors through JNI

lidavidm commented on code in PR #13246:
URL: https://github.com/apache/arrow/pull/13246#discussion_r913789581


##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -162,11 +165,104 @@ std::shared_ptr<ReservationListener> ReservationListenableMemoryPool::get_listen
 
 ReservationListenableMemoryPool::~ReservationListenableMemoryPool() {}
 
+std::string Describe(JNIEnv* env, jthrowable t) {
+  jclass describer_class =
+      env->FindClass("org/apache/arrow/dataset/jni/JniExceptionDescriber");
+  jmethodID describe_method = env->GetStaticMethodID(
+      describer_class, "describe", "(Ljava/lang/Throwable;)Ljava/lang/String;");
+  std::string description = JStringToCString(
+      env, (jstring)env->CallStaticObjectMethod(describer_class, describe_method, t));
+  return description;
+}
+
+arrow::Result<bool> IsErrorInstanceOf(JNIEnv* env, jthrowable t, std::string class_name) {
+  jclass jclass = env->FindClass(class_name.c_str());
+  if (jclass == nullptr) {
+    return arrow::Status::Invalid("Class not found: " + class_name);

Review Comment:
   Given the way this is used below, maybe this should be a `DCHECK(jclass) << ...;` or just return `false`?



##########
java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniExceptionDescriber.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+
+package org.apache.arrow.dataset.jni;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * For native code to invoke to convert a java/lang/Throwable to jstring.
+ */
+public class JniExceptionDescriber {

Review Comment:
   Does this need to be a public class?



##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -162,11 +165,104 @@ std::shared_ptr<ReservationListener> ReservationListenableMemoryPool::get_listen
 
 ReservationListenableMemoryPool::~ReservationListenableMemoryPool() {}
 
+std::string Describe(JNIEnv* env, jthrowable t) {
+  jclass describer_class =
+      env->FindClass("org/apache/arrow/dataset/jni/JniExceptionDescriber");
+  jmethodID describe_method = env->GetStaticMethodID(
+      describer_class, "describe", "(Ljava/lang/Throwable;)Ljava/lang/String;");
+  std::string description = JStringToCString(
+      env, (jstring)env->CallStaticObjectMethod(describer_class, describe_method, t));
+  return description;
+}
+
+arrow::Result<bool> IsErrorInstanceOf(JNIEnv* env, jthrowable t, std::string class_name) {
+  jclass jclass = env->FindClass(class_name.c_str());
+  if (jclass == nullptr) {
+    return arrow::Status::Invalid("Class not found: " + class_name);
+  }
+  return env->IsInstanceOf(t, jclass);
+}
+
+arrow::StatusCode MapJavaError(JNIEnv* env, jthrowable t) {
+  StatusCode code;
+  if (IsErrorInstanceOf(env, t, "org/apache/arrow/memory/OutOfMemoryException") ==
+      JNI_TRUE) {
+    code = StatusCode::OutOfMemory;
+  } else if (IsErrorInstanceOf(env, t, "java/lang/UnsupportedOperationException") ==
+             JNI_TRUE) {
+    code = StatusCode::NotImplemented;
+  } else if (IsErrorInstanceOf(env, t, "java/io/NotSerializableException") == JNI_TRUE) {
+    code = StatusCode::SerializationError;
+  } else if (IsErrorInstanceOf(env, t, "java/io/IOException") == JNI_TRUE) {
+    code = StatusCode::IOError;
+  } else if (IsErrorInstanceOf(env, t, "java/lang/IllegalArgumentException") ==
+             JNI_TRUE) {
+    code = StatusCode::Invalid;
+  } else if (IsErrorInstanceOf(env, t, "java/lang/IllegalStateException") == JNI_TRUE) {
+    code = StatusCode::Invalid;
+  } else {
+    code = StatusCode::UnknownError;
+  }
+  return code;
+}
+
+JNIEnv* GetEnvOrAttach(JavaVM* vm) {
+  JNIEnv* env;
+  int getEnvStat = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);
+  if (getEnvStat == JNI_EDETACHED) {
+    // Reattach current thread to JVM
+    getEnvStat = vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr);
+    if (getEnvStat != JNI_OK) {
+      std::cout << "Failed to attach current thread to JVM. " << std::endl;

Review Comment:
   ARROW_LOG instead of std::cout?



##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -17,6 +17,7 @@
 
 #include "./jni_util.h"

Review Comment:
   ```suggestion
   #include "jni_util.h"
   ```



##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -17,14 +17,14 @@
 
 #include <mutex>
 
+#include "./jni_util.h"

Review Comment:
   ```suggestion
   #include "jni_util.h"
   ```
   since Kou just fixed this



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org