Search in sources :

Example 1 with UserMetadataService

use of com.netflix.metacat.common.server.usermetadata.UserMetadataService 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) Getter(lombok.Getter) 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) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lists(com.google.common.collect.Lists) TagService(com.netflix.metacat.common.server.usermetadata.TagService) Stream(java.util.stream.Stream) BaseDto(com.netflix.metacat.common.dto.BaseDto) Registry(com.netflix.spectator.api.Registry) 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) 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)

Aggregations

Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)1 QualifiedName (com.netflix.metacat.common.QualifiedName)1 BaseDto (com.netflix.metacat.common.dto.BaseDto)1 DefinitionMetadataDto (com.netflix.metacat.common.dto.DefinitionMetadataDto)1 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)1 Metrics (com.netflix.metacat.common.server.monitoring.Metrics)1 Config (com.netflix.metacat.common.server.properties.Config)1 TagService (com.netflix.metacat.common.server.usermetadata.TagService)1 UserMetadataService (com.netflix.metacat.common.server.usermetadata.UserMetadataService)1 MetacatContextManager (com.netflix.metacat.common.server.util.MetacatContextManager)1 Registry (com.netflix.spectator.api.Registry)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Getter (lombok.Getter)1 Slf4j (lombok.extern.slf4j.Slf4j)1