Search in sources :

Example 46 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext 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 47 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext 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 48 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext 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 49 with MetacatRequestContext

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

the class ApiFilter method doFilter.

/**
 * {@inheritDoc}
 */
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    // Pre-processing
    if (!(request instanceof HttpServletRequest)) {
        throw new ServletException("Expected an HttpServletRequest but didn't get one");
    }
    final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String userName = httpServletRequest.getHeader(MetacatRequestContext.HEADER_KEY_USER_NAME);
    if (userName == null) {
        userName = "metacat";
    }
    final String clientAppName = httpServletRequest.getHeader(MetacatRequestContext.HEADER_KEY_CLIENT_APP_NAME);
    final String clientId = httpServletRequest.getHeader("X-Forwarded-For");
    final String jobId = httpServletRequest.getHeader(MetacatRequestContext.HEADER_KEY_JOB_ID);
    final String dataTypeContext = httpServletRequest.getHeader(MetacatRequestContext.HEADER_KEY_DATA_TYPE_CONTEXT);
    final MetacatRequestContext context = MetacatRequestContext.builder().userName(userName).clientAppName(clientAppName).clientId(clientId).jobId(jobId).dataTypeContext(dataTypeContext).scheme(httpServletRequest.getScheme()).apiUri(httpServletRequest.getRequestURI()).build();
    MetacatContextManager.setContext(context);
    log.info(context.toString());
    // Do the rest of the chain
    chain.doFilter(request, response);
    // Post processing
    MetacatContextManager.removeContext();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext)

Example 50 with MetacatRequestContext

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

the class MetadataController method deleteDefinitionMetadata.

/**
 * Delete the definition metadata for the given name.
 *
 * @param name  Name of definition metadata to be deleted
 * @param force If true, deletes the metadata without checking if the database/table/partition exists
 */
@RequestMapping(method = RequestMethod.DELETE, path = "/definition")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiOperation(position = 4, value = "Deletes the given definition metadata")
public void deleteDefinitionMetadata(@ApiParam(value = "Name of definition metadata to be deleted", required = true) @RequestParam(name = "name") final String name, @ApiParam(value = "If true, deletes the metadata without checking if the database/table/partition exists") @RequestParam(name = "force", defaultValue = "false") final boolean force) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    requestWrapper.processRequest("deleteDefinitionMetadata", () -> {
        metadataService.deleteDefinitionMetadata(QualifiedName.fromString(name), force, metacatRequestContext);
        return null;
    });
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)50 QualifiedName (com.netflix.metacat.common.QualifiedName)22 TableDto (com.netflix.metacat.common.dto.TableDto)20 ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)20 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)16 List (java.util.List)12 PartitionDto (com.netflix.metacat.common.dto.PartitionDto)11 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)11 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)10 Collectors (java.util.stream.Collectors)10 Slf4j (lombok.extern.slf4j.Slf4j)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 Lists (com.google.common.collect.Lists)8 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)8 GetPartitionsRequestDto (com.netflix.metacat.common.dto.GetPartitionsRequestDto)8 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)8 Metrics (com.netflix.metacat.common.server.monitoring.Metrics)8 UserMetadataService (com.netflix.metacat.common.server.usermetadata.UserMetadataService)8 MetacatContextManager (com.netflix.metacat.common.server.util.MetacatContextManager)8 Registry (com.netflix.spectator.api.Registry)8