Search in sources :

Example 16 with DatabaseNotFoundException

use of com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException in project metacat by Netflix.

the class S3ConnectorDatabaseService method get.

@Override
public DatabaseInfo get(@Nonnull final ConnectorRequestContext context, @Nonnull final QualifiedName name) {
    final String databaseName = name.getDatabaseName();
    Preconditions.checkNotNull(databaseName, "Database name is null");
    log.debug("Get database {}", name);
    final Database database = databaseDao.getBySourceDatabaseName(catalogName, databaseName);
    if (database == null) {
        throw new DatabaseNotFoundException(name);
    }
    return infoConverter.toDatabaseInfo(QualifiedName.ofCatalog(catalogName), database);
}
Also used : DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) Database(com.netflix.metacat.connector.s3.model.Database)

Example 17 with DatabaseNotFoundException

use of com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException in project metacat by Netflix.

the class ElasticSearchMetacatRefresh method deleteUnmarkedEntities.

private void deleteUnmarkedEntities(final List<QualifiedName> qNames, final List<QualifiedName> excludeQualifiedNames) {
    log.info("Start: Delete unmarked entities");
    //
    // get unmarked qualified names
    // check if it not exists
    // delete
    //
    elasticSearchUtil.refresh();
    final MetacatRequestContext context = new MetacatRequestContext("admin", "metacat-refresh", null, null, null);
    final List<DatabaseDto> unmarkedDatabaseDtos = elasticSearchUtil.getQualifiedNamesByMarkerByNames("database", qNames, refreshMarker, excludeQualifiedNames, DatabaseDto.class);
    if (!unmarkedDatabaseDtos.isEmpty()) {
        if (unmarkedDatabaseDtos.size() <= config.getElasticSearchThresholdUnmarkedDatabasesDelete()) {
            log.info("Start: Delete unmarked databases({})", unmarkedDatabaseDtos.size());
            final List<String> unmarkedDatabaseNames = Lists.newArrayList();
            final List<DatabaseDto> deleteDatabaseDtos = unmarkedDatabaseDtos.stream().filter(databaseDto -> {
                boolean result = false;
                try {
                    unmarkedDatabaseNames.add(databaseDto.getName().toString());
                    final DatabaseDto dto = databaseService.get(databaseDto.getName(), false);
                    if (dto == null) {
                        result = true;
                    }
                } catch (DatabaseNotFoundException ignored) {
                    result = true;
                } catch (Exception ignored) {
                }
                return result;
            }).collect(Collectors.toList());
            log.info("Unmarked databases({}): {}", unmarkedDatabaseNames.size(), unmarkedDatabaseNames);
            log.info("Deleting databases({})", deleteDatabaseDtos.size());
            if (!deleteDatabaseDtos.isEmpty()) {
                final List<QualifiedName> deleteDatabaseQualifiedNames = deleteDatabaseDtos.stream().map(DatabaseDto::getName).collect(Collectors.toList());
                final List<String> deleteDatabaseNames = deleteDatabaseQualifiedNames.stream().map(QualifiedName::toString).collect(Collectors.toList());
                log.info("Deleting databases({}): {}", deleteDatabaseNames.size(), deleteDatabaseNames);
                userMetadataService.deleteDefinitionMetadatas(deleteDatabaseQualifiedNames);
                elasticSearchUtil.softDelete("database", deleteDatabaseNames, context);
            }
            log.info("End: Delete unmarked databases({})", unmarkedDatabaseDtos.size());
        } else {
            log.info("Count of unmarked databases({}) is more than the threshold {}", unmarkedDatabaseDtos.size(), config.getElasticSearchThresholdUnmarkedDatabasesDelete());
            registry.counter(registry.createId(Metrics.CounterElasticSearchUnmarkedDatabaseThreshholdReached.name())).increment();
        }
    }
    final List<TableDto> unmarkedTableDtos = elasticSearchUtil.getQualifiedNamesByMarkerByNames("table", qNames, refreshMarker, excludeQualifiedNames, TableDto.class);
    if (!unmarkedTableDtos.isEmpty()) {
        if (unmarkedTableDtos.size() <= config.getElasticSearchThresholdUnmarkedTablesDelete()) {
            log.info("Start: Delete unmarked tables({})", unmarkedTableDtos.size());
            final List<String> unmarkedTableNames = Lists.newArrayList();
            final List<TableDto> deleteTableDtos = unmarkedTableDtos.stream().filter(tableDto -> {
                boolean result = false;
                try {
                    unmarkedTableNames.add(tableDto.getName().toString());
                    final Optional<TableDto> dto = tableService.get(tableDto.getName(), false);
                    if (!dto.isPresent()) {
                        result = true;
                    }
                } catch (Exception ignored) {
                }
                return result;
            }).collect(Collectors.toList());
            log.info("Unmarked tables({}): {}", unmarkedTableNames.size(), unmarkedTableNames);
            log.info("Deleting tables({}): {}", deleteTableDtos.size());
            if (!deleteTableDtos.isEmpty()) {
                final List<String> deleteTableNames = deleteTableDtos.stream().map(dto -> dto.getName().toString()).collect(Collectors.toList());
                log.info("Deleting tables({}): {}", deleteTableNames.size(), deleteTableNames);
                userMetadataService.deleteMetadatas("admin", Lists.newArrayList(deleteTableDtos));
                // Publish event. Elasticsearch event handler will take care of updating the index already
                // TODO: Re-evaluate events vs. direct calls for these types of situations like in Genie
                deleteTableDtos.forEach(tableDto -> {
                    tagService.delete(tableDto.getName(), false);
                    this.eventBus.postAsync(new MetacatDeleteTablePostEvent(tableDto.getName(), context, this, tableDto));
                });
            }
            log.info("End: Delete unmarked tables({})", unmarkedTableDtos.size());
        } else {
            log.info("Count of unmarked tables({}) is more than the threshold {}", unmarkedTableDtos.size(), config.getElasticSearchThresholdUnmarkedTablesDelete());
            registry.counter(registry.createId(Metrics.CounterElasticSearchUnmarkedTableThreshholdReached.name())).increment();
        }
    }
    log.info("End: Delete unmarked entities");
}
Also used : UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) CatalogService(com.netflix.metacat.main.services.CatalogService) MetacatEventBus(com.netflix.metacat.common.server.events.MetacatEventBus) SortOrder(com.netflix.metacat.common.dto.SortOrder) MetacatContextManager(com.netflix.metacat.common.server.util.MetacatContextManager) PartitionService(com.netflix.metacat.main.services.PartitionService) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) TagService(com.netflix.metacat.common.server.usermetadata.TagService) DatabaseService(com.netflix.metacat.main.services.DatabaseService) Splitter(com.google.common.base.Splitter) NonNull(lombok.NonNull) Predicate(java.util.function.Predicate) Pageable(com.netflix.metacat.common.dto.Pageable) Set(java.util.Set) CatalogMappingDto(com.netflix.metacat.common.dto.CatalogMappingDto) QualifiedName(com.netflix.metacat.common.QualifiedName) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) CatalogDto(com.netflix.metacat.common.dto.CatalogDto) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) Optional(java.util.Optional) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Sort(com.netflix.metacat.common.dto.Sort) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) MetacatDeleteTablePostEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent) TableDto(com.netflix.metacat.common.dto.TableDto) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Supplier(java.util.function.Supplier) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) TableService(com.netflix.metacat.main.services.TableService) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) Config(com.netflix.metacat.common.server.properties.Config) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Metrics(com.netflix.metacat.common.server.monitoring.Metrics) Functions(com.google.common.base.Functions) Throwables(com.google.common.base.Throwables) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Registry(com.netflix.spectator.api.Registry) Instant(org.joda.time.Instant) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) Optional(java.util.Optional) QualifiedName(com.netflix.metacat.common.QualifiedName) TableDto(com.netflix.metacat.common.dto.TableDto) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) MetacatDeleteTablePostEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)

Example 18 with DatabaseNotFoundException

use of com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException in project metacat by Netflix.

the class DatabaseServiceImpl method get.

@Override
public DatabaseDto get(@Nonnull final QualifiedName name, final boolean includeUserMetadata) {
    validate(name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final MetacatCatalogConfig config = connectorManager.getCatalogConfig(name.getCatalogName());
    final ConnectorDatabaseService service = connectorManager.getDatabaseService(name.getCatalogName());
    final ConnectorTableService tableService = connectorManager.getTableService(name.getCatalogName());
    final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
    final List<QualifiedName> tableNames = tableService.listNames(connectorContext, name, null, null, null);
    List<QualifiedName> viewNames = Collections.emptyList();
    if (config.isIncludeViewsWithTables()) {
        // TODO JdbcMetadata returns ImmutableList.of() for views.  We should change it to fetch views.
        try {
            viewNames = service.listViewNames(connectorContext, name);
        } catch (UnsupportedOperationException ignored) {
        }
    }
    // Check to see if schema exists
    if (tableNames.isEmpty() && viewNames.isEmpty() && !exists(name)) {
        throw new DatabaseNotFoundException(name);
    }
    final DatabaseDto dto = converterUtil.toDatabaseDto(service.get(connectorContext, name));
    dto.setType(connectorManager.getCatalogConfig(name).getType());
    dto.setTables(Stream.concat(tableNames.stream(), viewNames.stream()).map(QualifiedName::getTableName).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList()));
    if (includeUserMetadata) {
        log.info("Populate user metadata for schema {}", name);
        userMetadataService.populateMetadata(dto);
    }
    return dto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorDatabaseService(com.netflix.metacat.common.server.connectors.ConnectorDatabaseService) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) MetacatCatalogConfig(com.netflix.metacat.main.spi.MetacatCatalogConfig) QualifiedName(com.netflix.metacat.common.QualifiedName) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext)

Example 19 with DatabaseNotFoundException

use of com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException in project metacat by Netflix.

the class HiveConnectorTableService method listNames.

/**
     * {@inheritDoc}.
     */
@Override
public List<QualifiedName> listNames(@Nonnull @NonNull final ConnectorContext requestContext, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    try {
        final List<QualifiedName> qualifiedNames = Lists.newArrayList();
        final String tableFilter = (prefix != null && prefix.isTableDefinition()) ? prefix.getTableName() : null;
        for (String tableName : metacatHiveClient.getAllTables(name.getDatabaseName())) {
            if (tableFilter == null || tableName.startsWith(tableFilter)) {
                final QualifiedName qualifiedName = QualifiedName.ofTable(name.getCatalogName(), name.getDatabaseName(), tableName);
                if (prefix != null && !qualifiedName.toString().startsWith(prefix.toString())) {
                    continue;
                }
                qualifiedNames.add(qualifiedName);
            }
        }
        ////supporting sort by qualified name only
        if (sort != null) {
            ConnectorUtils.sort(qualifiedNames, sort, Comparator.comparing(QualifiedName::toString));
        }
        return ConnectorUtils.paginate(qualifiedNames, pageable);
    } catch (MetaException exception) {
        throw new InvalidMetaException(name, exception);
    } catch (NoSuchObjectException exception) {
        throw new DatabaseNotFoundException(name, exception);
    } catch (TException exception) {
        throw new ConnectorException(String.format("Failed listNames hive table %s", name), exception);
    }
}
Also used : TException(org.apache.thrift.TException) QualifiedName(com.netflix.metacat.common.QualifiedName) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)

Example 20 with DatabaseNotFoundException

use of com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException in project metacat by Netflix.

the class CassandraConnectorTableService method get.

/**
     * {@inheritDoc}
     */
@Override
public TableInfo get(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName name) {
    final String keyspace = name.getDatabaseName();
    final String table = name.getTableName();
    log.debug("Attempting to get metadata for Cassandra table {}.{} for request {}", keyspace, table, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(name);
        }
        final TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
        if (tableMetadata == null) {
            throw new TableNotFoundException(name);
        }
        final TableInfo tableInfo = this.getTableInfo(name, tableMetadata);
        log.debug("Successfully got metadata for Cassandra table {}.{} for request {}", keyspace, table, context);
        return tableInfo;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Aggregations

DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)33 QualifiedName (com.netflix.metacat.common.QualifiedName)19 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)10 DriverException (com.datastax.driver.core.exceptions.DriverException)10 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)10 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)8 Database (com.netflix.metacat.connector.s3.model.Database)8 TableMetadata (com.datastax.driver.core.TableMetadata)6 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)6 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)6 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)6 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)6 TException (org.apache.thrift.TException)6 TableAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)5 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)5 Strings (com.google.common.base.Strings)4 ImmutableList (com.google.common.collect.ImmutableList)4 Lists (com.google.common.collect.Lists)4 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)4 Pageable (com.netflix.metacat.common.dto.Pageable)4