Search in sources :

Example 51 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class MViewServiceEventHandler method metacatRenameTablePostEventHandler.

/**
 * Subscriber.
 *
 * @param event event
 */
@EventListener
public void metacatRenameTablePostEventHandler(final MetacatRenameTablePostEvent event) {
    if (!event.isMView()) {
        final QualifiedName oldName = event.getOldTable().getName();
        final QualifiedName newName = event.getCurrentTable().getName();
        final List<NameDateDto> views = mViewService.list(oldName);
        if (views != null && !views.isEmpty()) {
            views.forEach(view -> {
                final QualifiedName newViewName = QualifiedName.ofView(oldName.getCatalogName(), oldName.getDatabaseName(), newName.getTableName(), view.getName().getViewName());
                mViewService.rename(view.getName(), newViewName);
            });
        }
    }
}
Also used : QualifiedName(com.netflix.metacat.common.QualifiedName) NameDateDto(com.netflix.metacat.common.NameDateDto) EventListener(org.springframework.context.event.EventListener)

Example 52 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class MViewServiceEventHandler method metacatDeleteTablePostEventHandler.

/**
 * Subscriber.
 *
 * @param event event
 */
@EventListener
public void metacatDeleteTablePostEventHandler(final MetacatDeleteTablePostEvent event) {
    if (config.canCascadeViewsMetadataOnTableDelete() && !event.isMView()) {
        final QualifiedName name = event.getTable().getName();
        try {
            // delete views associated with this table
            final List<NameDateDto> viewNames = mViewService.list(name);
            viewNames.forEach(viewName -> mViewService.deleteAndReturn(viewName.getName()));
        } catch (Exception e) {
            log.warn("Failed cleaning mviews after deleting table {}", name);
        }
        // delete table partitions metadata
        try {
            final List<QualifiedName> names = userMetadataService.getDescendantDefinitionNames(name);
            if (names != null && !names.isEmpty()) {
                userMetadataService.deleteDefinitionMetadata(names);
            }
        } catch (Exception e) {
            log.warn("Failed cleaning partition definition metadata after deleting table {}", name);
        }
    }
}
Also used : QualifiedName(com.netflix.metacat.common.QualifiedName) NameDateDto(com.netflix.metacat.common.NameDateDto) EventListener(org.springframework.context.event.EventListener)

Example 53 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class MetadataService method cleanUpDeletedDataMetadata.

/**
 * Deletes all the data metadata marked for deletion.
 */
public void cleanUpDeletedDataMetadata() {
    // Get the data metadata that were marked deleted a number of days back
    // Check if the uri is being used
    // If uri is not used then delete the entry from data_metadata
    log.info("Start deleting data metadata");
    try {
        final DateTime priorTo = DateTime.now().minusDays(config.getDataMetadataDeleteMarkerLifetimeInDays());
        final int limit = 100000;
        final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
        while (true) {
            final List<String> urisToDelete = userMetadataService.getDeletedDataMetadataUris(priorTo.toDate(), 0, limit);
            log.info("Count of deleted marked data metadata: {}", urisToDelete.size());
            if (urisToDelete.size() > 0) {
                final List<String> uris = urisToDelete.parallelStream().filter(uri -> !uri.contains("=")).map(userMetadataService::getDescendantDataUris).flatMap(Collection::stream).collect(Collectors.toList());
                uris.addAll(urisToDelete);
                log.info("Count of deleted marked data metadata (including descendants) : {}", uris.size());
                final List<List<String>> subListsUris = Lists.partition(uris, 1000);
                subListsUris.parallelStream().forEach(subUris -> {
                    MetacatContextManager.setContext(metacatRequestContext);
                    final Map<String, List<QualifiedName>> uriPartitionQualifiedNames = partitionService.getQualifiedNames(subUris, false);
                    final Map<String, List<QualifiedName>> uriTableQualifiedNames = tableService.getQualifiedNames(subUris, false);
                    final Map<String, List<QualifiedName>> uriQualifiedNames = Stream.concat(uriPartitionQualifiedNames.entrySet().stream(), uriTableQualifiedNames.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
                        final List<QualifiedName> subNames = Lists.newArrayList(a);
                        subNames.addAll(b);
                        return subNames;
                    }));
                    final List<String> canDeleteMetadataForUris = subUris.parallelStream().filter(s -> !Strings.isNullOrEmpty(s)).filter(s -> uriQualifiedNames.get(s) == null || uriQualifiedNames.get(s).size() == 0).collect(Collectors.toList());
                    log.info("Start deleting data metadata: {}", canDeleteMetadataForUris.size());
                    userMetadataService.deleteDataMetadata(canDeleteMetadataForUris);
                    userMetadataService.deleteDataMetadataDeletes(subUris);
                    MetacatContextManager.removeContext();
                });
            }
            if (urisToDelete.size() < limit) {
                break;
            }
        }
    } catch (Exception e) {
        registry.counter(Metrics.CounterDeleteMetaData.getMetricName()).increment();
        log.warn("Failed deleting data metadata", e);
    }
    log.info("End deleting data metadata");
}
Also used : UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Getter(lombok.Getter) MetacatContextManager(com.netflix.metacat.common.server.util.MetacatContextManager) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) TagService(com.netflix.metacat.common.server.usermetadata.TagService) ThreadServiceManager(com.netflix.metacat.common.server.util.ThreadServiceManager) BaseDto(com.netflix.metacat.common.dto.BaseDto) Map(java.util.Map) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) Config(com.netflix.metacat.common.server.properties.Config) Metrics(com.netflix.metacat.common.server.monitoring.Metrics) Function(com.google.common.base.Function) Collection(java.util.Collection) DateTime(org.joda.time.DateTime) QualifiedName(com.netflix.metacat.common.QualifiedName) Collectors(java.util.stream.Collectors) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) Registry(com.netflix.spectator.api.Registry) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) List(java.util.List) Map(java.util.Map) DateTime(org.joda.time.DateTime) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException)

Example 54 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class CatalogServiceImpl method get.

/**
 * {@inheritDoc}
 */
@Nonnull
@Override
public CatalogDto get(final QualifiedName name) {
    final Set<MetacatCatalogConfig> configs = connectorManager.getCatalogConfigs(name.getCatalogName());
    final CatalogDto result = new CatalogDto();
    result.setName(name);
    final ConnectorRequestContext context = converterUtil.toConnectorContext(MetacatContextManager.getContext());
    final List<String> databases = Lists.newArrayList();
    configs.forEach(config -> {
        QualifiedName qName = name;
        if (config.getSchemaWhitelist().isEmpty()) {
            result.setType(config.getType());
        } else {
            qName = QualifiedName.ofDatabase(name.getCatalogName(), config.getSchemaWhitelist().get(0));
        }
        databases.addAll(connectorManager.getDatabaseService(qName).listNames(context, name, null, null, null).stream().map(QualifiedName::getDatabaseName).filter(s -> config.getSchemaBlacklist().isEmpty() || !config.getSchemaBlacklist().contains(s)).filter(s -> config.getSchemaWhitelist().isEmpty() || config.getSchemaWhitelist().contains(s)).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList()));
    });
    result.setDatabases(databases);
    userMetadataService.populateMetadata(result, false);
    return result;
}
Also used : UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) CatalogService(com.netflix.metacat.main.services.CatalogService) CreateCatalogDto(com.netflix.metacat.common.dto.CreateCatalogDto) MetacatEventBus(com.netflix.metacat.common.server.events.MetacatEventBus) ConverterUtil(com.netflix.metacat.common.server.converter.ConverterUtil) MetacatContextManager(com.netflix.metacat.common.server.util.MetacatContextManager) MetacatUpdateDatabasePreEvent(com.netflix.metacat.common.server.events.MetacatUpdateDatabasePreEvent) Set(java.util.Set) CatalogMappingDto(com.netflix.metacat.common.dto.CatalogMappingDto) QualifiedName(com.netflix.metacat.common.QualifiedName) MetacatCatalogConfig(com.netflix.metacat.common.server.spi.MetacatCatalogConfig) MetacatUpdateDatabasePostEvent(com.netflix.metacat.common.server.events.MetacatUpdateDatabasePostEvent) Collectors(java.util.stream.Collectors) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) CatalogDto(com.netflix.metacat.common.dto.CatalogDto) List(java.util.List) Lists(com.google.common.collect.Lists) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorManager(com.netflix.metacat.main.manager.ConnectorManager) Nonnull(javax.annotation.Nonnull) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) CreateCatalogDto(com.netflix.metacat.common.dto.CreateCatalogDto) CatalogDto(com.netflix.metacat.common.dto.CatalogDto) MetacatCatalogConfig(com.netflix.metacat.common.server.spi.MetacatCatalogConfig) QualifiedName(com.netflix.metacat.common.QualifiedName) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Nonnull(javax.annotation.Nonnull)

Example 55 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class DatabaseServiceImpl method get.

/**
 * {@inheritDoc}
 */
@Override
public DatabaseDto get(final QualifiedName name, final GetDatabaseServiceParameters getDatabaseServiceParameters) {
    validate(name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final MetacatCatalogConfig config = connectorManager.getCatalogConfig(name);
    final ConnectorDatabaseService service = connectorManager.getDatabaseService(name);
    final ConnectorTableService tableService = connectorManager.getTableService(name);
    final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
    final DatabaseDto dto = converterUtil.toDatabaseDto(service.get(connectorRequestContext, name));
    dto.setType(config.getType());
    if (getDatabaseServiceParameters.isIncludeTableNames()) {
        final List<QualifiedName> tableNames = tableService.listNames(connectorRequestContext, 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(connectorRequestContext, name);
            } catch (UnsupportedOperationException ignored) {
            }
        }
        // Check to see if schema exists
        if (tableNames.isEmpty() && viewNames.isEmpty() && !exists(name)) {
            throw new DatabaseNotFoundException(name);
        }
        dto.setTables(Stream.concat(tableNames.stream(), viewNames.stream()).map(QualifiedName::getTableName).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList()));
    }
    if (getDatabaseServiceParameters.isIncludeUserMetadata()) {
        log.info("Populate user metadata for schema {}", name);
        userMetadataService.populateMetadata(dto, getDatabaseServiceParameters.isDisableOnReadMetadataIntercetor());
    }
    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.common.server.spi.MetacatCatalogConfig) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) QualifiedName(com.netflix.metacat.common.QualifiedName) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext)

Aggregations

QualifiedName (com.netflix.metacat.common.QualifiedName)144 List (java.util.List)52 Lists (com.google.common.collect.Lists)44 Collectors (java.util.stream.Collectors)41 Map (java.util.Map)38 Slf4j (lombok.extern.slf4j.Slf4j)36 Strings (com.google.common.base.Strings)35 Nullable (javax.annotation.Nullable)33 Pageable (com.netflix.metacat.common.dto.Pageable)29 Sort (com.netflix.metacat.common.dto.Sort)29 Nonnull (javax.annotation.Nonnull)29 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)28 TableDto (com.netflix.metacat.common.dto.TableDto)27 Maps (com.google.common.collect.Maps)25 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)25 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)24 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)24 Optional (java.util.Optional)22 Registry (com.netflix.spectator.api.Registry)21 Set (java.util.Set)21