use of io.trino.plugin.hive.metastore.AcidTransactionOwner in project trino by trinodb.
the class HiveMetastoreTableOperations method commitToExistingTable.
@Override
protected void commitToExistingTable(TableMetadata base, TableMetadata metadata) {
String newMetadataLocation = writeNewMetadata(metadata, version + 1);
HiveIdentity identity = new HiveIdentity(session.getIdentity());
long lockId = thriftMetastore.acquireTableExclusiveLock(identity, new AcidTransactionOwner(session.getUser()), session.getQueryId(), database, tableName);
try {
Table table;
try {
Table currentTable = fromMetastoreApiTable(thriftMetastore.getTable(identity, database, tableName).orElseThrow(() -> new TableNotFoundException(getSchemaTableName())));
checkState(currentMetadataLocation != null, "No current metadata location for existing table");
String metadataLocation = currentTable.getParameters().get(METADATA_LOCATION_PROP);
if (!currentMetadataLocation.equals(metadataLocation)) {
throw new CommitFailedException("Metadata location [%s] is not same as table metadata location [%s] for %s", currentMetadataLocation, metadataLocation, getSchemaTableName());
}
table = Table.builder(currentTable).setDataColumns(toHiveColumns(metadata.schema().columns())).withStorage(storage -> storage.setLocation(metadata.location())).setParameter(METADATA_LOCATION_PROP, newMetadataLocation).setParameter(PREVIOUS_METADATA_LOCATION_PROP, currentMetadataLocation).build();
} catch (RuntimeException e) {
try {
io().deleteFile(newMetadataLocation);
} catch (RuntimeException ex) {
e.addSuppressed(ex);
}
throw new TrinoException(ICEBERG_COMMIT_ERROR, format("Failed to commit to table %s.%s", database, tableName), e);
}
// todo privileges should not be replaced for an alter
PrincipalPrivileges privileges = table.getOwner().map(MetastoreUtil::buildInitialPrivilegeSet).orElse(NO_PRIVILEGES);
metastore.replaceTable(database, tableName, table, privileges);
} finally {
thriftMetastore.releaseTableLock(identity, lockId);
}
shouldRefresh = true;
}
Aggregations