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;
}
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;
}
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;
}
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();
}
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;
});
}
Aggregations