Search in sources :

Example 6 with PolarisTableEntity

use of com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity in project metacat by Netflix.

the class PolarisConnectorTableService method get.

/**
 * {@inheritDoc}.
 */
@Override
public TableInfo get(final ConnectorRequestContext requestContext, final QualifiedName name) {
    try {
        final PolarisTableEntity polarisTableEntity = polarisStoreService.getTable(name.getDatabaseName(), name.getTableName()).orElseThrow(() -> new TableNotFoundException(name));
        final TableInfo info = polarisTableMapper.toInfo(polarisTableEntity);
        final String tableLoc = HiveTableUtil.getIcebergTableMetadataLocation(info);
        return getIcebergTable(name, tableLoc, info, requestContext.isIncludeMetadata(), connectorContext.getConfig().isIcebergCacheEnabled());
    } catch (TableNotFoundException | IllegalArgumentException exception) {
        log.error(String.format("Not found exception for polaris table %s", name), exception);
        throw exception;
    } catch (ConnectorException connectorException) {
        log.error("Encountered connector exception for polaris table {}. {}", name, connectorException);
        throw connectorException;
    } catch (Exception exception) {
        final String msg = String.format("Failed getting polaris table %s", name);
        log.error(msg, exception);
        throw exception;
    }
}
Also used : TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)

Example 7 with PolarisTableEntity

use of com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity in project metacat by Netflix.

the class PolarisConnectorTableService method rename.

/**
 * {@inheritDoc}.
 */
@Override
public void rename(final ConnectorRequestContext context, final QualifiedName oldName, final QualifiedName newName) {
    // check exists then rename in non-transactional optimistic manner
    if (exists(context, newName)) {
        throw new TableAlreadyExistsException(newName);
    }
    try {
        final PolarisTableEntity table = polarisStoreService.getTable(oldName.getDatabaseName(), oldName.getTableName()).orElseThrow(() -> new TableNotFoundException(oldName));
        polarisStoreService.saveTable(table.toBuilder().tblName(newName.getTableName()).build());
    } catch (TableNotFoundException exception) {
        log.error(String.format("Not found exception for polaris table %s", oldName), exception);
        throw exception;
    } catch (DataIntegrityViolationException exception) {
        throw new InvalidMetaException(oldName, exception);
    } catch (Exception exception) {
        final String msg = String.format("Failed renaming polaris table %s", oldName);
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 8 with PolarisTableEntity

use of com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity in project metacat by Netflix.

the class PolarisConnectorTableService method update.

/**
 * {@inheritDoc}.
 */
@Override
public void update(final ConnectorRequestContext requestContext, final TableInfo tableInfo) {
    final QualifiedName name = tableInfo.getName();
    final Config conf = connectorContext.getConfig();
    try {
        final Map<String, String> newTableMetadata = tableInfo.getMetadata();
        if (MapUtils.isEmpty(newTableMetadata)) {
            final String message = String.format("No parameters defined for iceberg table %s", name);
            log.warn(message);
            throw new InvalidMetaException(name, message, null);
        }
        final String prevLoc = newTableMetadata.get(DirectSqlTable.PARAM_PREVIOUS_METADATA_LOCATION);
        final String newLoc = newTableMetadata.get(DirectSqlTable.PARAM_METADATA_LOCATION);
        if (StringUtils.isBlank(prevLoc) || StringUtils.isBlank(newLoc)) {
            final String message = String.format("Invalid metadata for %s. Provided previous %s or new %s location is empty.", name, prevLoc, newLoc);
            log.error(message);
            throw new InvalidMetaException(name, message, null);
        }
        if (conf.isIcebergPreviousMetadataLocationCheckEnabled() && !icebergTableHandler.doesMetadataLocationExist(name, prevLoc)) {
            final String message = String.format("Provided previous metadata location: %s for table: %s does not exist.", name, prevLoc);
            log.error(message);
            throw new InvalidMetaException(name, message, null);
        }
        // optimistically attempt to update metadata location
        final boolean updated = polarisStoreService.updateTableMetadataLocation(name.getDatabaseName(), name.getTableName(), prevLoc, newLoc);
        // if succeeded then done, else try to figure out why and throw corresponding exception
        if (updated) {
            requestContext.setIgnoreErrorsAfterUpdate(true);
            return;
        }
        final PolarisTableEntity table = polarisStoreService.getTable(name.getDatabaseName(), name.getTableName()).orElseThrow(() -> new TableNotFoundException(name));
        final String existingLoc = table.getMetadataLocation();
        if (StringUtils.isBlank(existingLoc)) {
            final String message = String.format("Invalid metadata location for %s existing location is empty.", name);
            log.error(message);
            throw new TablePreconditionFailedException(name, message, existingLoc, prevLoc);
        }
        if (StringUtils.equalsIgnoreCase(existingLoc, newLoc)) {
            log.warn("Existing metadata location is the same as new. Existing: {}, New: {}", existingLoc, newLoc);
            return;
        }
        if (!Objects.equals(existingLoc, prevLoc)) {
            final String message = String.format("Invalid metadata location for %s expected: %s, provided: %s", name, existingLoc, prevLoc);
            log.error(message);
            throw new TablePreconditionFailedException(name, message, existingLoc, prevLoc);
        }
    } catch (TableNotFoundException | InvalidMetaException | TablePreconditionFailedException exception) {
        throw exception;
    } catch (DataIntegrityViolationException exception) {
        throw new InvalidMetaException(name, exception);
    } catch (Exception exception) {
        final String msg = String.format("Failed updating polaris table %s", tableInfo.getName());
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) Config(com.netflix.metacat.common.server.properties.Config) QualifiedName(com.netflix.metacat.common.QualifiedName) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException)

Example 9 with PolarisTableEntity

use of com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity in project metacat by Netflix.

the class PolarisConnectorTableService method list.

/**
 * {@inheritDoc}.
 */
@Override
public List<TableInfo> list(final ConnectorRequestContext requestContext, final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    try {
        final String tableFilter = (prefix != null && prefix.isTableDefinition()) ? prefix.getTableName() : "";
        final List<PolarisTableEntity> tbls = polarisStoreService.getTableEntities(name.getDatabaseName(), tableFilter);
        if (sort != null) {
            ConnectorUtils.sort(tbls, sort, Comparator.comparing(t -> t.getTblName()));
        }
        return ConnectorUtils.paginate(tbls, pageable).stream().map(t -> polarisTableMapper.toInfo(t)).collect(Collectors.toList());
    } catch (Exception exception) {
        final String msg = String.format("Failed polaris list tables %s using prefix %s", name, prefix);
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Cacheable(org.springframework.cache.annotation.Cacheable) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) DirectSqlTable(com.netflix.metacat.connector.hive.sql.DirectSqlTable) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) Lists(com.google.common.collect.Lists) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Map(java.util.Map) IcebergTableWrapper(com.netflix.metacat.connector.hive.iceberg.IcebergTableWrapper) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) HiveConnectorInfoConverter(com.netflix.metacat.connector.hive.converters.HiveConnectorInfoConverter) Config(com.netflix.metacat.common.server.properties.Config) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Nullable(javax.annotation.Nullable) PolarisTableMapper(com.netflix.metacat.connector.polaris.mappers.PolarisTableMapper) MapUtils(org.apache.commons.collections.MapUtils) Pageable(com.netflix.metacat.common.dto.Pageable) QualifiedName(com.netflix.metacat.common.QualifiedName) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) IcebergTableHandler(com.netflix.metacat.connector.hive.iceberg.IcebergTableHandler) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) HiveTableUtil(com.netflix.metacat.connector.hive.util.HiveTableUtil) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) ConnectorUtils(com.netflix.metacat.common.server.connectors.ConnectorUtils) Comparator(java.util.Comparator) PolarisStoreService(com.netflix.metacat.connector.polaris.store.PolarisStoreService) Sort(com.netflix.metacat.common.dto.Sort) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)

Example 10 with PolarisTableEntity

use of com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity in project metacat by Netflix.

the class PolarisStoreConnector method getTableEntities.

/**
 * Fetch table entities for given database.
 * @param databaseName database name
 * @param tableNamePrefix table name prefix. can be empty.
 * @return table entities in the database.
 */
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<PolarisTableEntity> getTableEntities(final String databaseName, final String tableNamePrefix) {
    final int pageFetchSize = 1000;
    final List<PolarisTableEntity> retval = new ArrayList<>();
    final String tblPrefix = tableNamePrefix == null ? "" : tableNamePrefix;
    Pageable page = PageRequest.of(0, pageFetchSize, Sort.by("tblName").ascending());
    Slice<PolarisTableEntity> tbls;
    boolean hasNext;
    do {
        tbls = tblRepo.findAllTablesByDbNameAndTablePrefix(databaseName, tblPrefix, page);
        retval.addAll(tbls.toList());
        hasNext = tbls.hasNext();
        if (hasNext) {
            page = tbls.nextPageable();
        }
    } while (hasNext);
    return retval;
}
Also used : Pageable(org.springframework.data.domain.Pageable) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PolarisTableEntity (com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity)13 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)6 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)5 TableAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)5 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)5 TablePreconditionFailedException (com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 QualifiedName (com.netflix.metacat.common.QualifiedName)3 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)2 Config (com.netflix.metacat.common.server.properties.Config)2 PolarisDatabaseEntity (com.netflix.metacat.connector.polaris.store.entities.PolarisDatabaseEntity)2 Lists (com.google.common.collect.Lists)1 Pageable (com.netflix.metacat.common.dto.Pageable)1 Sort (com.netflix.metacat.common.dto.Sort)1 ConnectorContext (com.netflix.metacat.common.server.connectors.ConnectorContext)1 ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)1 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)1 ConnectorUtils (com.netflix.metacat.common.server.connectors.ConnectorUtils)1