Search in sources :

Example 96 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project metacat by Netflix.

the class MySqlLookupService method get.

/**
 * Returns the lookup for the given <code>name</code>.
 *
 * @param name lookup name
 * @return lookup
 */
@Override
@Transactional(readOnly = true)
public Lookup get(final String name) {
    try {
        return jdbcTemplate.queryForObject(SQL_GET_LOOKUP, new Object[] { name }, new int[] { Types.VARCHAR }, (rs, rowNum) -> {
            final Lookup lookup = new Lookup();
            lookup.setId(rs.getLong("id"));
            lookup.setName(rs.getString("name"));
            lookup.setType(rs.getString("type"));
            lookup.setCreatedBy(rs.getString("createdBy"));
            lookup.setLastUpdated(rs.getDate("lastUpdated"));
            lookup.setLastUpdatedBy(rs.getString("lastUpdatedBy"));
            lookup.setDateCreated(rs.getDate("dateCreated"));
            lookup.setValues(getValues(rs.getLong("id")));
            return lookup;
        });
    } catch (EmptyResultDataAccessException e) {
        return null;
    } catch (Exception e) {
        final String message = String.format("Failed to get the lookup for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Lookup(com.netflix.metacat.common.server.model.Lookup) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 97 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project metacat by Netflix.

the class DirectSqlTable method updateIcebergTable.

/**
 * Locks and updates the iceberg table for update so that no other request can modify the table at the same time.
 * 1. Gets the table parameters and locks the requested records. If lock cannot be attained,
 * the request to update fails
 * 2. Validates the metadata location
 * 3. If validated, updates the table parameters.
 * @param tableInfo table info
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateIcebergTable(final TableInfo tableInfo) {
    final QualifiedName tableName = tableInfo.getName();
    final Map<String, String> newTableMetadata = tableInfo.getMetadata();
    // 
    if (newTableMetadata == null || newTableMetadata.isEmpty()) {
        final String message = String.format("No parameters defined for iceberg table %s", tableName);
        log.warn(message);
        throw new InvalidMetaException(tableName, message, null);
    }
    // 
    // If the previous metadata location is not empty, check if it is valid.
    // 
    final String previousMetadataLocation = newTableMetadata.get(PARAM_PREVIOUS_METADATA_LOCATION);
    if (config.isIcebergPreviousMetadataLocationCheckEnabled() && !StringUtils.isBlank(previousMetadataLocation)) {
        boolean doesPathExists = true;
        try {
            final Path previousMetadataPath = new Path(previousMetadataLocation);
            doesPathExists = warehouse.getFs(previousMetadataPath).exists(previousMetadataPath);
        } catch (Exception ignored) {
            log.warn(String.format("Failed getting the filesystem for %s", previousMetadataLocation));
            registry.counter(HiveMetrics.CounterFileSystemReadFailure.name()).increment();
        }
        if (!doesPathExists) {
            throw new InvalidMetaException(tableName, String.format("Invalid metadata for %s..Location %s does not exist", tableName, previousMetadataLocation), null);
        }
    }
    final Long tableId = getTableId(tableName);
    Map<String, String> existingTableMetadata = null;
    log.debug("Lock Iceberg table {}", tableName);
    try {
        existingTableMetadata = jdbcTemplate.query(SQL.TABLE_PARAMS_LOCK, new SqlParameterValue[] { new SqlParameterValue(Types.BIGINT, tableId) }, rs -> {
            final Map<String, String> result = Maps.newHashMap();
            while (rs.next()) {
                result.put(rs.getString(COL_PARAM_KEY), rs.getString(COL_PARAM_VALUE));
            }
            return result;
        });
    } catch (EmptyResultDataAccessException ex) {
        log.info(String.format("No parameters defined for iceberg table %s", tableName));
    } catch (Exception ex) {
        final String message = String.format("Failed getting a lock on iceberg table %s", tableName);
        log.warn(message, ex);
        throw new InvalidMetaException(tableName, message, null);
    }
    if (existingTableMetadata == null) {
        existingTableMetadata = Maps.newHashMap();
    }
    final boolean needUpdate = validateIcebergUpdate(tableName, existingTableMetadata, newTableMetadata);
    final String existingMetadataLocation = existingTableMetadata.get(PARAM_METADATA_LOCATION);
    final String newMetadataLocation = newTableMetadata.get(PARAM_METADATA_LOCATION);
    log.info("Servicing Iceberg commit request with tableId: {}, needUpdate: {}, " + "previousLocation: {}, existingLocation: {}, newLocation: {}", tableId, needUpdate, previousMetadataLocation, existingMetadataLocation, newMetadataLocation);
    if (needUpdate) {
        final MapDifference<String, String> diff = Maps.difference(existingTableMetadata, newTableMetadata);
        insertTableParams(tableId, diff.entriesOnlyOnRight());
        final Map<String, String> updateParams = diff.entriesDiffering().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, s -> s.getValue().rightValue()));
        updateTableParams(tableId, updateParams);
        // 
        // In addition to updating the table params, the table location in HMS needs to be updated for usage by
        // external tools, that access HMS directly
        // 
        updateTableLocation(tableId, tableInfo);
        log.info("Finished updating Iceberg table with tableId: {}", tableId);
    }
    log.debug("Unlocked Iceberg table {}", tableName);
}
Also used : Path(org.apache.hadoop.fs.Path) StringUtils(org.apache.commons.lang.StringUtils) DataAccessException(org.springframework.dao.DataAccessException) HiveConnectorFastServiceMetric(com.netflix.metacat.connector.hive.util.HiveConnectorFastServiceMetric) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) Warehouse(org.apache.hadoop.hive.metastore.Warehouse) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Strings(com.google.common.base.Strings) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) Lists(com.google.common.collect.Lists) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) Propagation(org.springframework.transaction.annotation.Propagation) Map(java.util.Map) Path(org.apache.hadoop.fs.Path) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) Config(com.netflix.metacat.common.server.properties.Config) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Objects(java.util.Objects) MapDifference(com.google.common.collect.MapDifference) HiveMetrics(com.netflix.metacat.connector.hive.monitoring.HiveMetrics) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) HiveTableUtil(com.netflix.metacat.connector.hive.util.HiveTableUtil) Registry(com.netflix.spectator.api.Registry) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Transactional(org.springframework.transaction.annotation.Transactional) Types(java.sql.Types) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) QualifiedName(com.netflix.metacat.common.QualifiedName) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) DataAccessException(org.springframework.dao.DataAccessException) 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) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Map(java.util.Map) Transactional(org.springframework.transaction.annotation.Transactional)

Example 98 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project metacat by Netflix.

the class DirectSqlDatabase method getDatabaseById.

private DatabaseInfo getDatabaseById(final Long id, final QualifiedName databaseName) {
    DatabaseInfo result = null;
    try {
        // Retrieve databaseRowSet info record
        final SqlRowSet databaseRowSet = jdbcTemplate.queryForRowSet(SQL.GET_DATABASE, new Object[] { id }, new int[] { Types.BIGINT });
        if (databaseRowSet.first()) {
            final AuditInfo auditInfo = AuditInfo.builder().createdBy(databaseRowSet.getString(COL_OWNER)).build();
            // Retrieve databaseRowSet params
            final Map<String, String> metadata = Maps.newHashMap();
            try {
                final SqlRowSet paramRowSet = jdbcTemplate.queryForRowSet(SQL.GET_DATABASE_PARAMS, new Object[] { id }, new int[] { Types.BIGINT });
                while (paramRowSet.next()) {
                    metadata.put(paramRowSet.getString(COL_PARAM_KEY), paramRowSet.getString(COL_PARAM_VALUE));
                }
            } catch (EmptyResultDataAccessException ignored) {
            }
            result = DatabaseInfo.builder().name(databaseName).uri(databaseRowSet.getString(COL_URI)).auditInfo(auditInfo).metadata(metadata).build();
        }
    } catch (EmptyResultDataAccessException e) {
        log.debug("Database {} not found.", databaseName);
        throw new DatabaseNotFoundException(databaseName);
    }
    return result;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) AuditInfo(com.netflix.metacat.common.server.connectors.model.AuditInfo) DatabaseInfo(com.netflix.metacat.common.server.connectors.model.DatabaseInfo) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 99 with EmptyResultDataAccessException

use of org.springframework.dao.EmptyResultDataAccessException in project metacat by Netflix.

the class SequenceGeneration method newPartitionSequenceIdByName.

/**
 * Returns the current sequence ids and increments the sequence ids by the given <code>size</code>.
 *
 * @param size              number of records getting inserted
 * @param sequenceParamName the sequence Parameter Name
 * @return current sequence ids
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Long newPartitionSequenceIdByName(final int size, final String sequenceParamName) {
    Long result = null;
    try {
        // Get current sequence number
        result = jdbcTemplate.queryForObject(SQL.SEQUENCE_NEXT_VAL_BYNAME, new Object[] { sequenceParamName }, Long.class);
    } catch (EmptyResultDataAccessException e) {
        log.warn("Failed getting the sequence ids for partition", e);
    } catch (Exception e) {
        throw new ConnectorException("Failed retrieving the sequence numbers.");
    }
    try {
        if (result == null) {
            // init to 1L in case there's no records
            result = 1L;
            jdbcTemplate.update(SQL.SEQUENCE_INSERT_VAL, result + size, sequenceParamName);
        } else {
            jdbcTemplate.update(SQL.SEQUENCE_UPDATE_VAL, result + size, sequenceParamName);
        }
        return result;
    } catch (Exception e) {
        throw new ConnectorException("Failed updating the sequence ids for partition", e);
    }
}
Also used : ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)99 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)46 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)31 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)9 Transactional (org.springframework.transaction.annotation.Transactional)9 Group (cz.metacentrum.perun.core.api.Group)8 User (cz.metacentrum.perun.core.api.User)8 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)7 SQLException (java.sql.SQLException)7 Test (org.junit.Test)6 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)6 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Service (cz.metacentrum.perun.core.api.Service)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)3