Search in sources :

Example 11 with DatabaseNotFoundException

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

the class CassandraConnectorDatabaseService method get.

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

Example 12 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 13 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 14 with DatabaseNotFoundException

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

the class S3ConnectorDatabaseService method delete.

@Override
public void delete(@Nonnull final ConnectorContext context, @Nonnull final QualifiedName name) {
    log.debug("Start: Delete database {}", name);
    final String databaseName = name.getDatabaseName();
    Preconditions.checkNotNull(databaseName, "Database name is null");
    final Database database = databaseDao.getBySourceDatabaseName(catalogName, databaseName);
    if (database == null) {
        throw new DatabaseNotFoundException(name);
    } else if (database.getTables() != null && !database.getTables().isEmpty()) {
        throw new ConnectorException("Database " + databaseName + " is not empty. One or more tables exist.", null);
    }
    databaseDao.delete(database);
    log.debug("End: Delete database {}", name);
}
Also used : DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) Database(com.netflix.metacat.connector.s3.model.Database)

Aggregations

DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)14 QualifiedName (com.netflix.metacat.common.QualifiedName)8 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)5 DriverException (com.datastax.driver.core.exceptions.DriverException)5 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 TableMetadata (com.datastax.driver.core.TableMetadata)3 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)3 TableAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)3 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)3 Database (com.netflix.metacat.connector.s3.model.Database)3 Strings (com.google.common.base.Strings)2 ImmutableList (com.google.common.collect.ImmutableList)2 Lists (com.google.common.collect.Lists)2 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)2 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)2 Pageable (com.netflix.metacat.common.dto.Pageable)2 Sort (com.netflix.metacat.common.dto.Sort)2 ConnectorContext (com.netflix.metacat.common.server.connectors.ConnectorContext)2 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)2 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)2