use of org.projectnessie.model.IcebergTable in project iceberg by apache.
the class NessieCatalog method renameTable.
@Override
public void renameTable(TableIdentifier from, TableIdentifier toOriginal) {
reference.checkMutable();
TableIdentifier to = NessieUtil.removeCatalogName(toOriginal, name());
IcebergTable existingFromTable = table(from);
if (existingFromTable == null) {
throw new NoSuchTableException("table %s doesn't exists", from.name());
}
IcebergTable existingToTable = table(to);
if (existingToTable != null) {
throw new AlreadyExistsException("table %s already exists", to.name());
}
CommitMultipleOperationsBuilder operations = api.commitMultipleOperations().commitMeta(NessieUtil.buildCommitMetadata(String.format("Iceberg rename table from '%s' to '%s'", from, to), catalogOptions)).operation(Operation.Put.of(NessieUtil.toKey(to), existingFromTable, existingFromTable)).operation(Operation.Delete.of(NessieUtil.toKey(from)));
try {
Tasks.foreach(operations).retry(5).stopRetryOn(NessieNotFoundException.class).throwFailureWhenFinished().onFailure((o, exception) -> refresh()).run(ops -> {
Branch branch = ops.branch(reference.getAsBranch()).commit();
reference.updateReference(branch);
}, BaseNessieClientServerException.class);
} catch (NessieNotFoundException e) {
// and removed by another.
throw new RuntimeException("Failed to drop table as ref is no longer valid.", e);
} catch (BaseNessieClientServerException e) {
throw new CommitFailedException(e, "Failed to rename table: the current reference is not up to date.");
} catch (HttpClientException ex) {
// safe than sorry.
throw new CommitStateUnknownException(ex);
}
// Intentionally just "throw through" Nessie's HttpClientException here and do not "special case"
// just the "timeout" variant to propagate all kinds of network errors (e.g. connection reset).
// Network code implementation details and all kinds of network devices can induce unexpected
// behavior. So better be safe than sorry.
}
use of org.projectnessie.model.IcebergTable in project iceberg by apache.
the class NessieCatalog method dropTable.
@Override
public boolean dropTable(TableIdentifier identifier, boolean purge) {
reference.checkMutable();
IcebergTable existingTable = table(identifier);
if (existingTable == null) {
return false;
}
if (purge) {
LOG.info("Purging data for table {} was set to true but is ignored", identifier.toString());
}
CommitMultipleOperationsBuilder commitBuilderBase = api.commitMultipleOperations().commitMeta(NessieUtil.buildCommitMetadata(String.format("Iceberg delete table %s", identifier), catalogOptions)).operation(Operation.Delete.of(NessieUtil.toKey(identifier)));
// We try to drop the table. Simple retry after ref update.
boolean threw = true;
try {
Tasks.foreach(commitBuilderBase).retry(5).stopRetryOn(NessieNotFoundException.class).throwFailureWhenFinished().onFailure((o, exception) -> refresh()).run(commitBuilder -> {
Branch branch = commitBuilder.branch(reference.getAsBranch()).commit();
reference.updateReference(branch);
}, BaseNessieClientServerException.class);
threw = false;
} catch (NessieConflictException e) {
LOG.error("Cannot drop table: failed after retry (update ref and retry)", e);
} catch (NessieNotFoundException e) {
LOG.error("Cannot drop table: ref is no longer valid.", e);
} catch (BaseNessieClientServerException e) {
LOG.error("Cannot drop table: unknown error", e);
}
return !threw;
}
use of org.projectnessie.model.IcebergTable in project iceberg by apache.
the class NessieTableOperations method doCommit.
@Override
protected void doCommit(TableMetadata base, TableMetadata metadata) {
reference.checkMutable();
String newMetadataLocation = writeNewMetadata(metadata, currentVersion() + 1);
boolean delete = true;
try {
ImmutableIcebergTable.Builder newTableBuilder = ImmutableIcebergTable.builder();
if (table != null) {
newTableBuilder.id(table.getId());
}
Snapshot snapshot = metadata.currentSnapshot();
long snapshotId = snapshot != null ? snapshot.snapshotId() : -1L;
IcebergTable newTable = newTableBuilder.snapshotId(snapshotId).schemaId(metadata.currentSchemaId()).specId(metadata.defaultSpecId()).sortOrderId(metadata.defaultSortOrderId()).metadataLocation(newMetadataLocation).build();
LOG.debug("Committing '{}' against '{}': {}", key, reference.getReference(), newTable);
ImmutableCommitMeta.Builder builder = ImmutableCommitMeta.builder();
builder.message(buildCommitMsg(base, metadata));
if (isSnapshotOperation(base, metadata)) {
builder.putProperties("iceberg.operation", snapshot.operation());
}
Branch branch = api.commitMultipleOperations().operation(Operation.Put.of(key, newTable, table)).commitMeta(NessieUtil.catalogOptions(builder, catalogOptions).build()).branch(reference.getAsBranch()).commit();
reference.updateReference(branch);
delete = false;
} catch (NessieConflictException ex) {
throw new CommitFailedException(ex, "Commit failed: Reference hash is out of date. " + "Update the reference %s and try again", reference.getName());
} catch (HttpClientException ex) {
// Intentionally catch all nessie-client-exceptions here and not just the "timeout" variant
// to catch all kinds of network errors (e.g. connection reset). Network code implementation
// details and all kinds of network devices can induce unexpected behavior. So better be
// safe than sorry.
delete = false;
throw new CommitStateUnknownException(ex);
} catch (NessieNotFoundException ex) {
throw new RuntimeException(String.format("Commit failed: Reference %s no longer exist", reference.getName()), ex);
} finally {
if (delete) {
io().deleteFile(newMetadataLocation);
}
}
}
use of org.projectnessie.model.IcebergTable in project iceberg by apache.
the class TestBranchVisibility method verifyRefState.
private void verifyRefState(NessieCatalog catalog, TableIdentifier identifier, long snapshotId, int schemaId) throws Exception {
IcebergTable icebergTable = loadIcebergTable(catalog, identifier);
Assertions.assertThat(icebergTable).extracting(IcebergTable::getSnapshotId, IcebergTable::getSchemaId).containsExactly(snapshotId, schemaId);
}
use of org.projectnessie.model.IcebergTable in project iceberg by apache.
the class TestNessieTable method testFailure.
@Test
public void testFailure() throws NessieNotFoundException, NessieConflictException {
Table icebergTable = catalog.loadTable(TABLE_IDENTIFIER);
Branch branch = (Branch) api.getReference().refName(BRANCH).get();
IcebergTable table = getTable(BRANCH, KEY);
IcebergTable value = IcebergTable.of("dummytable.metadata.json", 42, 42, 42, 42, "cid");
api.commitMultipleOperations().branch(branch).operation(Operation.Put.of(KEY, value)).commitMeta(CommitMeta.fromMessage("")).commit();
Assertions.assertThatThrownBy(() -> icebergTable.updateSchema().addColumn("data", Types.LongType.get()).commit()).isInstanceOf(CommitFailedException.class).hasMessage("Commit failed: Reference hash is out of date. Update the reference iceberg-table-test and try again");
}
Aggregations