Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JniCatalog.java
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 package com.cloudera.impala.service;
16 
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.UUID;
20 
21 import org.apache.thrift.TException;
22 import org.apache.thrift.TSerializer;
23 import org.apache.thrift.protocol.TBinaryProtocol;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 
35 import com.cloudera.impala.thrift.TCatalogObject;
36 import com.cloudera.impala.thrift.TDdlExecRequest;
37 import com.cloudera.impala.thrift.TFunction;
38 import com.cloudera.impala.thrift.TGetAllCatalogObjectsResponse;
39 import com.cloudera.impala.thrift.TGetDbsParams;
40 import com.cloudera.impala.thrift.TGetDbsResult;
41 import com.cloudera.impala.thrift.TGetFunctionsRequest;
42 import com.cloudera.impala.thrift.TGetFunctionsResponse;
43 import com.cloudera.impala.thrift.TGetTablesParams;
44 import com.cloudera.impala.thrift.TGetTablesResult;
45 import com.cloudera.impala.thrift.TLogLevel;
46 import com.cloudera.impala.thrift.TPrioritizeLoadRequest;
47 import com.cloudera.impala.thrift.TResetMetadataRequest;
48 import com.cloudera.impala.thrift.TSentryAdminCheckRequest;
49 import com.cloudera.impala.thrift.TUniqueId;
50 import com.cloudera.impala.thrift.TUpdateCatalogRequest;
52 import com.google.common.base.Preconditions;
53 import com.google.common.base.Strings;
54 
59 public class JniCatalog {
60  private final static Logger LOG = LoggerFactory.getLogger(JniCatalog.class);
61  private final static TBinaryProtocol.Factory protocolFactory_ =
62  new TBinaryProtocol.Factory();
65 
66  // A unique identifier for this instance of the Catalog Service.
67  private static final TUniqueId catalogServiceId_ = generateId();
68 
69  private static TUniqueId generateId() {
70  UUID uuid = UUID.randomUUID();
71  return new TUniqueId(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
72  }
73 
74  public JniCatalog(boolean loadInBackground, int numMetadataLoadingThreads,
75  String sentryServiceConfig, int impalaLogLevel, int otherLogLevel)
76  throws InternalException {
77  Preconditions.checkArgument(numMetadataLoadingThreads > 0);
78  // This trick saves having to pass a TLogLevel enum, which is an object and more
79  // complex to pass through JNI.
80  GlogAppender.Install(TLogLevel.values()[impalaLogLevel],
81  TLogLevel.values()[otherLogLevel]);
82 
83  // Check if the Sentry Service is configured. If so, create a configuration object.
84  SentryConfig sentryConfig = null;
85  if (!Strings.isNullOrEmpty(sentryServiceConfig)) {
86  sentryConfig = new SentryConfig(sentryServiceConfig);
87  sentryConfig.loadConfig();
88  }
89  LOG.info(JniUtil.getJavaVersion());
90 
91  catalog_ = new CatalogServiceCatalog(loadInBackground,
92  numMetadataLoadingThreads, sentryConfig, getServiceId());
93  try {
94  catalog_.reset();
95  } catch (CatalogException e) {
96  LOG.error("Error initialializing Catalog. Please run 'invalidate metadata'", e);
97  }
99  }
100 
101  public static TUniqueId getServiceId() { return catalogServiceId_; }
102 
106  public byte[] getCatalogObjects(long from_version) throws ImpalaException, TException {
107  TGetAllCatalogObjectsResponse resp =
108  catalog_.getCatalogObjects(from_version);
109  TSerializer serializer = new TSerializer(protocolFactory_);
110  return serializer.serialize(resp);
111  }
112 
116  public long getCatalogVersion() {
117  return catalog_.getCatalogVersion();
118  }
119 
123  public byte[] execDdl(byte[] thriftDdlExecReq) throws ImpalaException {
124  TDdlExecRequest params = new TDdlExecRequest();
125  JniUtil.deserializeThrift(protocolFactory_, params, thriftDdlExecReq);
126  TSerializer serializer = new TSerializer(protocolFactory_);
127  try {
128  return serializer.serialize(catalogOpExecutor_.execDdlRequest(params));
129  } catch (TException e) {
130  throw new InternalException(e.getMessage());
131  }
132  }
133 
137  public byte[] resetMetadata(byte[] thriftResetMetadataReq)
138  throws ImpalaException, TException {
139  TResetMetadataRequest req = new TResetMetadataRequest();
140  JniUtil.deserializeThrift(protocolFactory_, req, thriftResetMetadataReq);
141  TSerializer serializer = new TSerializer(protocolFactory_);
142  return serializer.serialize(catalogOpExecutor_.execResetMetadata(req));
143  }
144 
150  public byte[] getDbNames(byte[] thriftGetTablesParams) throws ImpalaException,
151  TException {
152  TGetDbsParams params = new TGetDbsParams();
153  JniUtil.deserializeThrift(protocolFactory_, params, thriftGetTablesParams);
154  TGetDbsResult result = new TGetDbsResult();
155  result.setDbs(catalog_.getDbNames(null));
156  TSerializer serializer = new TSerializer(protocolFactory_);
157  return serializer.serialize(result);
158  }
159 
165  public byte[] getTableNames(byte[] thriftGetTablesParams) throws ImpalaException,
166  TException {
167  TGetTablesParams params = new TGetTablesParams();
168  JniUtil.deserializeThrift(protocolFactory_, params, thriftGetTablesParams);
169  List<String> tables = catalog_.getTableNames(params.db, params.pattern);
170  TGetTablesResult result = new TGetTablesResult();
171  result.setTables(tables);
172  TSerializer serializer = new TSerializer(protocolFactory_);
173  return serializer.serialize(result);
174  }
175 
179  public byte[] getCatalogObject(byte[] thriftParams) throws ImpalaException,
180  TException {
181  TCatalogObject objectDescription = new TCatalogObject();
182  JniUtil.deserializeThrift(protocolFactory_, objectDescription, thriftParams);
183  TSerializer serializer = new TSerializer(protocolFactory_);
184  return serializer.serialize(catalog_.getTCatalogObject(objectDescription));
185  }
186 
190  public byte[] getFunctions(byte[] thriftParams) throws ImpalaException,
191  TException {
192  TGetFunctionsRequest request = new TGetFunctionsRequest();
193  JniUtil.deserializeThrift(protocolFactory_, request, thriftParams);
194  TSerializer serializer = new TSerializer(protocolFactory_);
195  if (!request.isSetDb_name()) {
196  throw new InternalException("Database name must be set in call to " +
197  "getFunctions()");
198  }
199 
200  // Get all the functions and convert them to their Thrift representation.
201  List<Function> fns = catalog_.getFunctions(request.getDb_name());
202  TGetFunctionsResponse response = new TGetFunctionsResponse();
203  response.setFunctions(new ArrayList<TFunction>(fns.size()));
204  for (Function fn: fns) {
205  response.addToFunctions(fn.toThrift());
206  }
207 
208  return serializer.serialize(response);
209  }
210 
211  public void prioritizeLoad(byte[] thriftLoadReq) throws ImpalaException,
212  TException {
213  TPrioritizeLoadRequest request = new TPrioritizeLoadRequest();
214  JniUtil.deserializeThrift(protocolFactory_, request, thriftLoadReq);
215  catalog_.prioritizeLoad(request.getObject_descs());
216  }
217 
223  public void checkUserSentryAdmin(byte[] thriftReq) throws ImpalaException,
224  TException {
225  TSentryAdminCheckRequest request = new TSentryAdminCheckRequest();
226  JniUtil.deserializeThrift(protocolFactory_, request, thriftReq);
227  catalog_.getSentryProxy().checkUserSentryAdmin(
228  new User(request.getHeader().getRequesting_user()));
229  }
230 
235  public byte[] updateCatalog(byte[] thriftUpdateCatalog) throws ImpalaException,
236  TException {
237  TUpdateCatalogRequest request = new TUpdateCatalogRequest();
238  JniUtil.deserializeThrift(protocolFactory_, request, thriftUpdateCatalog);
239  TSerializer serializer = new TSerializer(protocolFactory_);
240  return serializer.serialize(catalogOpExecutor_.updateCatalog(request));
241  }
242 }
byte[] getDbNames(byte[] thriftGetTablesParams)
byte[] execDdl(byte[] thriftDdlExecReq)
byte[] getCatalogObjects(long from_version)
byte[] getCatalogObject(byte[] thriftParams)
void prioritizeLoad(byte[] thriftLoadReq)
byte[] getFunctions(byte[] thriftParams)
byte[] resetMetadata(byte[] thriftResetMetadataReq)
byte[] updateCatalog(byte[] thriftUpdateCatalog)
JniCatalog(boolean loadInBackground, int numMetadataLoadingThreads, String sentryServiceConfig, int impalaLogLevel, int otherLogLevel)
Definition: JniCatalog.java:74
static final TBinaryProtocol.Factory protocolFactory_
Definition: JniCatalog.java:61
final CatalogServiceCatalog catalog_
Definition: JniCatalog.java:63
void checkUserSentryAdmin(byte[] thriftReq)
final CatalogOpExecutor catalogOpExecutor_
Definition: JniCatalog.java:64
byte[] getTableNames(byte[] thriftGetTablesParams)
static final TUniqueId catalogServiceId_
Definition: JniCatalog.java:67