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