Search in sources :

Example 26 with MetacatRequestContext

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

the class TableServiceImpl method get.

@Override
public Optional<TableDto> get(@Nonnull final QualifiedName name, final boolean includeInfo, final boolean includeDefinitionMetadata, final boolean includeDataMetadata) {
    validate(name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
    final ConnectorTableService service = connectorManager.getTableService(name.getCatalogName());
    final TableDto table;
    if (includeInfo) {
        try {
            table = converterUtil.toTableDto(service.get(connectorContext, name));
        } catch (NotFoundException ignored) {
            return Optional.empty();
        }
    } else {
        table = new TableDto();
        table.setName(name);
    }
    if (includeDefinitionMetadata) {
        final Optional<ObjectNode> definitionMetadata = userMetadataService.getDefinitionMetadata(name);
        if (definitionMetadata.isPresent()) {
            table.setDefinitionMetadata(definitionMetadata.get());
        }
    }
    if (includeDataMetadata) {
        TableDto dto = table;
        if (!includeInfo) {
            try {
                dto = converterUtil.toTableDto(service.get(connectorContext, name));
            } catch (NotFoundException ignored) {
            }
        }
        if (dto != null && dto.getSerde() != null) {
            final Optional<ObjectNode> dataMetadata = userMetadataService.getDataMetadata(dto.getSerde().getUri());
            if (dataMetadata.isPresent()) {
                table.setDataMetadata(dataMetadata.get());
            }
        }
    }
    return Optional.of(table);
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableDto(com.netflix.metacat.common.dto.TableDto)

Example 27 with MetacatRequestContext

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

the class TableServiceImpl method getQualifiedNames.

@Override
public List<QualifiedName> getQualifiedNames(final String uri, final boolean prefixSearch) {
    final List<QualifiedName> result = Lists.newArrayList();
    connectorManager.getCatalogs().keySet().forEach(catalogName -> {
        final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
        final ConnectorTableService service = connectorManager.getTableService(catalogName);
        final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
        try {
            final Map<String, List<QualifiedName>> names = service.getTableNames(connectorContext, Lists.newArrayList(uri), prefixSearch);
            final List<QualifiedName> qualifiedNames = names.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
            result.addAll(qualifiedNames);
        } catch (final UnsupportedOperationException uoe) {
            log.debug("Catalog {} doesn't support getting table names by URI. Skipping", catalogName);
        }
    });
    return result;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) QualifiedName(com.netflix.metacat.common.QualifiedName) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) List(java.util.List)

Example 28 with MetacatRequestContext

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

the class MetadataService method processDeletedDataMetadata.

/**
     * Deletes all the data metadata marked for deletion.
     */
public void processDeletedDataMetadata() {
    // 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(uri -> userMetadataService.getDescendantDataUris(uri)).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.deleteDataMetadatas(canDeleteMetadataForUris);
                    userMetadataService.deleteDataMetadataDeletes(subUris);
                    MetacatContextManager.removeContext();
                });
            }
            if (urisToDelete.size() < limit) {
                break;
            }
        }
    } catch (Exception e) {
        registry.counter(Metrics.CounterDeleteMetaData.name()).increment();
        log.warn("Failed deleting data metadata", e);
    }
    log.info("End deleting data metadata");
}
Also used : UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) NonNull(lombok.NonNull) MetacatContextManager(com.netflix.metacat.common.server.util.MetacatContextManager) Collection(java.util.Collection) DateTime(org.joda.time.DateTime) QualifiedName(com.netflix.metacat.common.QualifiedName) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lists(com.google.common.collect.Lists) Stream(java.util.stream.Stream) Registry(com.netflix.spectator.api.Registry) Map(java.util.Map) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) AllArgsConstructor(lombok.AllArgsConstructor) Config(com.netflix.metacat.common.server.properties.Config) Nonnull(javax.annotation.Nonnull) Metrics(com.netflix.metacat.common.server.monitoring.Metrics) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) List(java.util.List) Map(java.util.Map) DateTime(org.joda.time.DateTime)

Example 29 with MetacatRequestContext

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

the class DatabaseServiceImpl method delete.

@Override
public void delete(@Nonnull final QualifiedName name) {
    validate(name);
    log.info("Dropping schema {}", name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final DatabaseDto dto = get(name, true);
    eventBus.postSync(new MetacatDeleteDatabasePreEvent(name, metacatRequestContext, this, dto));
    final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
    connectorManager.getDatabaseService(name.getCatalogName()).delete(connectorContext, name);
    // Delete definition metadata if it exists
    if (userMetadataService.getDefinitionMetadata(name).isPresent()) {
        log.info("Deleting user metadata for schema {}", name);
        userMetadataService.deleteDefinitionMetadatas(ImmutableList.of(name));
    }
    eventBus.postAsync(new MetacatDeleteDatabasePostEvent(name, metacatRequestContext, this, dto));
}
Also used : MetacatDeleteDatabasePostEvent(com.netflix.metacat.common.server.events.MetacatDeleteDatabasePostEvent) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) MetacatDeleteDatabasePreEvent(com.netflix.metacat.common.server.events.MetacatDeleteDatabasePreEvent) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext)

Example 30 with MetacatRequestContext

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

the class DatabaseServiceImpl method create.

@Override
public DatabaseDto create(@Nonnull final QualifiedName name, @Nonnull final DatabaseDto dto) {
    validate(name);
    log.info("Creating schema {}", name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.postSync(new MetacatCreateDatabasePreEvent(name, metacatRequestContext, this));
    final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
    connectorManager.getDatabaseService(name.getCatalogName()).create(connectorContext, converterUtil.fromDatabaseDto(dto));
    if (dto.getDefinitionMetadata() != null) {
        log.info("Saving user metadata for schema {}", name);
        userMetadataService.saveDefinitionMetadata(name, metacatRequestContext.getUserName(), Optional.of(dto.getDefinitionMetadata()), true);
    }
    final DatabaseDto createdDto = get(name, dto.getDefinitionMetadata() != null);
    eventBus.postAsync(new MetacatCreateDatabasePostEvent(name, metacatRequestContext, this, createdDto));
    return createdDto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) MetacatCreateDatabasePostEvent(com.netflix.metacat.common.server.events.MetacatCreateDatabasePostEvent) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) MetacatCreateDatabasePreEvent(com.netflix.metacat.common.server.events.MetacatCreateDatabasePreEvent)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)36 ConnectorContext (com.netflix.metacat.common.server.connectors.ConnectorContext)21 QualifiedName (com.netflix.metacat.common.QualifiedName)15 TableDto (com.netflix.metacat.common.dto.TableDto)15 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)12 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)11 List (java.util.List)8 PartitionDto (com.netflix.metacat.common.dto.PartitionDto)7 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 GetPartitionsRequestDto (com.netflix.metacat.common.dto.GetPartitionsRequestDto)6 Lists (com.google.common.collect.Lists)5 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)5 PartitionsSaveRequestDto (com.netflix.metacat.common.dto.PartitionsSaveRequestDto)5 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)5 Collectors (java.util.stream.Collectors)5 Slf4j (lombok.extern.slf4j.Slf4j)5 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)4 Pageable (com.netflix.metacat.common.dto.Pageable)4 PartitionsSaveResponseDto (com.netflix.metacat.common.dto.PartitionsSaveResponseDto)4