Search in sources :

Example 1 with TableMetadata

use of org.apache.iceberg.TableMetadata in project presto by prestodb.

the class HiveTableOperations method commit.

@Override
public void commit(@Nullable TableMetadata base, TableMetadata metadata) {
    requireNonNull(metadata, "metadata is null");
    // if the metadata is already out of date, reject it
    if (!Objects.equals(base, current())) {
        throw new CommitFailedException("Cannot commit: stale table metadata for %s", getSchemaTableName());
    }
    // if the metadata is not changed, return early
    if (Objects.equals(base, metadata)) {
        return;
    }
    String newMetadataLocation = writeNewMetadata(metadata, version + 1);
    Table table;
    // getting a process-level lock per table to avoid concurrent commit attempts to the same table from the same
    // JVM process, which would result in unnecessary and costly HMS lock acquisition requests
    Optional<Long> lockId = Optional.empty();
    ReentrantLock tableLevelMutex = commitLockCache.getUnchecked(database + "." + tableName);
    tableLevelMutex.lock();
    try {
        try {
            lockId = Optional.of(metastore.lock(metastoreContext, database, tableName));
            if (base == null) {
                String tableComment = metadata.properties().get(TABLE_COMMENT);
                Map<String, String> parameters = new HashMap<>();
                parameters.put("EXTERNAL", "TRUE");
                parameters.put(TABLE_TYPE_PROP, ICEBERG_TABLE_TYPE_VALUE);
                parameters.put(METADATA_LOCATION, newMetadataLocation);
                if (tableComment != null) {
                    parameters.put(TABLE_COMMENT, tableComment);
                }
                Table.Builder builder = Table.builder().setDatabaseName(database).setTableName(tableName).setOwner(owner.orElseThrow(() -> new IllegalStateException("Owner not set"))).setTableType(PrestoTableType.EXTERNAL_TABLE).setDataColumns(toHiveColumns(metadata.schema().columns())).withStorage(storage -> storage.setLocation(metadata.location())).withStorage(storage -> storage.setStorageFormat(STORAGE_FORMAT)).setParameters(parameters);
                table = builder.build();
            } else {
                Table currentTable = getTable();
                checkState(currentMetadataLocation != null, "No current metadata location for existing table");
                String metadataLocation = currentTable.getParameters().get(METADATA_LOCATION);
                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, newMetadataLocation).setParameter(PREVIOUS_METADATA_LOCATION, currentMetadataLocation).build();
            }
        } catch (RuntimeException e) {
            try {
                io().deleteFile(newMetadataLocation);
            } catch (RuntimeException exception) {
                e.addSuppressed(exception);
            }
            throw e;
        }
        PrestoPrincipal owner = new PrestoPrincipal(USER, table.getOwner());
        PrincipalPrivileges privileges = new PrincipalPrivileges(ImmutableMultimap.<String, HivePrivilegeInfo>builder().put(table.getOwner(), new HivePrivilegeInfo(SELECT, true, owner, owner)).put(table.getOwner(), new HivePrivilegeInfo(INSERT, true, owner, owner)).put(table.getOwner(), new HivePrivilegeInfo(UPDATE, true, owner, owner)).put(table.getOwner(), new HivePrivilegeInfo(DELETE, true, owner, owner)).build(), ImmutableMultimap.of());
        if (base == null) {
            metastore.createTable(metastoreContext, table, privileges);
        } else {
            metastore.replaceTable(metastoreContext, database, tableName, table, privileges);
        }
    } finally {
        shouldRefresh = true;
        try {
            lockId.ifPresent(id -> metastore.unlock(metastoreContext, id));
        } catch (Exception e) {
            log.error(e, "Failed to unlock: %s", lockId.orElse(null));
        } finally {
            tableLevelMutex.unlock();
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) HdfsEnvironment(com.facebook.presto.hive.HdfsEnvironment) LocationProviders(org.apache.iceberg.LocationProviders) LoadingCache(com.google.common.cache.LoadingCache) IcebergUtil.isIcebergTable(com.facebook.presto.iceberg.IcebergUtil.isIcebergTable) PrestoPrincipal(com.facebook.presto.spi.security.PrestoPrincipal) HiveSchemaUtil(org.apache.iceberg.hive.HiveSchemaUtil) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) ICEBERG_TABLE_TYPE_VALUE(org.apache.iceberg.BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE) TABLE_TYPE_PROP(org.apache.iceberg.BaseMetastoreTableOperations.TABLE_TYPE_PROP) TableMetadata(org.apache.iceberg.TableMetadata) LocationProvider(org.apache.iceberg.io.LocationProvider) TableOperations(org.apache.iceberg.TableOperations) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ExtendedHiveMetastore(com.facebook.presto.hive.metastore.ExtendedHiveMetastore) Map(java.util.Map) ICEBERG_INVALID_METADATA(com.facebook.presto.iceberg.IcebergErrorCode.ICEBERG_INVALID_METADATA) HdfsContext(com.facebook.presto.hive.HdfsContext) TableMetadataParser(org.apache.iceberg.TableMetadataParser) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) StorageFormat(com.facebook.presto.hive.metastore.StorageFormat) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) FileInputFormat(org.apache.hadoop.mapred.FileInputFormat) NestedField(org.apache.iceberg.types.Types.NestedField) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SELECT(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.SELECT) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) Objects(java.util.Objects) List(java.util.List) PrestoTableType(com.facebook.presto.hive.metastore.PrestoTableType) Optional(java.util.Optional) HivePrivilegeInfo(com.facebook.presto.hive.metastore.HivePrivilegeInfo) CacheBuilder(com.google.common.cache.CacheBuilder) FileOutputFormat(org.apache.hadoop.mapred.FileOutputFormat) TableMetadataParser.getFileExtension(org.apache.iceberg.TableMetadataParser.getFileExtension) Logger(com.facebook.airlift.log.Logger) Table(com.facebook.presto.hive.metastore.Table) Column(com.facebook.presto.hive.metastore.Column) HiveType(com.facebook.presto.hive.HiveType) WRITE_METADATA_LOCATION(org.apache.iceberg.TableProperties.WRITE_METADATA_LOCATION) OutputFile(org.apache.iceberg.io.OutputFile) HashMap(java.util.HashMap) PrestoException(com.facebook.presto.spi.PrestoException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Objects.requireNonNull(java.util.Objects.requireNonNull) TABLE_COMMENT(com.facebook.presto.hive.HiveMetadata.TABLE_COMMENT) METADATA_COMPRESSION_DEFAULT(org.apache.iceberg.TableProperties.METADATA_COMPRESSION_DEFAULT) DELETE(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.DELETE) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Nullable(javax.annotation.Nullable) USER(com.facebook.presto.spi.security.PrincipalType.USER) INSERT(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.INSERT) UPDATE(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.UPDATE) METADATA_COMPRESSION(org.apache.iceberg.TableProperties.METADATA_COMPRESSION) ReentrantLock(java.util.concurrent.locks.ReentrantLock) LazySimpleSerDe(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe) Integer.parseInt(java.lang.Integer.parseInt) TimeUnit(java.util.concurrent.TimeUnit) UUID.randomUUID(java.util.UUID.randomUUID) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) Tasks(org.apache.iceberg.util.Tasks) FileIO(org.apache.iceberg.io.FileIO) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) HivePrivilegeInfo(com.facebook.presto.hive.metastore.HivePrivilegeInfo) IcebergUtil.isIcebergTable(com.facebook.presto.iceberg.IcebergUtil.isIcebergTable) Table(com.facebook.presto.hive.metastore.Table) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) HashMap(java.util.HashMap) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) PrestoException(com.facebook.presto.spi.PrestoException) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) PrestoPrincipal(com.facebook.presto.spi.security.PrestoPrincipal)

Example 2 with TableMetadata

use of org.apache.iceberg.TableMetadata in project hive by apache.

the class TestHiveCommits method testThriftExceptionUnknownFailedCommit.

/**
 * Pretends we throw an exception while persisting and don't know what happened, can't check to find out,
 * but in reality the commit failed
 */
@Test
public void testThriftExceptionUnknownFailedCommit() throws TException, InterruptedException {
    Table table = catalog.loadTable(TABLE_IDENTIFIER);
    HiveTableOperations ops = (HiveTableOperations) ((HasTableOperations) table).operations();
    TableMetadata metadataV1 = ops.current();
    table.updateSchema().addColumn("n", Types.IntegerType.get()).commit();
    ops.refresh();
    TableMetadata metadataV2 = ops.current();
    Assert.assertEquals(2, ops.current().schema().columns().size());
    HiveTableOperations spyOps = spy(ops);
    failCommitAndThrowException(spyOps);
    breakFallbackCatalogCommitCheck(spyOps);
    AssertHelpers.assertThrows("Should throw CommitStateUnknownException since the catalog check was blocked", CommitStateUnknownException.class, "Datacenter on fire", () -> spyOps.commit(metadataV2, metadataV1));
    ops.refresh();
    Assert.assertEquals("Current metadata should not have changed", metadataV2, ops.current());
    Assert.assertTrue("Current metadata file should still exist", metadataFileExists(ops.current()));
    Assert.assertEquals("Client could not determine outcome so new metadata file should also exist", 3, metadataFileCount(ops.current()));
}
Also used : TableMetadata(org.apache.iceberg.TableMetadata) Table(org.apache.iceberg.Table) Test(org.junit.Test)

Example 3 with TableMetadata

use of org.apache.iceberg.TableMetadata in project hive by apache.

the class TestInputFormatReaderDeletes method createTable.

@Override
protected Table createTable(String name, Schema schema, PartitionSpec spec) throws IOException {
    Table table;
    File location = temp.newFolder(inputFormat, fileFormat.name());
    Assert.assertTrue(location.delete());
    helper = new TestHelper(conf, tables, location.toString(), schema, spec, fileFormat, temp);
    table = helper.createTable();
    TableOperations ops = ((BaseTable) table).operations();
    TableMetadata meta = ops.current();
    ops.commit(meta, meta.upgradeToFormatVersion(2));
    return table;
}
Also used : TableMetadata(org.apache.iceberg.TableMetadata) BaseTable(org.apache.iceberg.BaseTable) Table(org.apache.iceberg.Table) TableOperations(org.apache.iceberg.TableOperations) BaseTable(org.apache.iceberg.BaseTable) File(java.io.File)

Example 4 with TableMetadata

use of org.apache.iceberg.TableMetadata in project hive by apache.

the class HiveCatalog method dropTable.

@Override
public boolean dropTable(TableIdentifier identifier, boolean purge) {
    if (!isValidIdentifier(identifier)) {
        return false;
    }
    String database = identifier.namespace().level(0);
    TableOperations ops = newTableOps(identifier);
    TableMetadata lastMetadata;
    if (purge && ops.current() != null) {
        lastMetadata = ops.current();
    } else {
        lastMetadata = null;
    }
    try {
        clients.run(client -> {
            client.dropTable(database, identifier.name(), false, /* do not delete data */
            false);
            return null;
        });
        if (purge && lastMetadata != null) {
            CatalogUtil.dropTableData(ops.io(), lastMetadata);
        }
        LOG.info("Dropped table: {}", identifier);
        return true;
    } catch (NoSuchTableException | NoSuchObjectException e) {
        LOG.info("Skipping drop, table does not exist: {}", identifier, e);
        return false;
    } catch (TException e) {
        throw new RuntimeException("Failed to drop " + identifier, e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Interrupted in call to dropTable", e);
    }
}
Also used : TableMetadata(org.apache.iceberg.TableMetadata) TException(org.apache.thrift.TException) TableOperations(org.apache.iceberg.TableOperations) BaseMetastoreTableOperations(org.apache.iceberg.BaseMetastoreTableOperations) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException)

Example 5 with TableMetadata

use of org.apache.iceberg.TableMetadata in project hive by apache.

the class TestHiveCommits method testThriftExceptionSuccessOnCommit.

/**
 * Pretends we throw an error while persisting that actually does commit serverside
 */
@Test
public void testThriftExceptionSuccessOnCommit() throws TException, InterruptedException {
    Table table = catalog.loadTable(TABLE_IDENTIFIER);
    HiveTableOperations ops = (HiveTableOperations) ((HasTableOperations) table).operations();
    TableMetadata metadataV1 = ops.current();
    table.updateSchema().addColumn("n", Types.IntegerType.get()).commit();
    ops.refresh();
    TableMetadata metadataV2 = ops.current();
    Assert.assertEquals(2, ops.current().schema().columns().size());
    HiveTableOperations spyOps = spy(ops);
    // Simulate a communication error after a successful commit
    commitAndThrowException(ops, spyOps);
    // Shouldn't throw because the commit actually succeeds even though persistTable throws an exception
    spyOps.commit(metadataV2, metadataV1);
    ops.refresh();
    Assert.assertNotEquals("Current metadata should have changed", metadataV2, ops.current());
    Assert.assertTrue("Current metadata file should still exist", metadataFileExists(ops.current()));
    Assert.assertEquals("Commit should have been successful and new metadata file should be made", 3, metadataFileCount(ops.current()));
}
Also used : TableMetadata(org.apache.iceberg.TableMetadata) Table(org.apache.iceberg.Table) Test(org.junit.Test)

Aggregations

TableMetadata (org.apache.iceberg.TableMetadata)11 Table (org.apache.iceberg.Table)7 Test (org.junit.Test)5 TableOperations (org.apache.iceberg.TableOperations)4 HdfsContext (com.facebook.presto.hive.HdfsContext)2 MetastoreContext (com.facebook.presto.hive.metastore.MetastoreContext)2 PrestoException (com.facebook.presto.spi.PrestoException)2 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Logger (com.facebook.airlift.log.Logger)1 HdfsEnvironment (com.facebook.presto.hive.HdfsEnvironment)1 TABLE_COMMENT (com.facebook.presto.hive.HiveMetadata.TABLE_COMMENT)1 HiveType (com.facebook.presto.hive.HiveType)1 TableAlreadyExistsException (com.facebook.presto.hive.TableAlreadyExistsException)1 Column (com.facebook.presto.hive.metastore.Column)1 Database (com.facebook.presto.hive.metastore.Database)1 ExtendedHiveMetastore (com.facebook.presto.hive.metastore.ExtendedHiveMetastore)1 HivePrivilegeInfo (com.facebook.presto.hive.metastore.HivePrivilegeInfo)1 DELETE (com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.DELETE)1 INSERT (com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.INSERT)1