Skip to content

Commit

Permalink
Nessie: Use memoized Supplier to avoid unnecessary API calls (apache#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nastra authored Apr 20, 2022
1 parent 9618147 commit e26e364
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.collect.BiMap;
import com.google.common.collect.FluentIterable;
Expand Down Expand Up @@ -92,6 +93,7 @@ public class GuavaClasses {
ThreadFactoryBuilder.class.getName();
Iterables.class.getName();
CountingOutputStream.class.getName();
Suppliers.class.getName();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
import org.apache.iceberg.exceptions.NoSuchNamespaceException;
import org.apache.iceberg.exceptions.NoSuchTableException;
import org.apache.iceberg.relocated.com.google.common.base.Suppliers;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.util.Tasks;
import org.projectnessie.client.NessieConfigConstants;
Expand Down Expand Up @@ -70,7 +71,7 @@ public NessieIcebergClient(
NessieApiV1 api, String requestedRef, String requestedHash, Map<String, String> catalogOptions) {
this.api = api;
this.catalogOptions = catalogOptions;
this.reference = () -> loadReference(requestedRef, requestedHash);
this.reference = Suppliers.memoize(() -> loadReference(requestedRef, requestedHash));
}

public NessieApiV1 getApi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.iceberg.TableOperations;
import org.apache.iceberg.avro.AvroSchemaUtil;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.types.Types;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -321,31 +322,22 @@ public void testExistingTableUpdate() {
}

@Test
public void testCommitsOutsideOfCatalogApi() throws NessieNotFoundException, NessieConflictException {
public void testFailure() throws NessieNotFoundException, NessieConflictException {
Table icebergTable = catalog.loadTable(TABLE_IDENTIFIER);
Branch branch = (Branch) api.getReference().refName(BRANCH).get();
Assertions.assertThat(api.getCommitLog().refName(BRANCH).get().getLogEntries())
.extracting(e -> e.getCommitMeta().getHash())
.hasSize(1)
.containsExactly(branch.getHash());

IcebergTable table = getTable(BRANCH, KEY);

IcebergTable value = IcebergTable.of("dummytable.metadata.json", 42, 42, 42, 42, "cid");
// we do a separate manual commit outside of the catalog API
Branch commit = api.commitMultipleOperations().branch(branch)
api.commitMultipleOperations().branch(branch)
.operation(Operation.Put.of(KEY, value))
.commitMeta(CommitMeta.fromMessage(""))
.commit();
Assertions.assertThat(api.getCommitLog().refName(BRANCH).get().getLogEntries())
.extracting(e -> e.getCommitMeta().getHash())
.hasSize(2)
.containsExactly(commit.getHash(), branch.getHash());

icebergTable.updateSchema().addColumn("data", Types.LongType.get()).commit();
Branch latest = (Branch) api.getReference().refName(BRANCH).get();
Assertions.assertThat(api.getCommitLog().refName(BRANCH).get().getLogEntries())
.extracting(e -> e.getCommitMeta().getHash())
.hasSize(3)
.containsExactly(latest.getHash(), commit.getHash(), branch.getHash());
Assertions.assertThatThrownBy(() -> icebergTable.updateSchema().addColumn("data", Types.LongType.get()).commit())
.isInstanceOf(CommitFailedException.class)
.hasMessage(
"Cannot commit: Reference hash is out of date. Update the reference 'iceberg-table-test' and try again");
}

@Test
Expand Down

0 comments on commit e26e364

Please sign in to comment.