Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hbase-table-factory.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include "common/status.h"
18 #include "common/logging.h"
19 #include "runtime/hbase-table.h"
20 #include "util/jni-util.h"
21 
22 #include "common/names.h"
23 
24 namespace impala {
25 
28 
29 jobject HBaseTableFactory::conf_ = NULL;
30 jobject HBaseTableFactory::executor_ = NULL;
31 
33  // Get the JNIEnv* corresponding to current thread.
34  JNIEnv* env = getJNIEnv();
35  if (env == NULL) {
36  return Status("Failed to get/create JVM");
37  }
38 
39  // Get the Configuration and an ExecutorService.
40  // These will remain the same across all threads.
41  // Get o.a.h.Configuration via HBaseConfiguration
42  jclass hbase_conf_cl =
43  env->FindClass("org/apache/hadoop/hbase/HBaseConfiguration");
45 
46  jmethodID hbase_conf_create_id =
47  env->GetStaticMethodID(hbase_conf_cl, "create",
48  "()Lorg/apache/hadoop/conf/Configuration;");
50 
51  jobject local_conf =
52  env->CallStaticObjectMethod(hbase_conf_cl, hbase_conf_create_id);
54  JniUtil::LocalToGlobalRef(env, local_conf, &conf_));
55  env->DeleteLocalRef(local_conf);
57 
58  // ExecutorService from java.util.concurrent.Executors#newCachedThreadPool
59  // Get an executor that can spin up and down threads; this should allow
60  // repeated calls to HBase to be fast but still mean no extra memory or
61  // threads are needed if there have been no HBase scans in a while.
62  jclass executors_cl = env->FindClass("java/util/concurrent/Executors");
64 
65  jmethodID executors_cached_thread = env->GetStaticMethodID(executors_cl,
66  "newCachedThreadPool", "()Ljava/util/concurrent/ExecutorService;");
68 
69  // Now create a single executor that will be used for all HTable's.
70  jobject local_executor =
71  env->CallStaticObjectMethod(executors_cl, executors_cached_thread);
73 
74  // Make sure the GC doesn't clean up the executor.
76  JniUtil::LocalToGlobalRef(env, local_executor, &executor_));
77 
78  env->DeleteLocalRef(local_executor);
80 
81  // Executor class and methods for shutdown.
82  executor_cl_ = env->FindClass("java/util/concurrent/ExecutorService");
84 
85  executor_shutdown_id_ = env->GetMethodID(executor_cl_, "shutdownNow",
86  "()Ljava/util/List;");
88 
90  return Status::OK;
91 }
92 
94  JNIEnv* env = getJNIEnv();
95 
96  // Clean up the global refs and stop the threads.
97  if (conf_ != NULL) {
98  env->DeleteGlobalRef(conf_);
99  }
100 
101  if (executor_ != NULL && executor_shutdown_id_ != NULL) {
102  env->CallObjectMethod(executor_, executor_shutdown_id_);
103  env->DeleteGlobalRef(executor_);
104  }
105 }
106 
107 Status HBaseTableFactory::GetTable(const string& table_name,
108  scoped_ptr<HBaseTable>* hbase_table) {
109  hbase_table->reset(new HBaseTable(table_name, conf_, executor_));
110  RETURN_IF_ERROR((*hbase_table)->Init());
111  return Status::OK;
112 }
113 
114 } // namespace impala
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
static Status InitJNI()
Call this to initialize the HBase HTable jni references.
Definition: hbase-table.cc:92
static jmethodID executor_shutdown_id_
Status GetTable(const std::string &table_name, boost::scoped_ptr< HBaseTable > *hbase_table)
Class to wrap JNI calls into HTable.
Definition: hbase-table.h:29
static jobject conf_
Configuration and executor jobject's. Initialized in Init().
static Status LocalToGlobalRef(JNIEnv *env, jobject local_ref, jobject *global_ref)
Definition: jni-util.cc:67
#define RETURN_ERROR_IF_EXC(env)
Definition: jni-util.h:99
static const Status OK
Definition: status.h:87
static jclass executor_cl_
ExecutorService class and methods.
JNIEnv * getJNIEnv(void)
C linkage for helper functions in hdfsJniHelper.h.