Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::JniUtil Class Reference

#include <jni-util.h>

Collaboration diagram for impala::JniUtil:

Static Public Member Functions

static void InitLibhdfs ()
 Call this prior to any libhdfs calls. More...
 
static Status Init ()
 Find JniUtil class, and get JniUtil.throwableToString method id. More...
 
static bool ClassExists (JNIEnv *env, const char *class_str)
 
static Status GetGlobalClassRef (JNIEnv *env, const char *class_str, jclass *class_ref)
 
static Status LocalToGlobalRef (JNIEnv *env, jobject local_ref, jobject *global_ref)
 
static jmethodID throwable_to_string_id ()
 
static jmethodID throwable_to_stack_trace_id ()
 
static jclass jni_util_class ()
 Global reference to java JniUtil class. More...
 
static jclass internal_exc_class ()
 Global reference to InternalException class. More...
 
static Status Cleanup ()
 Delete all global references: class members, and those stored in global_refs_. More...
 
static Status GetJniExceptionMsg (JNIEnv *env, bool log_stack=true, const std::string &prefix="")
 
static Status GetJvmMetrics (const TGetJvmMetricsRequest &request, TGetJvmMetricsResponse *result)
 
static Status LoadJniMethod (JNIEnv *jni_env, const jclass &jni_class, JniMethodDescriptor *descriptor)
 
template<typename T >
static Status CallJniMethod (const jobject &obj, const jmethodID &method, const T &arg)
 
template<typename R >
static Status CallJniMethod (const jobject &obj, const jmethodID &method, R *response)
 
template<typename T , typename R >
static Status CallJniMethod (const jobject &obj, const jmethodID &method, const T &arg, R *response)
 
template<typename T >
static Status CallJniMethod (const jobject &obj, const jmethodID &method, const T &arg, std::string *response)
 

Static Private Attributes

static jclass jni_util_cl_ = NULL
 
static jclass internal_exc_cl_ = NULL
 
static jmethodID throwable_to_string_id_ = NULL
 
static jmethodID throwable_to_stack_trace_id_ = NULL
 
static jmethodID get_jvm_metrics_id_ = NULL
 
static std::vector< jobject > global_refs_
 

Detailed Description

Utility class for JNI-related functionality. Init() should be called as soon as the native library is loaded. Creates global class references, and promotes local references to global references. Maintains a list of all global references for cleanup in Cleanup(). Attention! Lifetime of JNI components and common pitfalls:

  1. JNIEnv* cannot be shared among threads, so it should NOT be globally cached.
  2. References created via jnienv->New*() calls are local references that go out of scope at the end of a code block (and will be gc'ed by the JVM). They should NOT be cached.
  3. Use global references for caching classes. They need to be explicitly created and cleaned up (will not be gc'd up by the JVM). Global references can be shared among threads.
  4. JNI method ids and field ids are tied to the JVM that created them, and can be shared among threads. They are not "references" so there is no need to explicitly create a global reference to them.

Definition at line 174 of file jni-util.h.

Member Function Documentation

template<typename T >
static Status impala::JniUtil::CallJniMethod ( const jobject &  obj,
const jmethodID &  method,
const T &  arg 
)
inlinestatic
template<typename R >
static Status impala::JniUtil::CallJniMethod ( const jobject &  obj,
const jmethodID &  method,
R *  response 
)
inlinestatic
template<typename T , typename R >
static Status impala::JniUtil::CallJniMethod ( const jobject &  obj,
const jmethodID &  method,
const T &  arg,
R *  response 
)
inlinestatic
template<typename T >
static Status impala::JniUtil::CallJniMethod ( const jobject &  obj,
const jmethodID &  method,
const T &  arg,
std::string *  response 
)
inlinestatic
bool impala::JniUtil::ClassExists ( JNIEnv *  env,
const char *  class_str 
)
static

Returns true if the given class could be found on the CLASSPATH in env. Returns false otherwise, or if any other error occurred (e.g. a JNI exception). This function does not log any errors or exceptions.

Definition at line 45 of file jni-util.cc.

Referenced by impala::HBaseTableScanner::Init().

Status impala::JniUtil::Cleanup ( )
static

Delete all global references: class members, and those stored in global_refs_.

Definition at line 147 of file jni-util.cc.

References getJNIEnv(), global_refs_, and impala::Status::OK.

Status impala::JniUtil::GetGlobalClassRef ( JNIEnv *  env,
const char *  class_str,
jclass *  class_ref 
)
static

Returns a global JNI reference to the class specified by class_str into class_ref. The reference is added to global_refs_ for cleanup in Deinit(). Returns Status::OK if successful. Catches Java exceptions and converts their message into status.

Definition at line 56 of file jni-util.cc.

References LocalToGlobalRef(), impala::Status::OK, RETURN_ERROR_IF_EXC, and RETURN_IF_ERROR.

Referenced by impala::HBaseTableScanner::Init(), impala::HBaseTable::InitJNI(), impala::HBaseTableWriter::InitJNI(), and impala::HiveUdfCall::Open().

Status impala::JniUtil::GetJniExceptionMsg ( JNIEnv *  env,
bool  log_stack = true,
const std::string &  prefix = "" 
)
static

Returns the error message for 'e'. If no exception, returns Status::OK log_stack determines if the stack trace is written to the log prefix, if non-empty will be prepended to the error message.

Definition at line 161 of file jni-util.cc.

References jni_util_class(), impala::Status::OK, throwable_to_stack_trace_id(), and throwable_to_string_id().

Referenced by impala::HBaseTable::Close(), impala::ExternalDataSourceExecutor::Close(), impala::HiveUdfCall::Close(), impala::HBaseTableScanner::Close(), impala::HiveUdfCall::Evaluate(), and impala::HBaseTableScanner::HandleResultScannerTimeout().

Status impala::JniUtil::GetJvmMetrics ( const TGetJvmMetricsRequest &  request,
TGetJvmMetricsResponse *  result 
)
static

Populates 'result' with a list of memory metrics from the Jvm. Returns Status::OK unless there is an exception.

Definition at line 188 of file jni-util.cc.

References CallJniMethod(), get_jvm_metrics_id_, and jni_util_class().

Referenced by impala::JvmMetric::CalculateValue(), and impala::JvmMetric::InitMetrics().

Status impala::JniUtil::Init ( )
static

Find JniUtil class, and get JniUtil.throwableToString method id.

Definition at line 74 of file jni-util.cc.

References get_jvm_metrics_id_, getJNIEnv(), internal_exc_cl_, jni_util_cl_, impala::Status::OK, throwable_to_stack_trace_id_, and throwable_to_string_id_.

Referenced by main().

void impala::JniUtil::InitLibhdfs ( )
static

Call this prior to any libhdfs calls.

Definition at line 140 of file jni-util.cc.

Referenced by main().

static jclass impala::JniUtil::jni_util_class ( )
inlinestatic

Global reference to java JniUtil class.

Definition at line 203 of file jni-util.h.

References jni_util_cl_.

Referenced by GetJniExceptionMsg(), and GetJvmMetrics().

Status impala::JniUtil::LoadJniMethod ( JNIEnv *  jni_env,
const jclass &  jni_class,
JniMethodDescriptor descriptor 
)
static

Loads a method whose signature is in the supplied descriptor. Returns Status::OK and sets descriptor->method_id to a JNI method handle if successful, otherwise an error status is returned.

Definition at line 193 of file jni-util.cc.

References impala::JniMethodDescriptor::method_id, impala::JniMethodDescriptor::name, impala::Status::OK, RETURN_ERROR_IF_EXC, and impala::JniMethodDescriptor::signature.

Referenced by impala::Catalog::Catalog(), impala::Frontend::Frontend(), impala::ExternalDataSourceExecutor::Init(), and impala::RequestPoolService::RequestPoolService().

Status impala::JniUtil::LocalToGlobalRef ( JNIEnv *  env,
jobject  local_ref,
jobject *  global_ref 
)
static

Creates a global reference from a local reference returned into global_ref. Adds global reference to global_refs_ for cleanup in Deinit(). Returns Status::OK if successful. Catches Java exceptions and converts their message into status.

Definition at line 67 of file jni-util.cc.

References global_refs_, impala::Status::OK, and RETURN_ERROR_IF_EXC.

Referenced by impala::Catalog::Catalog(), impala::HBaseTableWriter::CreatePutList(), impala::Frontend::Frontend(), GetGlobalClassRef(), impala::HBaseTable::Init(), impala::HBaseTableFactory::Init(), impala::HBaseTableWriter::Init(), impala::HBaseTableScanner::Init(), and impala::RequestPoolService::RequestPoolService().

static jmethodID impala::JniUtil::throwable_to_stack_trace_id ( )
inlinestatic

Definition at line 200 of file jni-util.h.

References throwable_to_stack_trace_id_.

Referenced by GetJniExceptionMsg().

static jmethodID impala::JniUtil::throwable_to_string_id ( )
inlinestatic

Definition at line 199 of file jni-util.h.

References throwable_to_string_id_.

Referenced by GetJniExceptionMsg().

Member Data Documentation

jmethodID impala::JniUtil::get_jvm_metrics_id_ = NULL
staticprivate

Definition at line 294 of file jni-util.h.

Referenced by GetJvmMetrics(), and Init().

vector< jobject > impala::JniUtil::global_refs_
staticprivate

List of global references created with GetGlobalClassRef() or LocalToGlobalRef. All global references are deleted in Cleanup().

Definition at line 297 of file jni-util.h.

Referenced by Cleanup(), and LocalToGlobalRef().

jclass impala::JniUtil::internal_exc_cl_ = NULL
staticprivate

Definition at line 291 of file jni-util.h.

Referenced by Init(), and internal_exc_class().

jclass impala::JniUtil::jni_util_cl_ = NULL
staticprivate

Definition at line 290 of file jni-util.h.

Referenced by Init(), and jni_util_class().

jmethodID impala::JniUtil::throwable_to_stack_trace_id_ = NULL
staticprivate

Definition at line 293 of file jni-util.h.

Referenced by Init(), and throwable_to_stack_trace_id().

jmethodID impala::JniUtil::throwable_to_string_id_ = NULL
staticprivate

Definition at line 292 of file jni-util.h.

Referenced by Init(), and throwable_to_string_id().


The documentation for this class was generated from the following files: