Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Catalog.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.catalog;
16 
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.atomic.AtomicReference;
21 
22 import org.apache.log4j.Logger;
23 
26 import com.cloudera.impala.thrift.TCatalogObject;
27 import com.cloudera.impala.thrift.TFunction;
28 import com.cloudera.impala.thrift.TPartitionKeyValue;
29 import com.cloudera.impala.thrift.TTableName;
31 import com.google.common.base.Joiner;
32 import com.google.common.base.Preconditions;
33 import com.google.common.collect.Lists;
34 
53 public abstract class Catalog {
54  private static final Logger LOG = Logger.getLogger(Catalog.class);
55 
56  // Initial catalog version.
57  public final static long INITIAL_CATALOG_VERSION = 0L;
58  public static final String DEFAULT_DB = "default";
59  private static final int META_STORE_CLIENT_POOL_SIZE = 5;
60 
61  public static final String BUILTINS_DB = "_impala_builtins";
62 
64 
65  // Cache of authorization policy metadata. Populated from data retried from the
66  // Sentry Service, if configured.
68 
69  // Thread safe cache of database metadata. Uses an AtomicReference so reset()
70  // operations can atomically swap dbCache_ references.
71  // TODO: Update this to use a CatalogObjectCache?
72  protected AtomicReference<ConcurrentHashMap<String, Db>> dbCache_ =
73  new AtomicReference<ConcurrentHashMap<String, Db>>(
74  new ConcurrentHashMap<String, Db>());
75 
76  // DB that contains all builtins
77  private static Db builtinsDb_;
78 
79  // Cache of data sources.
80  protected final CatalogObjectCache<DataSource> dataSources_;
81 
82  // Cache of known HDFS cache pools. Allows for checking the existence
83  // of pools without hitting HDFS.
84  protected final CatalogObjectCache<HdfsCachePool> hdfsCachePools_ =
85  new CatalogObjectCache<HdfsCachePool>(false);
86 
91  public Catalog(boolean initMetastoreClientPool) {
92  if (initMetastoreClientPool) {
93  metaStoreClientPool_.addClients(META_STORE_CLIENT_POOL_SIZE);
94  }
95  dataSources_ = new CatalogObjectCache<DataSource>();
96  builtinsDb_ = new BuiltinsDb(BUILTINS_DB, this);
97  addDb(builtinsDb_);
98  }
99 
100  public Db getBuiltinsDb() { return builtinsDb_; }
101 
107  public Db addDb(Db db) {
108  return dbCache_.get().put(db.getName().toLowerCase(), db);
109  }
110 
115  public Db getDb(String dbName) {
116  Preconditions.checkState(dbName != null && !dbName.isEmpty(),
117  "Null or empty database name given as argument to Catalog.getDb");
118  return dbCache_.get().get(dbName.toLowerCase());
119  }
120 
126  public Db removeDb(String dbName) {
127  return dbCache_.get().remove(dbName.toLowerCase());
128  }
129 
136  public List<String> getDbNames(String dbPattern) {
137  return filterStringsByPattern(dbCache_.get().keySet(), dbPattern);
138  }
139 
144  public Table getTable(String dbName, String tableName) throws
146  Db db = getDb(dbName);
147  if (db == null) {
148  throw new DatabaseNotFoundException("Database '" + dbName + "' not found");
149  }
150  return db.getTable(tableName);
151  }
152 
157  public Table removeTable(TTableName tableName) {
158  // Remove the old table name from the cache and add the new table.
159  Db db = getDb(tableName.getDb_name());
160  if (db == null) return null;
161  return db.removeTable(tableName.getTable_name());
162  }
163 
173  public List<String> getTableNames(String dbName, String tablePattern)
175  Preconditions.checkNotNull(dbName);
176  Db db = getDb(dbName);
177  if (db == null) {
178  throw new DatabaseNotFoundException("Database '" + dbName + "' not found");
179  }
180  return filterStringsByPattern(db.getAllTableNames(), tablePattern);
181  }
182 
187  public boolean containsTable(String dbName, String tableName) {
188  Db db = getDb(dbName);
189  return (db == null) ? false : db.containsTable(tableName);
190  }
191 
197  public boolean addDataSource(DataSource dataSource) {
198  return dataSources_.add(dataSource);
199  }
200 
205  public DataSource removeDataSource(String dataSourceName) {
206  Preconditions.checkNotNull(dataSourceName);
207  return dataSources_.remove(dataSourceName.toLowerCase());
208  }
209 
213  public DataSource getDataSource(String dataSourceName) {
214  Preconditions.checkNotNull(dataSourceName);
215  return dataSources_.get(dataSourceName.toLowerCase());
216  }
217 
221  public List<DataSource> getDataSources() {
222  return dataSources_.getValues();
223  }
224 
231  public List<String> getDataSourceNames(String pattern) {
232  return filterStringsByPattern(dataSources_.keySet(), pattern);
233  }
234 
241  public List<DataSource> getDataSources(String pattern) {
242  List<String> names = filterStringsByPattern(dataSources_.keySet(), pattern);
243  List<DataSource> dataSources = Lists.newArrayListWithCapacity(names.size());
244  for (String name: names) {
245  dataSources.add(dataSources_.get(name));
246  }
247  return dataSources;
248  }
249 
259  public boolean addFunction(Function fn) {
260  Db db = getDb(fn.dbName());
261  if (db == null) return false;
262  return db.addFunction(fn);
263  }
264 
270  public Function getFunction(Function desc, Function.CompareMode mode) {
271  Db db = getDb(desc.dbName());
272  if (db == null) return null;
273  return db.getFunction(desc, mode);
274  }
275 
276  public static Function getBuiltin(Function desc, Function.CompareMode mode) {
277  return builtinsDb_.getFunction(desc, mode);
278  }
279 
286  Db db = getDb(desc.dbName());
287  if (db == null) return null;
288  return db.removeFunction(desc);
289  }
290 
296  Db db = getDb(name.getDb());
297  if (db == null) return false;
298  return db.containsFunction(name.getFunction());
299  }
300 
304  public boolean addHdfsCachePool(HdfsCachePool cachePool) {
305  return hdfsCachePools_.add(cachePool);
306  }
307 
312  public HdfsCachePool getHdfsCachePool(String poolName) {
313  return hdfsCachePools_.get(poolName);
314  }
315 
320  public void close() { metaStoreClientPool_.close(); }
321 
322 
326  public MetaStoreClient getMetaStoreClient() { return metaStoreClientPool_.getClient(); }
327 
338  private List<String> filterStringsByPattern(Iterable<String> candidates,
339  String matchPattern) {
340  List<String> filtered = Lists.newArrayList();
341  if (matchPattern == null) {
342  filtered = Lists.newArrayList(candidates);
343  } else {
344  PatternMatcher matcher = PatternMatcher.createHivePatternMatcher(matchPattern);
345  for (String candidate: candidates) {
346  if (matcher.matches(candidate)) filtered.add(candidate);
347  }
348  }
349  Collections.sort(filtered, String.CASE_INSENSITIVE_ORDER);
350  return filtered;
351  }
352 
361  public HdfsPartition getHdfsPartition(String dbName, String tableName,
362  List<TPartitionKeyValue> partitionSpec) throws CatalogException {
363  String partitionNotFoundMsg =
364  "Partition not found: " + Joiner.on(", ").join(partitionSpec);
365  Table table = getTable(dbName, tableName);
366  // This is not an Hdfs table, throw an error.
367  if (!(table instanceof HdfsTable)) {
368  throw new PartitionNotFoundException(partitionNotFoundMsg);
369  }
370  // Get the HdfsPartition object for the given partition spec.
371  HdfsPartition partition =
372  ((HdfsTable) table).getPartitionFromThriftPartitionSpec(partitionSpec);
373  if (partition == null) throw new PartitionNotFoundException(partitionNotFoundMsg);
374  return partition;
375  }
376 
384  public boolean containsHdfsPartition(String dbName, String tableName,
385  List<TPartitionKeyValue> partitionSpec) throws CatalogException {
386  try {
387  return getHdfsPartition(dbName, tableName, partitionSpec) != null;
388  } catch (PartitionNotFoundException e) {
389  return false;
390  }
391  }
392 
399  public TCatalogObject getTCatalogObject(TCatalogObject objectDesc)
400  throws CatalogException {
401  TCatalogObject result = new TCatalogObject();
402  switch (objectDesc.getType()) {
403  case DATABASE: {
404  Db db = getDb(objectDesc.getDb().getDb_name());
405  if (db == null) {
406  throw new CatalogException(
407  "Database not found: " + objectDesc.getDb().getDb_name());
408  }
409  result.setType(db.getCatalogObjectType());
410  result.setCatalog_version(db.getCatalogVersion());
411  result.setDb(db.toThrift());
412  break;
413  }
414  case TABLE:
415  case VIEW: {
416  Table table = getTable(objectDesc.getTable().getDb_name(),
417  objectDesc.getTable().getTbl_name());
418  if (table == null) {
419  throw new CatalogException("Table not found: " +
420  objectDesc.getTable().getTbl_name());
421  }
422  result.setType(table.getCatalogObjectType());
423  result.setCatalog_version(table.getCatalogVersion());
424  result.setTable(table.toThrift());
425  break;
426  }
427  case FUNCTION: {
428  TFunction tfn = objectDesc.getFn();
429  Function desc = Function.fromThrift(tfn);
430  Function fn = getFunction(desc, Function.CompareMode.IS_INDISTINGUISHABLE);
431  if (fn == null) {
432  throw new CatalogException("Function not found: " + tfn);
433  }
434  result.setType(fn.getCatalogObjectType());
435  result.setCatalog_version(fn.getCatalogVersion());
436  result.setFn(fn.toThrift());
437  break;
438  }
439  case DATA_SOURCE: {
440  String dataSrcName = objectDesc.getData_source().getName();
441  DataSource dataSrc = getDataSource(dataSrcName);
442  if (dataSrc == null) {
443  throw new CatalogException("Data source not found: " + dataSrcName);
444  }
445  result.setType(dataSrc.getCatalogObjectType());
446  result.setCatalog_version(dataSrc.getCatalogVersion());
447  result.setData_source(dataSrc.toThrift());
448  break;
449  }
450  case HDFS_CACHE_POOL: {
451  HdfsCachePool pool = getHdfsCachePool(objectDesc.getCache_pool().getPool_name());
452  if (pool == null) {
453  throw new CatalogException(
454  "Hdfs cache pool not found: " + objectDesc.getCache_pool().getPool_name());
455  }
456  result.setType(pool.getCatalogObjectType());
457  result.setCatalog_version(pool.getCatalogVersion());
458  result.setCache_pool(pool.toThrift());
459  break;
460  }
461  case ROLE:
462  Role role = authPolicy_.getRole(objectDesc.getRole().getRole_name());
463  if (role == null) {
464  throw new CatalogException("Role not found: " +
465  objectDesc.getRole().getRole_name());
466  }
467  result.setType(role.getCatalogObjectType());
468  result.setCatalog_version(role.getCatalogVersion());
469  result.setRole(role.toThrift());
470  break;
471  case PRIVILEGE:
472  Role tmpRole = authPolicy_.getRole(objectDesc.getPrivilege().getRole_id());
473  if (tmpRole == null) {
474  throw new CatalogException("No role associated with ID: " +
475  objectDesc.getPrivilege().getRole_id());
476  }
477  for (RolePrivilege p: tmpRole.getPrivileges()) {
478  if (p.getName().equalsIgnoreCase(
479  objectDesc.getPrivilege().getPrivilege_name())) {
480  result.setType(p.getCatalogObjectType());
481  result.setCatalog_version(p.getCatalogVersion());
482  result.setPrivilege(p.toThrift());
483  return result;
484  }
485  }
486  throw new CatalogException(String.format("Role '%s' does not contain " +
487  "privilege: '%s'", tmpRole.getName(),
488  objectDesc.getPrivilege().getPrivilege_name()));
489  default: throw new IllegalStateException(
490  "Unexpected TCatalogObject type: " + objectDesc.getType());
491  }
492  return result;
493  }
494 }
Function getFunction(Function desc, Function.CompareMode mode)
Definition: Catalog.java:270
final MetaStoreClientPool metaStoreClientPool_
Definition: Catalog.java:63
AtomicReference< ConcurrentHashMap< String, Db > > dbCache_
Definition: Catalog.java:72
boolean addDataSource(DataSource dataSource)
Definition: Catalog.java:197
List< RolePrivilege > getPrivileges()
Definition: Role.java:62
List< String > filterStringsByPattern(Iterable< String > candidates, String matchPattern)
Definition: Catalog.java:338
final CatalogObjectCache< DataSource > dataSources_
Definition: Catalog.java:80
final CatalogObjectCache< HdfsCachePool > hdfsCachePools_
Definition: Catalog.java:84
List< String > getTableNames(String dbName, String tablePattern)
Definition: Catalog.java:173
List< DataSource > getDataSources()
Definition: Catalog.java:221
boolean addFunction(Function fn)
Definition: Catalog.java:259
Catalog(boolean initMetastoreClientPool)
Definition: Catalog.java:91
boolean containsFunction(FunctionName name)
Definition: Catalog.java:295
DataSource removeDataSource(String dataSourceName)
Definition: Catalog.java:205
static final String BUILTINS_DB
Definition: Catalog.java:61
List< String > getDataSourceNames(String pattern)
Definition: Catalog.java:231
List< DataSource > getDataSources(String pattern)
Definition: Catalog.java:241
Table removeTable(TTableName tableName)
Definition: Catalog.java:157
Table getTable(String dbName, String tableName)
Definition: Catalog.java:144
static final long INITIAL_CATALOG_VERSION
Definition: Catalog.java:57
HdfsPartition getHdfsPartition(String dbName, String tableName, List< TPartitionKeyValue > partitionSpec)
Definition: Catalog.java:361
Function removeFunction(Function desc)
Definition: Catalog.java:285
DataSource getDataSource(String dataSourceName)
Definition: Catalog.java:213
List< String > getDbNames(String dbPattern)
Definition: Catalog.java:136
boolean addHdfsCachePool(HdfsCachePool cachePool)
Definition: Catalog.java:304
ObjectPool pool
AuthorizationPolicy authPolicy_
Definition: Catalog.java:67
static final int META_STORE_CLIENT_POOL_SIZE
Definition: Catalog.java:59
HdfsCachePool getHdfsCachePool(String poolName)
Definition: Catalog.java:312
static Function getBuiltin(Function desc, Function.CompareMode mode)
Definition: Catalog.java:276
boolean containsHdfsPartition(String dbName, String tableName, List< TPartitionKeyValue > partitionSpec)
Definition: Catalog.java:384
MetaStoreClient getMetaStoreClient()
Definition: Catalog.java:326
boolean containsTable(String dbName, String tableName)
Definition: Catalog.java:187
string name
Definition: cpu-info.cc:50
static final String DEFAULT_DB
Definition: Catalog.java:58
TCatalogObject getTCatalogObject(TCatalogObject objectDesc)
Definition: Catalog.java:399