diff --git a/hbase-native-client/test-util/mini-cluster.cc b/hbase-native-client/test-util/mini-cluster.cc index 9dd2f12..6877101 100644 --- a/hbase-native-client/test-util/mini-cluster.cc +++ b/hbase-native-client/test-util/mini-cluster.cc @@ -80,7 +80,6 @@ MiniCluster::~MiniCluster() { void MiniCluster::Setup() { jmethodID constructor; - pthread_mutex_lock(&count_mutex_); if (env_ == NULL) { env_ = CreateVM(&jvm_); if (env_ == NULL) { @@ -200,16 +199,21 @@ jbyteArray MiniCluster::StrToByteChar(const std::string &str) { } jobject MiniCluster::CreateTable(const std::string &table, const std::string &family) { + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); jstring table_name_str = env_->NewStringUTF(table.c_str()); jobject table_name = env_->CallStaticObjectMethod(table_name_class_, tbl_name_value_of_mid_, table_name_str); jstring family_str = env_->NewStringUTF(family.c_str()); jobject table_obj = env_->CallObjectMethod(htu_, create_table_mid_, table_name, family_str); + jvm_->DetachCurrentThread(); return table_obj; } jobject MiniCluster::CreateTable(const std::string &table, const std::vector &families) { + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); jstring table_name_str = env_->NewStringUTF(table.c_str()); jobject table_name = env_->CallStaticObjectMethod(table_name_class_, tbl_name_value_of_mid_, table_name_str); @@ -221,6 +225,7 @@ jobject MiniCluster::CreateTable(const std::string &table, } jobject table_obj = env_->CallObjectMethod(htu_, create_table_families_mid_, table_name, family_array); + jvm_->DetachCurrentThread(); return table_obj; } @@ -233,6 +238,8 @@ jobject MiniCluster::CreateTable(const std::string &table, const std::string &fa jobject MiniCluster::CreateTable(const std::string &table, const std::vector &families, const std::vector &keys) { + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); jstring table_name_str = env_->NewStringUTF(table.c_str()); jobject table_name = env_->CallStaticObjectMethod(table_name_class_, tbl_name_value_of_mid_, table_name_str); @@ -253,24 +260,36 @@ jobject MiniCluster::CreateTable(const std::string &table, const std::vectorCallObjectMethod(htu_, create_table_with_split_mid_, table_name, family_array, key_array); + jvm_->DetachCurrentThread(); return tbl; } jobject MiniCluster::StopRegionServer(int idx) { env(); - return env_->CallObjectMethod(cluster_, stop_rs_mid_, (jint)idx); + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); + auto ret = env_->CallObjectMethod(cluster_, stop_rs_mid_, (jint)idx); + jvm_->DetachCurrentThread(); + return ret; } // returns the Configuration for the cluster jobject MiniCluster::GetConf() { env(); - return env_->CallObjectMethod(cluster_, get_conf_mid_); + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); + auto ret = env_->CallObjectMethod(cluster_, get_conf_mid_); + jvm_->DetachCurrentThread(); + return ret; } // return the Admin instance for the local cluster jobject MiniCluster::admin() { env(); + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); jobject conn = env_->CallObjectMethod(htu(), get_conn_mid_); jobject admin = env_->CallObjectMethod(conn, get_admin_mid_); + jvm_->DetachCurrentThread(); return admin; } @@ -282,6 +301,8 @@ void MiniCluster::MoveRegion(const std::string ®ion, const std::string &serve jobject MiniCluster::StartCluster(int num_region_servers) { env(); + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); jmethodID mid = env_->GetMethodID(testing_util_class_, "startMiniCluster", "(I)Lorg/apache/hadoop/hbase/MiniHBaseCluster;"); if (mid == NULL) { @@ -289,13 +310,17 @@ jobject MiniCluster::StartCluster(int num_region_servers) { exit(-1); } cluster_ = env_->CallObjectMethod(htu(), mid, static_cast(num_region_servers)); + jvm_->DetachCurrentThread(); return cluster_; } void MiniCluster::StopCluster() { env(); + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void **)&env_, NULL); jmethodID mid = env_->GetMethodID(testing_util_class_, "shutdownMiniCluster", "()V"); env_->CallVoidMethod(htu(), mid); + jvm_->DetachCurrentThread(); if (jvm_ != NULL) { jvm_->DestroyJavaVM(); jvm_ = NULL; @@ -303,9 +328,12 @@ void MiniCluster::StopCluster() { } const std::string MiniCluster::GetConfValue(const std::string &key) { + std::lock_guard lock(mutex_); + jvm_->AttachCurrentThread((void**) &env_, NULL); jobject conf = GetConf(); jstring jval = (jstring)env_->CallObjectMethod(conf, conf_get_mid_, env_->NewStringUTF(key.c_str())); const char *val = env_->GetStringUTFChars(jval, 0); + jvm_->DetachCurrentThread(); return val; } diff --git a/hbase-native-client/test-util/mini-cluster.h b/hbase-native-client/test-util/mini-cluster.h index 6b4547c..e025556 100644 --- a/hbase-native-client/test-util/mini-cluster.h +++ b/hbase-native-client/test-util/mini-cluster.h @@ -19,6 +19,7 @@ #pragma once #include +#include #include #include @@ -45,6 +46,7 @@ class MiniCluster { const std::string GetConfValue(const std::string &key); private: + std::recursive_mutex mutex_; JNIEnv *env_; jclass testing_util_class_; jclass table_name_class_; diff --git a/hbase-native-client/test-util/test-util.h b/hbase-native-client/test-util/test-util.h index 40e99d1..7a5037c 100644 --- a/hbase-native-client/test-util/test-util.h +++ b/hbase-native-client/test-util/test-util.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include "core/configuration.h"