Search in sources :

Example 61 with MetacatRequestContext

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

the class TagController method removeTableTags.

/**
 * Remove the tags from the given table.
 * TODO: remove after removeTags api is adopted
 *
 * @param catalogName  catalog name
 * @param databaseName database name
 * @param tableName    table name
 * @param deleteAll    True if all tags need to be removed
 * @param tags         Tags to be removed from the given table
 */
@RequestMapping(method = RequestMethod.DELETE, path = "/catalog/{catalog-name}/database/{database-name}/table/{table-name}", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiOperation(position = 4, value = "Remove the tags from the given table", notes = "Remove the tags from the given table")
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_NO_CONTENT, message = "The tags were successfully deleted from the table"), @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "The requested catalog or database or table cannot be located") })
public void removeTableTags(@ApiParam(value = "The name of the catalog", required = true) @PathVariable("catalog-name") final String catalogName, @ApiParam(value = "The name of the database", required = true) @PathVariable("database-name") final String databaseName, @ApiParam(value = "The name of the table", required = true) @PathVariable("table-name") final String tableName, @ApiParam(value = "True if all tags need to be removed") @RequestParam(name = "all", defaultValue = "false") final boolean deleteAll, @ApiParam(value = "Tags to be removed from the given table") @Nullable @RequestBody(required = false) final Set<String> tags) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final QualifiedName name = this.requestWrapper.qualifyName(() -> QualifiedName.ofTable(catalogName, databaseName, tableName));
    this.requestWrapper.processRequest(name, "TagV1Resource.removeTableTags", () -> {
        // TODO: Business logic in API tier...
        if (!this.tableService.exists(name)) {
            // Delete tags if exists
            this.tagService.delete(name, false);
            throw new TableNotFoundException(name);
        }
        final TableDto oldTable = this.tableService.get(name, GetTableServiceParameters.builder().includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).disableOnReadMetadataIntercetor(false).build()).orElseThrow(IllegalStateException::new);
        this.tagService.removeTags(name, deleteAll, tags, true);
        final TableDto currentTable = this.tableService.get(name, GetTableServiceParameters.builder().includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).disableOnReadMetadataIntercetor(false).build()).orElseThrow(IllegalStateException::new);
        this.eventBus.post(new MetacatUpdateTablePostEvent(name, metacatRequestContext, this, oldTable, currentTable));
        return null;
    });
}
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) QualifiedName(com.netflix.metacat.common.QualifiedName) TableDto(com.netflix.metacat.common.dto.TableDto) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 62 with MetacatRequestContext

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

the class MViewServiceImpl method deletePartitions.

/**
 * {@inheritDoc}
 */
@Override
public void deletePartitions(final QualifiedName name, final List<String> partitionIds) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final PartitionsSaveRequestDto dto = new PartitionsSaveRequestDto();
    dto.setPartitionIdsForDeletes(partitionIds);
    eventBus.post(new MetacatDeleteMViewPartitionPreEvent(name, metacatRequestContext, this, dto));
    final QualifiedName viewQName = QualifiedName.ofTable(name.getCatalogName(), VIEW_DB_NAME, createViewName(name));
    partitionService.delete(viewQName, partitionIds);
    eventBus.post(new MetacatDeleteMViewPartitionPostEvent(name, metacatRequestContext, this, partitionIds));
}
Also used : PartitionsSaveRequestDto(com.netflix.metacat.common.dto.PartitionsSaveRequestDto) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) MetacatDeleteMViewPartitionPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPreEvent) MetacatDeleteMViewPartitionPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPostEvent) QualifiedName(com.netflix.metacat.common.QualifiedName)

Example 63 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 TableDto oldTable = get(name, GetTableServiceParameters.builder().disableOnReadMetadataIntercetor(false).includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).build()).orElseThrow(() -> new TableNotFoundException(name));
    eventBus.post(new MetacatUpdateTablePreEvent(name, metacatRequestContext, this, oldTable, tableDto));
    // 
    // Check if the table schema info is provided. If provided, we should continue calling the update on the table
    // schema. Uri may exist in the serde when updating data metadata for a table.
    // 
    boolean ignoreErrorsAfterUpdate = false;
    if (isTableInfoProvided(tableDto, oldTable)) {
        ignoreErrorsAfterUpdate = connectorTableServiceProxy.update(name, converterUtil.fromTableDto(tableDto));
    }
    try {
        // 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);
        }
    } catch (Exception e) {
        handleException(name, ignoreErrorsAfterUpdate, "saveMetadata", e);
    }
    // ignoreErrorsAfterUpdate is currently set only for iceberg tables
    if (config.isUpdateIcebergTableAsyncPostEventEnabled() && ignoreErrorsAfterUpdate) {
        eventBus.post(new MetacatUpdateIcebergTablePostEvent(name, metacatRequestContext, this, oldTable, tableDto));
        return tableDto;
    } else {
        TableDto updatedDto = tableDto;
        try {
            updatedDto = get(name, GetTableServiceParameters.builder().disableOnReadMetadataIntercetor(false).includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).build()).orElse(tableDto);
        } catch (Exception e) {
            handleException(name, ignoreErrorsAfterUpdate, "getTable", e);
        }
        try {
            eventBus.post(new MetacatUpdateTablePostEvent(name, metacatRequestContext, this, oldTable, updatedDto, updatedDto != tableDto));
        } catch (Exception e) {
            handleException(name, ignoreErrorsAfterUpdate, "postEvent", e);
        }
        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) MetacatUpdateIcebergTablePostEvent(com.netflix.metacat.common.server.events.MetacatUpdateIcebergTablePostEvent) TableDto(com.netflix.metacat.common.dto.TableDto) MetacatUpdateTablePreEvent(com.netflix.metacat.common.server.events.MetacatUpdateTablePreEvent) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)

Example 64 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);
    this.authorizationService.checkPermission(metacatRequestContext.getUserName(), tableDto.getName(), MetacatOperation.CREATE);
    // 
    // Set the owner,if null, with the session user name.
    // 
    setOwnerIfNull(tableDto, metacatRequestContext.getUserName());
    log.info("Creating table {}", name);
    eventBus.post(new MetacatCreateTablePreEvent(name, metacatRequestContext, this, tableDto));
    connectorTableServiceProxy.create(name, 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, 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);
        tag(name, tableDto.getDefinitionMetadata());
    }
    TableDto dto = tableDto;
    try {
        dto = get(name, GetTableServiceParameters.builder().disableOnReadMetadataIntercetor(false).includeInfo(true).includeDataMetadata(true).includeDefinitionMetadata(true).build()).orElse(tableDto);
    } catch (Exception e) {
        handleExceptionOnCreate(name, "getTable", e);
    }
    try {
        eventBus.post(new MetacatCreateTablePostEvent(name, metacatRequestContext, this, dto));
    } catch (Exception e) {
        handleExceptionOnCreate(name, "postEvent", e);
    }
    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) TableDto(com.netflix.metacat.common.dto.TableDto) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)

Example 65 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)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)65 QualifiedName (com.netflix.metacat.common.QualifiedName)28 ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)24 TableDto (com.netflix.metacat.common.dto.TableDto)22 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)19 List (java.util.List)16 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)13 Collectors (java.util.stream.Collectors)12 Slf4j (lombok.extern.slf4j.Slf4j)12 PartitionDto (com.netflix.metacat.common.dto.PartitionDto)11 Lists (com.google.common.collect.Lists)10 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)10 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)10 Metrics (com.netflix.metacat.common.server.monitoring.Metrics)10 UserMetadataService (com.netflix.metacat.common.server.usermetadata.UserMetadataService)10 Registry (com.netflix.spectator.api.Registry)10 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)9 GetPartitionsRequestDto (com.netflix.metacat.common.dto.GetPartitionsRequestDto)9 MetacatUpdateTablePostEvent (com.netflix.metacat.common.server.events.MetacatUpdateTablePostEvent)9 Config (com.netflix.metacat.common.server.properties.Config)9