Search in sources :

Example 1 with NessieNotFoundException

use of org.projectnessie.error.NessieNotFoundException 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.
}
Also used : TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) AlreadyExistsException(org.apache.iceberg.exceptions.AlreadyExistsException) HttpClientBuilder(org.projectnessie.client.http.HttpClientBuilder) CatalogUtil(org.apache.iceberg.CatalogUtil) ImmutableMap(org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap) CommitStateUnknownException(org.apache.iceberg.exceptions.CommitStateUnknownException) LoggerFactory(org.slf4j.LoggerFactory) HadoopFileIO(org.apache.iceberg.hadoop.HadoopFileIO) Function(java.util.function.Function) Reference(org.projectnessie.model.Reference) NessieClientBuilder(org.projectnessie.client.NessieClientBuilder) HttpClientException(org.projectnessie.client.http.HttpClientException) NessieConflictException(org.projectnessie.error.NessieConflictException) CatalogProperties(org.apache.iceberg.CatalogProperties) TableOperations(org.apache.iceberg.TableOperations) NoSuchNamespaceException(org.apache.iceberg.exceptions.NoSuchNamespaceException) Map(java.util.Map) Configuration(org.apache.hadoop.conf.Configuration) BaseMetastoreCatalog(org.apache.iceberg.BaseMetastoreCatalog) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) Namespace(org.apache.iceberg.catalog.Namespace) Content(org.projectnessie.model.Content) Configurable(org.apache.hadoop.conf.Configurable) SupportsNamespaces(org.apache.iceberg.catalog.SupportsNamespaces) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) Operation(org.projectnessie.model.Operation) Logger(org.slf4j.Logger) TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) Branch(org.projectnessie.model.Branch) Set(java.util.Set) Collectors(java.util.stream.Collectors) Joiner(org.apache.iceberg.relocated.com.google.common.base.Joiner) NessieApiV1(org.projectnessie.client.api.NessieApiV1) List(java.util.List) Stream(java.util.stream.Stream) NessieConfigConstants(org.projectnessie.client.NessieConfigConstants) IcebergTable(org.projectnessie.model.IcebergTable) Tasks(org.apache.iceberg.util.Tasks) DynMethods(org.apache.iceberg.common.DynMethods) Preconditions(org.apache.iceberg.relocated.com.google.common.base.Preconditions) Tag(org.projectnessie.model.Tag) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException) ContentKey(org.projectnessie.model.ContentKey) FileIO(org.apache.iceberg.io.FileIO) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) VisibleForTesting(org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting) TableReference(org.projectnessie.model.TableReference) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) AlreadyExistsException(org.apache.iceberg.exceptions.AlreadyExistsException) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) CommitStateUnknownException(org.apache.iceberg.exceptions.CommitStateUnknownException) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) HttpClientException(org.projectnessie.client.http.HttpClientException) Branch(org.projectnessie.model.Branch) IcebergTable(org.projectnessie.model.IcebergTable) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException)

Example 2 with NessieNotFoundException

use of org.projectnessie.error.NessieNotFoundException 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;
}
Also used : AlreadyExistsException(org.apache.iceberg.exceptions.AlreadyExistsException) HttpClientBuilder(org.projectnessie.client.http.HttpClientBuilder) CatalogUtil(org.apache.iceberg.CatalogUtil) ImmutableMap(org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap) CommitStateUnknownException(org.apache.iceberg.exceptions.CommitStateUnknownException) LoggerFactory(org.slf4j.LoggerFactory) HadoopFileIO(org.apache.iceberg.hadoop.HadoopFileIO) Function(java.util.function.Function) Reference(org.projectnessie.model.Reference) NessieClientBuilder(org.projectnessie.client.NessieClientBuilder) HttpClientException(org.projectnessie.client.http.HttpClientException) NessieConflictException(org.projectnessie.error.NessieConflictException) CatalogProperties(org.apache.iceberg.CatalogProperties) TableOperations(org.apache.iceberg.TableOperations) NoSuchNamespaceException(org.apache.iceberg.exceptions.NoSuchNamespaceException) Map(java.util.Map) Configuration(org.apache.hadoop.conf.Configuration) BaseMetastoreCatalog(org.apache.iceberg.BaseMetastoreCatalog) NoSuchTableException(org.apache.iceberg.exceptions.NoSuchTableException) Namespace(org.apache.iceberg.catalog.Namespace) Content(org.projectnessie.model.Content) Configurable(org.apache.hadoop.conf.Configurable) SupportsNamespaces(org.apache.iceberg.catalog.SupportsNamespaces) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) Operation(org.projectnessie.model.Operation) Logger(org.slf4j.Logger) TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) Branch(org.projectnessie.model.Branch) Set(java.util.Set) Collectors(java.util.stream.Collectors) Joiner(org.apache.iceberg.relocated.com.google.common.base.Joiner) NessieApiV1(org.projectnessie.client.api.NessieApiV1) List(java.util.List) Stream(java.util.stream.Stream) NessieConfigConstants(org.projectnessie.client.NessieConfigConstants) IcebergTable(org.projectnessie.model.IcebergTable) Tasks(org.apache.iceberg.util.Tasks) DynMethods(org.apache.iceberg.common.DynMethods) Preconditions(org.apache.iceberg.relocated.com.google.common.base.Preconditions) Tag(org.projectnessie.model.Tag) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException) ContentKey(org.projectnessie.model.ContentKey) FileIO(org.apache.iceberg.io.FileIO) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) VisibleForTesting(org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting) TableReference(org.projectnessie.model.TableReference) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) Branch(org.projectnessie.model.Branch) IcebergTable(org.projectnessie.model.IcebergTable) NessieConflictException(org.projectnessie.error.NessieConflictException) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException)

Example 3 with NessieNotFoundException

use of org.projectnessie.error.NessieNotFoundException 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);
        }
    }
}
Also used : ImmutableCommitMeta(org.projectnessie.model.ImmutableCommitMeta) CommitStateUnknownException(org.apache.iceberg.exceptions.CommitStateUnknownException) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) Snapshot(org.apache.iceberg.Snapshot) HttpClientException(org.projectnessie.client.http.HttpClientException) Branch(org.projectnessie.model.Branch) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) IcebergTable(org.projectnessie.model.IcebergTable) NessieConflictException(org.projectnessie.error.NessieConflictException) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException)

Example 4 with NessieNotFoundException

use of org.projectnessie.error.NessieNotFoundException in project nessie by projectnessie.

the class AbstractSparkSqlTest method testAssignBranchTo.

@Test
void testAssignBranchTo() throws NessieConflictException, NessieNotFoundException {
    String random = "randomBranch";
    assertThat(sql("CREATE BRANCH %s IN nessie", random)).containsExactly(row("Branch", random, hash));
    commitAndReturnLog(refName);
    sql("USE REFERENCE %s IN nessie", refName);
    sql("MERGE BRANCH %s INTO main IN nessie", refName);
    Reference main = api.getReference().refName("main").get();
    assertThat(sql("ASSIGN BRANCH %s TO main IN nessie", random)).containsExactly(row("Branch", random, main.getHash()));
    for (Object[] commit : fetchLog("main")) {
        String currentHash = (String) commit[2];
        assertThat(sql("ASSIGN BRANCH %s TO main AT %s IN nessie", random, currentHash)).containsExactly(row("Branch", random, currentHash));
    }
    String invalidHash = "abc";
    String unknownHash = "dd8d46a3dd5478ce69749a5455dba29d74f6d1171188f4c21d0e15ff4a0a9a9c";
    String invalidBranch = "invalidBranch";
    assertThatThrownBy(() -> sql("ASSIGN BRANCH %s TO main AT %s IN nessie", random, invalidHash)).isInstanceOf(IllegalArgumentException.class).hasMessage(Validation.HASH_MESSAGE + " - but was: " + invalidHash);
    assertThatThrownBy(() -> sql("ASSIGN BRANCH %s TO main AT %s IN nessie", random, unknownHash)).isInstanceOf(NessieNotFoundException.class).hasMessage(String.format("Could not find commit '%s' in reference '%s'.", unknownHash, "main"));
    assertThatThrownBy(() -> sql("ASSIGN BRANCH %s TO %s AT %s IN nessie", random, invalidBranch, hash)).isInstanceOf(NessieNotFoundException.class).hasMessage(String.format("Named reference '%s' not found", invalidBranch));
}
Also used : Reference(org.projectnessie.model.Reference) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) Test(org.junit.jupiter.api.Test)

Example 5 with NessieNotFoundException

use of org.projectnessie.error.NessieNotFoundException in project nessie by projectnessie.

the class AbstractSparkSqlTest method testAssignTagTo.

@Test
void testAssignTagTo() throws NessieConflictException, NessieNotFoundException {
    String random = "randomTag";
    assertThat(sql("CREATE TAG %s IN nessie", random)).containsExactly(row("Tag", random, hash));
    List<Object[]> commits = commitAndReturnLog(refName);
    sql("USE REFERENCE %s IN nessie", refName);
    sql("MERGE BRANCH %s INTO main IN nessie", refName);
    Reference main = api.getReference().refName("main").get();
    assertThat(sql("ASSIGN TAG %s TO main IN nessie", random)).containsExactly(row("Tag", random, main.getHash()));
    commits = fetchLog("main");
    for (Object[] commit : commits) {
        String currentHash = (String) commit[2];
        assertThat(sql("ASSIGN TAG %s TO main AT %s IN nessie", random, currentHash)).containsExactly(row("Tag", random, currentHash));
    }
    String invalidHash = "abc";
    String unknownHash = "dd8d46a3dd5478ce69749a5455dba29d74f6d1171188f4c21d0e15ff4a0a9a9c";
    String invalidTag = "invalidTag";
    assertThatThrownBy(() -> sql("ASSIGN TAG %s TO main AT %s IN nessie", random, invalidHash)).isInstanceOf(IllegalArgumentException.class).hasMessage(Validation.HASH_MESSAGE + " - but was: " + invalidHash);
    assertThatThrownBy(() -> sql("ASSIGN TAG %s TO main AT %s IN nessie", random, unknownHash)).isInstanceOf(NessieNotFoundException.class).hasMessage(String.format("Could not find commit '%s' in reference '%s'.", unknownHash, "main"));
    assertThatThrownBy(() -> sql("ASSIGN TAG %s TO %s AT %s IN nessie", random, invalidTag, hash)).isInstanceOf(NessieNotFoundException.class).hasMessage(String.format("Named reference '%s' not found", invalidTag));
}
Also used : Reference(org.projectnessie.model.Reference) NessieNotFoundException(org.projectnessie.error.NessieNotFoundException) Test(org.junit.jupiter.api.Test)

Aggregations

NessieNotFoundException (org.projectnessie.error.NessieNotFoundException)19 Content (org.projectnessie.model.Content)12 ContentKey (org.projectnessie.model.ContentKey)10 Map (java.util.Map)8 Branch (org.projectnessie.model.Branch)8 Reference (org.projectnessie.model.Reference)8 NessieConflictException (org.projectnessie.error.NessieConflictException)7 List (java.util.List)5 Stream (java.util.stream.Stream)5 CommitFailedException (org.apache.iceberg.exceptions.CommitFailedException)5 CommitStateUnknownException (org.apache.iceberg.exceptions.CommitStateUnknownException)5 NoSuchTableException (org.apache.iceberg.exceptions.NoSuchTableException)5 HttpClientException (org.projectnessie.client.http.HttpClientException)5 Operation (org.projectnessie.model.Operation)5 Instant (java.time.Instant)4 Set (java.util.Set)4 FetchOption (org.projectnessie.api.params.FetchOption)4 CommitMultipleOperationsBuilder (org.projectnessie.client.api.CommitMultipleOperationsBuilder)4 CommitMeta (org.projectnessie.model.CommitMeta)4 HashMap (java.util.HashMap)3