hantangwangd commented on code in PR #6948: URL: https://github.com/apache/iceberg/pull/6948#discussion_r2207898401
########## core/src/main/java/org/apache/iceberg/catalog/BaseCatalogTransaction.java: ########## @@ -0,0 +1,412 @@ +/* + * 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 java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.iceberg.AppendFiles; +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.BaseTransaction; +import org.apache.iceberg.DataTableScan; +import org.apache.iceberg.DeleteFiles; +import org.apache.iceberg.ExpireSnapshots; +import org.apache.iceberg.HasTableOperations; +import org.apache.iceberg.ManageSnapshots; +import org.apache.iceberg.OverwriteFiles; +import org.apache.iceberg.ReplacePartitions; +import org.apache.iceberg.ReplaceSortOrder; +import org.apache.iceberg.RewriteFiles; +import org.apache.iceberg.RewriteManifests; +import org.apache.iceberg.RowDelta; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.Transaction; +import org.apache.iceberg.Transactions; +import org.apache.iceberg.UpdateLocation; +import org.apache.iceberg.UpdatePartitionSpec; +import org.apache.iceberg.UpdateProperties; +import org.apache.iceberg.UpdateSchema; +import org.apache.iceberg.UpdateStatistics; +import org.apache.iceberg.exceptions.CommitStateUnknownException; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.rest.RESTCatalog; +import org.immutables.value.Value; + +public class BaseCatalogTransaction implements CatalogTransaction { + private final Map<TableIdentifier, Transaction> txByTable; + private final Map<TableRef, TableMetadata> initiallyReadTableMetadataByRef; + private final Map<TableIdentifier, Table> initiallyReadTables; + private final IsolationLevel isolationLevel; + private final Catalog origin; + private boolean hasCommitted = false; + + public BaseCatalogTransaction(Catalog origin, IsolationLevel isolationLevel) { + Preconditions.checkArgument(null != origin, "Invalid origin catalog: null"); + Preconditions.checkArgument(null != isolationLevel, "Invalid isolation level: null"); + Preconditions.checkArgument( + origin instanceof SupportsCatalogTransactions, + "Origin catalog does not support catalog transactions"); + this.origin = origin; + this.isolationLevel = isolationLevel; + this.txByTable = Maps.newHashMap(); + this.initiallyReadTableMetadataByRef = Maps.newHashMap(); + this.initiallyReadTables = Maps.newHashMap(); + } + + @Override + public void commitTransaction() { + Preconditions.checkState(!hasCommitted, "Transaction has already committed changes"); + + try { + // write skew is not possible in read-only transactions, so only perform that check if there + // were any pending updates + if (hasUpdates()) { + validateSerializableIsolation(); + } + + List<TableCommit> tableCommits = + txByTable.entrySet().stream() + .filter(e -> e.getValue() instanceof BaseTransaction) + .map( + e -> + TableCommit.create( + e.getKey(), + ((BaseTransaction) e.getValue()).startMetadata(), + ((BaseTransaction) e.getValue()).currentMetadata())) + .collect(Collectors.toList()); + + // TODO: should this be retryable? + // only commit if there were change + if (!tableCommits.isEmpty()) { + // TODO: remove this cast once commitTransaction(..) is defined at the Catalog level + ((RESTCatalog) origin).commitTransaction(tableCommits); + } Review Comment: Do we need some kind of global lock to ensure that no other transactions involving related tables can be concurrently committed between `validateSerializableIsolation()` and `commitTransaction(tableCommits)`? As I understand, once this happens, the SERIALIZABLE isolation level might be violated, we might encounter a write skew. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org