Search in sources :

Example 1 with MetacatDeleteTablePostEvent

use of com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent in project metacat by Netflix.

the class TableServiceImpl method deleteAndReturn.

@Override
public TableDto deleteAndReturn(@Nonnull final QualifiedName name, final boolean isMView) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.postSync(new MetacatDeleteTablePreEvent(name, metacatRequestContext, this));
    validate(name);
    final ConnectorTableService service = connectorManager.getTableService(name.getCatalogName());
    final Optional<TableDto> oTable = get(name, true);
    if (oTable.isPresent()) {
        log.info("Drop table {}", name);
        final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
        service.delete(connectorContext, name);
    }
    final TableDto tableDto = oTable.orElseGet(() -> {
        // If the table doesn't exist construct a blank copy we can use to delete the definitionMetadata and tags
        final TableDto t = new TableDto();
        t.setName(name);
        return t;
    });
    // Delete the metadata.  Type doesn't matter since we discard the result
    log.info("Deleting user metadata for table {}", name);
    userMetadataService.deleteMetadatas(metacatRequestContext.getUserName(), Lists.newArrayList(tableDto));
    log.info("Deleting tags for table {}", name);
    tagService.delete(name, false);
    eventBus.postAsync(new MetacatDeleteTablePostEvent(name, metacatRequestContext, this, tableDto));
    return tableDto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) MetacatDeleteTablePostEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) TableDto(com.netflix.metacat.common.dto.TableDto) MetacatDeleteTablePreEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePreEvent)

Example 2 with MetacatDeleteTablePostEvent

use of com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent 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)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)2 TableDto (com.netflix.metacat.common.dto.TableDto)2 MetacatDeleteTablePostEvent (com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent)2 Functions (com.google.common.base.Functions)1 Splitter (com.google.common.base.Splitter)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 QualifiedName (com.netflix.metacat.common.QualifiedName)1 CatalogDto (com.netflix.metacat.common.dto.CatalogDto)1 CatalogMappingDto (com.netflix.metacat.common.dto.CatalogMappingDto)1 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)1 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)1 Pageable (com.netflix.metacat.common.dto.Pageable)1