rdblue commented on code in PR #6948: URL: https://github.com/apache/iceberg/pull/6948#discussion_r1252364174
########## core/src/test/java/org/apache/iceberg/catalog/CatalogTransactionTests.java: ########## @@ -0,0 +1,736 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg.catalog; + +import static org.apache.iceberg.catalog.CatalogTransaction.IsolationLevel.SERIALIZABLE; +import static org.apache.iceberg.catalog.CatalogTransaction.IsolationLevel.SNAPSHOT; +import static org.apache.iceberg.types.Types.NestedField.required; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotSummary; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TestCatalogUtil; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.types.Types; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public abstract class CatalogTransactionTests< + C extends SupportsCatalogTransactions & SupportsNamespaces & Catalog> { + + @TempDir protected Path metadataDir; + + protected static final Schema SCHEMA = + new Schema( + required(3, "id", Types.IntegerType.get()), required(4, "data", Types.StringType.get())); + + // Partition spec used to create tables + protected static final PartitionSpec SPEC = + PartitionSpec.builderFor(SCHEMA).bucket("data", 16).build(); + + protected static final DataFile FILE_A = + DataFiles.builder(SPEC) + .withPath("/path/to/data-a.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=0") // easy way to set partition data for now + .withRecordCount(1) + .build(); + protected static final DataFile FILE_B = + DataFiles.builder(SPEC) + .withPath("/path/to/data-b.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=1") // easy way to set partition data for now + .withRecordCount(1) + .build(); + protected static final DataFile FILE_C = + DataFiles.builder(SPEC) + .withPath("/path/to/data-c.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=2") // easy way to set partition data for now + .withRecordCount(1) + .build(); + protected static final DataFile FILE_D = + DataFiles.builder(SPEC) + .withPath("/path/to/data-d.parquet") + .withFileSizeInBytes(10) + .withPartitionPath("data_bucket=3") // easy way to set partition data for now + .withRecordCount(1) + .build(); + + protected abstract C catalog(); + + @Test + public void testNulls() { + assertThatThrownBy(() -> new BaseCatalogTransaction(null, null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid origin catalog: null"); + + assertThatThrownBy(() -> new BaseCatalogTransaction(catalog(), null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid isolation level: null"); + } + + @Test + public void catalogTransactionSupport() { + assertThatThrownBy( + () -> new BaseCatalogTransaction(new TestCatalogUtil.TestCatalog(), SERIALIZABLE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Origin catalog does not support catalog transactions"); + } + + @Test + public void multipleCommits() { + CatalogTransaction catalogTx = catalog().createTransaction(SERIALIZABLE); + catalogTx.commitTransaction(); + assertThatThrownBy(catalogTx::commitTransaction) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Transaction has already committed changes"); + } + + @Test + public void invalidIsolationLevel() { + assertThatThrownBy(() -> catalog().createTransaction(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid isolation level: null"); + } + + @Test + public void catalogTxWithSingleOp() { + catalogTxWithSingleOp(CatalogTransaction.IsolationLevel.SNAPSHOT); + } + + @Test + public void catalogTxWithSingleOpWithSerializable() { + catalogTxWithSingleOp(SERIALIZABLE); + } + + private void catalogTxWithSingleOp(CatalogTransaction.IsolationLevel isolationLevel) { + TableIdentifier identifier = TableIdentifier.of("ns", "tx-with-single-op"); + catalog().createTable(identifier, SCHEMA, SPEC); + + Table one = catalog().loadTable(identifier); + TableMetadata base = ((BaseTable) one).operations().current(); + + CatalogTransaction catalogTransaction = catalog().createTransaction(isolationLevel); + Catalog txCatalog = catalogTransaction.asCatalog(); + txCatalog.loadTable(identifier).newAppend().appendFile(FILE_A).appendFile(FILE_B).commit(); + + assertThat(base).isSameAs(((BaseTable) one).operations().refresh()); + assertThat(base.currentSnapshot()).isNull(); + + catalogTransaction.commitTransaction(); + + TableMetadata updated = ((BaseTable) one).operations().refresh(); + assertThat(base).isNotSameAs(updated); + + Snapshot snapshot = updated.currentSnapshot(); + assertThat(snapshot.summary().get(SnapshotSummary.ADDED_FILES_PROP)).isEqualTo("2"); + assertThat(snapshot.summary().get(SnapshotSummary.ADDED_RECORDS_PROP)).isEqualTo("2"); + } + + @Test + public void txAgainstMultipleTables() { + txAgainstMultipleTables(SNAPSHOT); + } + + @Test + public void txAgainstMultipleTablesWithSerializable() { + txAgainstMultipleTables(SERIALIZABLE); + } + + private void txAgainstMultipleTables(CatalogTransaction.IsolationLevel isolationLevel) { + List<String> tables = Arrays.asList("a", "b", "c"); + for (String tbl : tables) { + catalog().createTable(TableIdentifier.of("ns", tbl), SCHEMA); + } + + TableIdentifier first = TableIdentifier.of("ns", "a"); + TableIdentifier second = TableIdentifier.of("ns", "b"); + TableIdentifier third = TableIdentifier.of("ns", "c"); + Table one = catalog().loadTable(first); + Table two = catalog().loadTable(second); + Table three = catalog().loadTable(third); + + TableMetadata baseMetadataOne = ((BaseTable) one).operations().current(); + TableMetadata baseMetadataTwo = ((BaseTable) two).operations().current(); + TableMetadata baseMetadataThree = ((BaseTable) three).operations().current(); + + CatalogTransaction catalogTransaction = catalog().createTransaction(isolationLevel); + Catalog txCatalog = catalogTransaction.asCatalog(); + + txCatalog.loadTable(first).newFastAppend().appendFile(FILE_A).appendFile(FILE_B).commit(); + assertThat(baseMetadataOne).isSameAs(((BaseTable) one).operations().refresh()); + + txCatalog.loadTable(second).newDelete().deleteFile(FILE_C).commit(); + txCatalog.loadTable(second).newFastAppend().appendFile(FILE_B).appendFile(FILE_C).commit(); + assertThat(baseMetadataTwo).isSameAs(((BaseTable) two).operations().refresh()); + + txCatalog.loadTable(third).newDelete().deleteFile(FILE_A).commit(); + txCatalog.loadTable(third).newAppend().appendFile(FILE_D).commit(); + + assertThat(baseMetadataOne).isSameAs(((BaseTable) one).operations().refresh()); + assertThat(baseMetadataTwo).isSameAs(((BaseTable) two).operations().refresh()); + assertThat(baseMetadataThree).isSameAs(((BaseTable) three).operations().refresh()); + + for (String tbl : tables) { + TableMetadata current = + ((BaseTable) catalog().loadTable(TableIdentifier.of("ns", tbl))).operations().refresh(); + assertThat(current.snapshots()).isEmpty(); Review Comment: I don't think that this should cast to `BaseTable` so much. You could use the `Table` API for this assertion. The `Table` API is much more reliable than casting to an assumed implementation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
