Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
frontend.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 
15 #include "service/frontend.h"
16 
17 #include <list>
18 #include <string>
19 
20 #include "common/logging.h"
21 #include "rpc/jni-thrift-util.h"
22 #include "util/jni-util.h"
23 #include "util/logging-support.h"
24 
25 #include "common/names.h"
26 
27 using namespace impala;
28 
29 DECLARE_string(sentry_config);
30 DECLARE_int32(non_impala_java_vlog);
31 
32 DEFINE_bool(load_catalog_at_startup, false, "if true, load all catalog data at startup");
33 
34 // Authorization related flags. Must be set to valid values to properly configure
35 // authorization.
36 DEFINE_string(server_name, "", "The name to use for securing this impalad "
37  "server during authorization. Set to enable authorization. By default, the "
38  "authorization policy will be loaded from the catalog server (via the statestore)."
39  "To use a file based authorization policy, set --authorization_policy_file.");
40 DEFINE_string(authorization_policy_file, "", "HDFS path to the authorization policy "
41  "file. If set, authorization will be enabled and the authorization policy will be "
42  "read from a file.");
43 DEFINE_string(authorization_policy_provider_class,
44  "org.apache.sentry.provider.common.HadoopGroupResourceAuthorizationProvider",
45  "Advanced: The authorization policy provider class name.");
46 DEFINE_string(authorized_proxy_user_config, "",
47  "Specifies the set of authorized proxy users (users who can delegate to other "
48  "users during authorization) and whom they are allowed to delegate. "
49  "Input is a semicolon-separated list of key=value pairs of authorized proxy "
50  "users to the user(s) they can delegate to. These users are specified as a comma "
51  "separated list of short usernames, or '*' to indicate all users. For example: "
52  "hue=user1,user2;admin=*");
53 
55  JniMethodDescriptor methods[] = {
56  {"<init>", "(ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;"
57  "Ljava/lang/String;II)V", &fe_ctor_},
58  {"createExecRequest", "([B)[B", &create_exec_request_id_},
59  {"getExplainPlan", "([B)Ljava/lang/String;", &get_explain_plan_id_},
60  {"getHadoopConfig", "([B)[B", &get_hadoop_config_id_},
61  {"getAllHadoopConfigs", "()[B", &get_hadoop_configs_id_},
62  {"checkConfiguration", "()Ljava/lang/String;", &check_config_id_},
63  {"updateCatalogCache", "([B)[B", &update_catalog_cache_id_},
64  {"getTableNames", "([B)[B", &get_table_names_id_},
65  {"describeTable", "([B)[B", &describe_table_id_},
66  {"showCreateTable", "([B)Ljava/lang/String;", &show_create_table_id_},
67  {"getDbNames", "([B)[B", &get_db_names_id_},
68  {"getDataSrcMetadata", "([B)[B", &get_data_src_metadata_id_},
69  {"getStats", "([B)[B", &get_stats_id_},
70  {"getFunctions", "([B)[B", &get_functions_id_},
71  {"getCatalogObject", "([B)[B", &get_catalog_object_id_},
72  {"getRoles", "([B)[B", &show_roles_id_},
73  {"getRolePrivileges", "([B)[B", &get_role_privileges_id_},
74  {"execHiveServer2MetadataOp", "([B)[B", &exec_hs2_metadata_op_id_},
75  {"setCatalogInitialized", "()V", &set_catalog_initialized_id_},
76  {"loadTableData", "([B)[B", &load_table_data_id_},
77  {"getTableFiles", "([B)[B", &get_table_files_id_}};
78 
79  JNIEnv* jni_env = getJNIEnv();
80  // create instance of java class JniFrontend
81  fe_class_ = jni_env->FindClass("com/cloudera/impala/service/JniFrontend");
82  EXIT_IF_EXC(jni_env);
83 
84  uint32_t num_methods = sizeof(methods) / sizeof(methods[0]);
85  for (int i = 0; i < num_methods; ++i) {
86  EXIT_IF_ERROR(JniUtil::LoadJniMethod(jni_env, fe_class_, &(methods[i])));
87  };
88 
89  jboolean lazy = (FLAGS_load_catalog_at_startup ? false : true);
90  jstring policy_file_path =
91  jni_env->NewStringUTF(FLAGS_authorization_policy_file.c_str());
92  jstring server_name =
93  jni_env->NewStringUTF(FLAGS_server_name.c_str());
94  jstring sentry_config =
95  jni_env->NewStringUTF(FLAGS_sentry_config.c_str());
96  jstring auth_provider_class =
97  jni_env->NewStringUTF(FLAGS_authorization_policy_provider_class.c_str());
98  jobject fe = jni_env->NewObject(fe_class_, fe_ctor_, lazy, server_name,
99  policy_file_path, sentry_config, auth_provider_class, FlagToTLogLevel(FLAGS_v),
100  FlagToTLogLevel(FLAGS_non_impala_java_vlog));
101  EXIT_IF_EXC(jni_env);
103 }
104 
105 Status Frontend::UpdateCatalogCache(const TUpdateCatalogCacheRequest& req,
106  TUpdateCatalogCacheResponse* resp) {
108 }
109 
110 Status Frontend::DescribeTable(const TDescribeTableParams& params,
111  TDescribeTableResult* response) {
112  return JniUtil::CallJniMethod(fe_, describe_table_id_, params, response);
113 }
114 
115 Status Frontend::ShowCreateTable(const TTableName& table_name, string* response) {
116  return JniUtil::CallJniMethod(fe_, show_create_table_id_, table_name, response);
117 }
118 
119 Status Frontend::GetTableNames(const string& db, const string* pattern,
120  const TSessionState* session, TGetTablesResult* table_names) {
121  TGetTablesParams params;
122  params.__set_db(db);
123  if (pattern != NULL) params.__set_pattern(*pattern);
124  if (session != NULL) params.__set_session(*session);
125  return JniUtil::CallJniMethod(fe_, get_table_names_id_, params, table_names);
126 }
127 
128 Status Frontend::GetDbNames(const string* pattern, const TSessionState* session,
129  TGetDbsResult* db_names) {
130  TGetDbsParams params;
131  if (pattern != NULL) params.__set_pattern(*pattern);
132  if (session != NULL) params.__set_session(*session);
133  return JniUtil::CallJniMethod(fe_, get_db_names_id_, params, db_names);
134 }
135 
136 Status Frontend::GetDataSrcMetadata(const string* pattern,
137  TGetDataSrcsResult* result) {
138  TGetDataSrcsParams params;
139  if (pattern != NULL) params.__set_pattern(*pattern);
140  return JniUtil::CallJniMethod(fe_, get_data_src_metadata_id_, params, result);
141 }
142 
143 Status Frontend::GetStats(const TShowStatsParams& params,
144  TResultSet* result) {
145  return JniUtil::CallJniMethod(fe_, get_stats_id_, params, result);
146 }
147 
148 Status Frontend::GetRolePrivileges(const TShowGrantRoleParams& params,
149  TResultSet* result) {
150  return JniUtil::CallJniMethod(fe_, get_role_privileges_id_, params, result);
151 }
152 
153 Status Frontend::GetFunctions(TFunctionCategory::type fn_category, const string& db,
154  const string* pattern, const TSessionState* session,
155  TGetFunctionsResult* functions) {
156  TGetFunctionsParams params;
157  params.__set_category(fn_category);
158  params.__set_db(db);
159  if (pattern != NULL) params.__set_pattern(*pattern);
160  if (session != NULL) params.__set_session(*session);
161  return JniUtil::CallJniMethod(fe_, get_functions_id_, params, functions);
162 }
163 
164 Status Frontend::ShowRoles(const TShowRolesParams& params, TShowRolesResult* result) {
165  return JniUtil::CallJniMethod(fe_, show_roles_id_, params, result);
166 }
167 
168 Status Frontend::GetCatalogObject(const TCatalogObject& req,
169  TCatalogObject* resp) {
171 }
172 
174  const TQueryCtx& query_ctx, TExecRequest* result) {
175  return JniUtil::CallJniMethod(fe_, create_exec_request_id_, query_ctx, result);
176 }
177 
179  const TQueryCtx& query_ctx, string* explain_string) {
180  return JniUtil::CallJniMethod(fe_, get_explain_plan_id_, query_ctx, explain_string);
181 }
182 
184  // Use FE to check Hadoop config setting
185  // TODO: check OS setting
186  stringstream ss;
187  JNIEnv* jni_env = getJNIEnv();
188  JniLocalFrame jni_frame;
189  RETURN_IF_ERROR(jni_frame.push(jni_env));
190  jstring error_string =
191  static_cast<jstring>(jni_env->CallObjectMethod(fe_, check_config_id_));
192  RETURN_ERROR_IF_EXC(jni_env);
193  jboolean is_copy;
194  const char *str = jni_env->GetStringUTFChars(error_string, &is_copy);
195  RETURN_ERROR_IF_EXC(jni_env);
196  ss << str;
197  jni_env->ReleaseStringUTFChars(error_string, str);
198  RETURN_ERROR_IF_EXC(jni_env);
199 
200  if (ss.str().size() > 0) {
201  return Status(ss.str());
202  }
203  return Status::OK;
204 }
205 
206 Status Frontend::ExecHiveServer2MetadataOp(const TMetadataOpRequest& request,
207  TResultSet* result) {
208  return JniUtil::CallJniMethod(fe_, exec_hs2_metadata_op_id_, request, result);
209 }
210 
211 Status Frontend::GetAllHadoopConfigs(TGetAllHadoopConfigsResponse* result) {
213 }
214 
215 Status Frontend::GetHadoopConfig(const TGetHadoopConfigRequest& request,
216  TGetHadoopConfigResponse* response) {
217  return JniUtil::CallJniMethod(fe_, get_hadoop_config_id_, request, response);
218 }
219 
220 Status Frontend::LoadData(const TLoadDataReq& request, TLoadDataResp* response) {
221  return JniUtil::CallJniMethod(fe_, load_table_data_id_, request, response);
222 }
223 
225  return !status.ok() && status.GetDetail().find("AuthorizationException") == 0;
226 }
227 
229  JNIEnv* jni_env = getJNIEnv();
230  JniLocalFrame jni_frame;
231  RETURN_IF_ERROR(jni_frame.push(jni_env));
232  jni_env->CallObjectMethod(fe_, set_catalog_initialized_id_);
233  RETURN_ERROR_IF_EXC(jni_env);
234  return Status::OK;
235 }
236 
237 Status Frontend::GetTableFiles(const TShowFilesParams& params, TResultSet* result) {
238  return JniUtil::CallJniMethod(fe_, get_table_files_id_, params, result);
239 }
jmethodID get_catalog_object_id_
Definition: frontend.h:173
jmethodID fe_ctor_
Definition: frontend.h:180
Status ShowRoles(const TShowRolesParams &params, TShowRolesResult *result)
Call FE to get the roles.
Definition: frontend.cc:164
DEFINE_bool(load_catalog_at_startup, false,"if true, load all catalog data at startup")
Status GetStats(const TShowStatsParams &params, TResultSet *result)
Call FE to get the table/column stats.
Definition: frontend.cc:143
jmethodID get_table_files_id_
Definition: frontend.h:179
const std::string GetDetail() const
Definition: status.cc:184
jmethodID load_table_data_id_
Definition: frontend.h:177
jmethodID show_create_table_id_
Definition: frontend.h:168
jmethodID get_explain_plan_id_
Definition: frontend.h:161
DECLARE_int32(non_impala_java_vlog)
TLogLevel::type FlagToTLogLevel(int flag)
DECLARE_string(sentry_config)
Status GetFunctions(TFunctionCategory::type fn_category, const std::string &db, const std::string *pattern, const TSessionState *session, TGetFunctionsResult *functions)
Definition: frontend.cc:153
Status ShowCreateTable(const TTableName &table_name, std::string *response)
Definition: frontend.cc:115
Status GetDataSrcMetadata(const std::string *pattern, TGetDataSrcsResult *result)
Definition: frontend.cc:136
jmethodID get_role_privileges_id_
Definition: frontend.h:175
jmethodID update_catalog_cache_id_
Definition: frontend.h:165
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
static Status LoadJniMethod(JNIEnv *jni_env, const jclass &jni_class, JniMethodDescriptor *descriptor)
Definition: jni-util.cc:193
#define EXIT_IF_EXC(env)
Definition: jni-util.h:85
jmethodID get_functions_id_
Definition: frontend.h:172
Status DescribeTable(const TDescribeTableParams &params, TDescribeTableResult *response)
Definition: frontend.cc:110
jmethodID exec_hs2_metadata_op_id_
Definition: frontend.h:176
Status GetCatalogObject(const TCatalogObject &request, TCatalogObject *response)
Definition: frontend.cc:168
DEFINE_string(server_name,"","The name to use for securing this impalad ""server during authorization. Set to enable authorization. By default, the ""authorization policy will be loaded from the catalog server (via the statestore).""To use a file based authorization policy, set --authorization_policy_file.")
Status GetExplainPlan(const TQueryCtx &query_ctx, std::string *explain_string)
Call FE to get explain plan.
Definition: frontend.cc:178
static Status LocalToGlobalRef(JNIEnv *env, jobject local_ref, jobject *global_ref)
Definition: jni-util.cc:67
Status LoadData(const TLoadDataReq &load_data_request, TLoadDataResp *response)
Definition: frontend.cc:220
jmethodID get_table_names_id_
Definition: frontend.h:166
jmethodID check_config_id_
Definition: frontend.h:164
static bool IsAuthorizationError(const Status &status)
Returns true if the error returned by the FE was due to an AuthorizationException.
Definition: frontend.cc:224
jmethodID describe_table_id_
Definition: frontend.h:167
Status GetRolePrivileges(const TShowGrantRoleParams &params, TResultSet *result)
Call FE to get the privileges granted to a role.
Definition: frontend.cc:148
jmethodID create_exec_request_id_
Definition: frontend.h:160
#define EXIT_IF_ERROR(stmt)
Definition: status.h:248
jmethodID get_stats_id_
Definition: frontend.h:171
Status GetHadoopConfig(const TGetHadoopConfigRequest &request, TGetHadoopConfigResponse *response)
Definition: frontend.cc:215
Status push(JNIEnv *env, int max_local_ref=10)
Definition: jni-util.cc:34
jmethodID get_db_names_id_
Definition: frontend.h:169
jmethodID set_catalog_initialized_id_
Definition: frontend.h:178
Describes one method to look up in a Java object.
Definition: jni-util.h:149
Status GetTableFiles(const TShowFilesParams &params, TResultSet *result)
Call FE to get files info for a table or partition.
Definition: frontend.cc:237
#define RETURN_ERROR_IF_EXC(env)
Definition: jni-util.h:99
Status GetDbNames(const std::string *pattern, const TSessionState *session, TGetDbsResult *table_names)
Definition: frontend.cc:128
jmethodID get_hadoop_configs_id_
Definition: frontend.h:163
Status SetCatalogInitialized()
Definition: frontend.cc:228
jmethodID get_hadoop_config_id_
Definition: frontend.h:162
Status ExecHiveServer2MetadataOp(const TMetadataOpRequest &request, TResultSet *result)
Calls FE to execute HiveServer2 metadata operation.
Definition: frontend.cc:206
static const Status OK
Definition: status.h:87
Status GetTableNames(const std::string &db, const std::string *pattern, const TSessionState *session, TGetTablesResult *table_names)
Definition: frontend.cc:119
Status ValidateSettings()
Validate Hadoop config; requires FE.
Definition: frontend.cc:183
static Status CallJniMethod(const jobject &obj, const jmethodID &method, const T &arg)
Definition: jni-util.h:231
Status UpdateCatalogCache(const TUpdateCatalogCacheRequest &req, TUpdateCatalogCacheResponse *resp)
Definition: frontend.cc:105
jmethodID get_data_src_metadata_id_
Definition: frontend.h:170
bool ok() const
Definition: status.h:172
Status GetExecRequest(const TQueryCtx &query_ctx, TExecRequest *result)
Call FE to get TExecRequest.
Definition: frontend.cc:173
Status GetAllHadoopConfigs(TGetAllHadoopConfigsResponse *result)
Returns all Hadoop configurations in key, value form in result.
Definition: frontend.cc:211
jclass fe_class_
Descriptor of Java Frontend class itself, used to create a new instance.
Definition: frontend.h:157
jmethodID show_roles_id_
Definition: frontend.h:174
JNIEnv * getJNIEnv(void)
C linkage for helper functions in hdfsJniHelper.h.