Search in sources :

Example 1 with AcidTransactionOwner

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;
}
Also used : TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Table(io.trino.plugin.hive.metastore.Table) ThriftMetastoreUtil.fromMetastoreApiTable(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.fromMetastoreApiTable) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) AcidTransactionOwner(io.trino.plugin.hive.metastore.AcidTransactionOwner) TrinoException(io.trino.spi.TrinoException) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) HiveIdentity(io.trino.plugin.hive.authentication.HiveIdentity)

Aggregations

HiveIdentity (io.trino.plugin.hive.authentication.HiveIdentity)1 AcidTransactionOwner (io.trino.plugin.hive.metastore.AcidTransactionOwner)1 PrincipalPrivileges (io.trino.plugin.hive.metastore.PrincipalPrivileges)1 Table (io.trino.plugin.hive.metastore.Table)1 ThriftMetastoreUtil.fromMetastoreApiTable (io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.fromMetastoreApiTable)1 TrinoException (io.trino.spi.TrinoException)1 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)1 CommitFailedException (org.apache.iceberg.exceptions.CommitFailedException)1