You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "amogh-jahagirdar (via GitHub)" <gi...@apache.org> on 2023/02/06 20:23:02 UTC

[GitHub] [iceberg] amogh-jahagirdar commented on a diff in pull request #6757: API, Core: Multi-Table transactions

amogh-jahagirdar commented on code in PR #6757:
URL: https://github.com/apache/iceberg/pull/6757#discussion_r1097784829


##########
core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalogTransaction.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.jdbc;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.time.Instant;
+import org.apache.iceberg.BaseCatalogTransaction;
+import org.apache.iceberg.BaseTable;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.Transaction;
+import org.apache.iceberg.TransactionalCatalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+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.util.Tasks;
+
+public class JdbcCatalogTransaction extends BaseCatalogTransaction {
+  private final JdbcClientPool connections;
+
+  public JdbcCatalogTransaction(
+      TransactionalCatalog origin, IsolationLevel isolationLevel, JdbcClientPool connections) {
+    super(origin, isolationLevel);
+    this.connections = connections;
+    beginTransaction();
+  }
+
+  private void beginTransaction() {
+    try {
+      connections.run(
+          conn -> {
+            try (PreparedStatement sql = conn.prepareStatement(JdbcUtil.BEGIN_TX)) {
+              return sql.execute();
+            }
+          });
+    } catch (SQLException | InterruptedException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public void commitTransaction() {
+    Preconditions.checkState(!hasCommitted(), "Transaction has already committed changes");
+    txByTable().forEach((key, value) -> updatesByTable().put(key, value.pendingUpdates()));
+
+    try {
+      if (IsolationLevel.SERIALIZABLE_WITH_FIXED_READS == isolationLevel()
+          || IsolationLevel.SERIALIZABLE_WITH_LOADING_READS == isolationLevel()) {
+        // make sure tables that are participating in the TX haven't been updated externally
+        for (TableIdentifier affectedTable : updatesByTable().keySet()) {
+          TableMetadata tableMetadata =
+              ((BaseTable) origin().loadTable(affectedTable)).operations().current();
+          if (tableMetadata.lastUpdatedMillis() >= txStartTime().toEpochMilli()) {

Review Comment:
   This goes back to my point above, I think checking tableMetadata last updated is too broad when performing the isolation level checks, I think it needs to be performed at the branch level. Unless we're establishing that multi-table transactions can only happen on each table's main branch but that feels too restrictive. Also not sure if we can just do a timestamp check (clock skew between the last updated time set and the start time of the transaction). 



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org