Search in sources :

Example 21 with ConnectorRequestContext

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

the class PartitionServiceImpl method getQualifiedNames.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, List<QualifiedName>> getQualifiedNames(final List<String> uris, final boolean prefixSearch) {
    final Map<String, List<QualifiedName>> result = Maps.newConcurrentMap();
    final List<ListenableFuture<Void>> futures = Lists.newArrayList();
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    connectorManager.getPartitionServices().forEach(service -> {
        futures.add(threadServiceManager.getExecutor().submit(() -> {
            final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
            try {
                final Map<String, List<QualifiedName>> partitionNames = service.getPartitionNames(connectorRequestContext, uris, prefixSearch);
                partitionNames.forEach((uri, subPartitionNames) -> {
                    final List<QualifiedName> existingPartitionNames = result.get(uri);
                    if (existingPartitionNames == null) {
                        result.put(uri, subPartitionNames);
                    } else {
                        existingPartitionNames.addAll(subPartitionNames);
                    }
                });
            } catch (final UnsupportedOperationException uoe) {
                log.debug("Partition service doesn't support getPartitionNames. Ignoring.");
            }
            return null;
        }));
    });
    try {
        Futures.allAsList(futures).get(1, TimeUnit.HOURS);
    } catch (Exception e) {
        Throwables.propagate(e);
    }
    return result;
}
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) ConverterUtil(com.netflix.metacat.common.server.converter.ConverterUtil) MetacatContextManager(com.netflix.metacat.common.server.util.MetacatContextManager) PartitionService(com.netflix.metacat.main.services.PartitionService) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto) PartitionInfo(com.netflix.metacat.common.server.connectors.model.PartitionInfo) Map(java.util.Map) PartitionsSaveRequestDto(com.netflix.metacat.common.dto.PartitionsSaveRequestDto) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Collection(java.util.Collection) Pageable(com.netflix.metacat.common.dto.Pageable) QualifiedName(com.netflix.metacat.common.QualifiedName) Collectors(java.util.stream.Collectors) PartitionsSaveResponse(com.netflix.metacat.common.server.connectors.model.PartitionsSaveResponse) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) MetacatSaveTablePartitionMetadataOnlyPostEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionMetadataOnlyPostEvent) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) ConnectorManager(com.netflix.metacat.main.manager.ConnectorManager) Sort(com.netflix.metacat.common.dto.Sort) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Id(com.netflix.spectator.api.Id) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ConnectorPartitionService(com.netflix.metacat.common.server.connectors.ConnectorPartitionService) Lists(com.google.common.collect.Lists) ThreadServiceManager(com.netflix.metacat.common.server.util.ThreadServiceManager) TableService(com.netflix.metacat.main.services.TableService) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) Config(com.netflix.metacat.common.server.properties.Config) Nullable(javax.annotation.Nullable) Metrics(com.netflix.metacat.common.server.monitoring.Metrics) MetacatDeleteTablePartitionPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePartitionPostEvent) GetPartitionsRequestDto(com.netflix.metacat.common.dto.GetPartitionsRequestDto) Throwables(com.google.common.base.Throwables) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Registry(com.netflix.spectator.api.Registry) MetacatSaveTablePartitionPostEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionPostEvent) PartitionListRequest(com.netflix.metacat.common.server.connectors.model.PartitionListRequest) MetacatDeleteTablePartitionPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePartitionPreEvent) MetacatSaveTablePartitionMetadataOnlyPreEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionMetadataOnlyPreEvent) MetacatSaveTablePartitionPreEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionPreEvent) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) QualifiedName(com.netflix.metacat.common.QualifiedName) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Map(java.util.Map) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)

Example 22 with ConnectorRequestContext

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

the class TableServiceImpl method create.

/**
 * {@inheritDoc}
 */
@Override
public TableDto create(final QualifiedName name, final TableDto tableDto) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    validate(name);
    // 
    // Set the owner,if null, with the session user name.
    // 
    setOwnerIfNull(tableDto, metacatRequestContext.getUserName());
    log.info("Creating table {}", name);
    eventBus.postSync(new MetacatCreateTablePreEvent(name, metacatRequestContext, this, tableDto));
    final ConnectorTableService service = connectorManager.getTableService(name);
    final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
    service.create(connectorRequestContext, converterUtil.fromTableDto(tableDto));
    if (tableDto.getDataMetadata() != null || tableDto.getDefinitionMetadata() != null) {
        log.info("Saving user metadata for table {}", name);
        final long start = registry.clock().wallTime();
        userMetadataService.saveMetadata(metacatRequestContext.getUserName(), tableDto, false);
        final long duration = registry.clock().wallTime() - start;
        log.info("Time taken to save user metadata for table {} is {} ms", name, duration);
        registry.timer(registry.createId(Metrics.TimerSaveTableMetadata.getMetricName()).withTags(name.parts())).record(duration, TimeUnit.MILLISECONDS);
        tag(name, tableDto.getDefinitionMetadata());
    }
    final TableDto dto = get(name, GetTableServiceParameters.builder().disableOnReadMetadataIntercetor(false).includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).build()).orElseThrow(() -> new IllegalStateException("Should exist"));
    eventBus.postAsync(new MetacatCreateTablePostEvent(name, metacatRequestContext, this, dto));
    return dto;
}
Also used : MetacatCreateTablePreEvent(com.netflix.metacat.common.server.events.MetacatCreateTablePreEvent) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) MetacatCreateTablePostEvent(com.netflix.metacat.common.server.events.MetacatCreateTablePostEvent) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) TableDto(com.netflix.metacat.common.dto.TableDto)

Example 23 with ConnectorRequestContext

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

the class TableServiceImpl method getQualifiedNames.

/**
 * {@inheritDoc}
 */
@Override
public List<QualifiedName> getQualifiedNames(final String uri, final boolean prefixSearch) {
    final List<QualifiedName> result = Lists.newArrayList();
    connectorManager.getTableServices().forEach(service -> {
        final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
        final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
        try {
            final Map<String, List<QualifiedName>> names = service.getTableNames(connectorRequestContext, 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("Table service doesn't support getting table names by URI. Skipping");
        }
    });
    return result;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) QualifiedName(com.netflix.metacat.common.QualifiedName) List(java.util.List) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext)

Example 24 with ConnectorRequestContext

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

the class TableServiceImpl method updateAndReturn.

/**
 * {@inheritDoc}
 */
@Override
public TableDto updateAndReturn(final QualifiedName name, final TableDto tableDto) {
    validate(name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorTableService service = connectorManager.getTableService(name);
    final TableDto oldTable = get(name, GetTableServiceParameters.builder().disableOnReadMetadataIntercetor(false).includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).build()).orElseThrow(() -> new TableNotFoundException(name));
    eventBus.postSync(new MetacatUpdateTablePreEvent(name, metacatRequestContext, this, oldTable, tableDto));
    // Ignore if the operation is not supported, so that we can at least go ahead and save the user metadata
    if (isTableInfoProvided(tableDto)) {
        try {
            log.info("Updating table {}", name);
            final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
            service.update(connectorRequestContext, converterUtil.fromTableDto(tableDto));
        } catch (UnsupportedOperationException ignored) {
        }
    }
    // Merge in metadata if the user sent any
    if (tableDto.getDataMetadata() != null || tableDto.getDefinitionMetadata() != null) {
        log.info("Saving user metadata for table {}", name);
        final long start = registry.clock().wallTime();
        userMetadataService.saveMetadata(metacatRequestContext.getUserName(), tableDto, true);
        final long duration = registry.clock().wallTime() - start;
        log.info("Time taken to save user metadata for table {} is {} ms", name, duration);
        registry.timer(registry.createId(Metrics.TimerSaveTableMetadata.getMetricName()).withTags(name.parts())).record(duration, TimeUnit.MILLISECONDS);
    }
    final TableDto updatedDto = get(name, GetTableServiceParameters.builder().disableOnReadMetadataIntercetor(false).includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).build()).orElseThrow(() -> new IllegalStateException("should exist"));
    eventBus.postAsync(new MetacatUpdateTablePostEvent(name, metacatRequestContext, this, oldTable, updatedDto));
    return updatedDto;
}
Also used : MetacatUpdateTablePostEvent(com.netflix.metacat.common.server.events.MetacatUpdateTablePostEvent) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) TableDto(com.netflix.metacat.common.dto.TableDto) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) MetacatUpdateTablePreEvent(com.netflix.metacat.common.server.events.MetacatUpdateTablePreEvent)

Example 25 with ConnectorRequestContext

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

the class DirectSqlGetPartition method getPartitionCount.

/**
 * Number of partitions for the given table.
 *
 * @param requestContext request context
 * @param tableName      tableName
 * @return Number of partitions
 */
@Transactional(readOnly = true)
public int getPartitionCount(final ConnectorRequestContext requestContext, final QualifiedName tableName) {
    final long start = registry.clock().wallTime();
    // Handler for reading the result set
    final ResultSetExtractor<Integer> handler = rs -> {
        int count = 0;
        while (rs.next()) {
            count = rs.getInt("count");
        }
        return count;
    };
    try {
        final Optional<QualifiedName> sourceTable = getSourceTableName(tableName.getDatabaseName(), tableName.getTableName(), false);
        return sourceTable.map(qualifiedName -> jdbcTemplate.query(SQL.SQL_GET_AUDIT_TABLE_PARTITION_COUNT, new String[] { tableName.getDatabaseName(), tableName.getTableName(), qualifiedName.getDatabaseName(), qualifiedName.getTableName() }, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR }, handler)).orElseGet(() -> jdbcTemplate.query(SQL.SQL_GET_PARTITION_COUNT, new String[] { tableName.getDatabaseName(), tableName.getTableName() }, new int[] { Types.VARCHAR, Types.VARCHAR }, handler));
    } catch (Exception e) {
        throw new ConnectorException("Failed getting the partition count", e);
    } finally {
        this.fastServiceMetric.recordTimer(HiveMetrics.TagGetPartitionCount.getMetricName(), registry.clock().wallTime() - start);
    }
}
Also used : HiveConfigConstants(com.netflix.metacat.connector.hive.util.HiveConfigConstants) HiveConnectorFastServiceMetric(com.netflix.metacat.connector.hive.util.HiveConnectorFastServiceMetric) PartitionKeyParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionKeyParserEval) Date(java.util.Date) PartitionFilterGenerator(com.netflix.metacat.connector.hive.util.PartitionFilterGenerator) PartitionParamParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionParamParserEval) Matcher(java.util.regex.Matcher) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) PartitionInfo(com.netflix.metacat.common.server.connectors.model.PartitionInfo) Map(java.util.Map) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) StorageInfo(com.netflix.metacat.common.server.connectors.model.StorageInfo) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Collection(java.util.Collection) Pageable(com.netflix.metacat.common.dto.Pageable) QualifiedName(com.netflix.metacat.common.QualifiedName) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) HiveMetrics(com.netflix.metacat.connector.hive.monitoring.HiveMetrics) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Joiner(com.google.common.base.Joiner) Sort(com.netflix.metacat.common.dto.Sort) Types(java.sql.Types) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AuditInfo(com.netflix.metacat.common.server.connectors.model.AuditInfo) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) ThreadServiceManager(com.netflix.metacat.common.server.util.ThreadServiceManager) PartitionParser(com.netflix.metacat.common.server.partition.parser.PartitionParser) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Functions(com.google.common.base.Functions) Throwables(com.google.common.base.Throwables) Maps(com.google.common.collect.Maps) Table(org.apache.hadoop.hive.metastore.api.Table) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) FilterPartition(com.netflix.metacat.common.server.partition.util.FilterPartition) TimeUnit(java.util.concurrent.TimeUnit) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) Futures(com.google.common.util.concurrent.Futures) StringReader(java.io.StringReader) Registry(com.netflix.spectator.api.Registry) PartitionListRequest(com.netflix.metacat.common.server.connectors.model.PartitionListRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Transactional(org.springframework.transaction.annotation.Transactional) QualifiedName(com.netflix.metacat.common.QualifiedName) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)29 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)20 QualifiedName (com.netflix.metacat.common.QualifiedName)15 List (java.util.List)13 Lists (com.google.common.collect.Lists)11 Pageable (com.netflix.metacat.common.dto.Pageable)10 Sort (com.netflix.metacat.common.dto.Sort)10 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)10 PartitionInfo (com.netflix.metacat.common.server.connectors.model.PartitionInfo)10 Map (java.util.Map)10 Nullable (javax.annotation.Nullable)10 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)9 Collectors (java.util.stream.Collectors)9 Strings (com.google.common.base.Strings)8 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)8 PartitionListRequest (com.netflix.metacat.common.server.connectors.model.PartitionListRequest)8 Maps (com.google.common.collect.Maps)7 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)6 TableDto (com.netflix.metacat.common.dto.TableDto)6