-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-55855][SQL] DSv2 Transaction API foundations #54642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andreaschat-db
wants to merge
1
commit into
apache:master
Choose a base branch
from
andreaschat-db:dsv2TransactionAPIFoundations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+346
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...lyst/src/main/java/org/apache/spark/sql/connector/catalog/TransactionalCatalogPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.spark.sql.connector.catalog; | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.transactions.Transaction; | ||
| import org.apache.spark.sql.connector.catalog.transactions.TransactionInfo; | ||
|
|
||
| /** | ||
| * A {@link CatalogPlugin} that supports transactions. | ||
| * <p> | ||
| * Catalogs that implement this interface opt in to transactional query execution. A catalog | ||
| * implementing this interface is responsible for starting transactions. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| public interface TransactionalCatalogPlugin extends CatalogPlugin { | ||
|
|
||
| /** | ||
| * Begins a new transaction and returns a {@link Transaction} representing it. | ||
| * | ||
| * @param info metadata about the transaction being started. | ||
| */ | ||
| Transaction beginTransaction(TransactionInfo info); | ||
| } |
77 changes: 77 additions & 0 deletions
77
...talyst/src/main/java/org/apache/spark/sql/connector/catalog/transactions/Transaction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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.spark.sql.connector.catalog.transactions; | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.CatalogPlugin; | ||
| import org.apache.spark.sql.connector.catalog.TransactionalCatalogPlugin; | ||
|
|
||
| import java.io.Closeable; | ||
|
|
||
| /** | ||
| * Represents a transaction. | ||
| * <p> | ||
| * Spark begins a transaction with {@link TransactionalCatalogPlugin#beginTransaction} and | ||
| * executes read/write operations against the transaction's catalog. On success, Spark | ||
| * calls {@link #commit()}; on failure, Spark calls {@link #abort()}. In both cases Spark | ||
| * subsequently calls {@link #close()} to release resources. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| public interface Transaction extends Closeable { | ||
|
|
||
| /** | ||
| * Returns the catalog associated with this transaction. This catalog is responsible for tracking | ||
| * read/write operations that occur within the boundaries of a transaction. This allows | ||
| * connectors to perform conflict resolution at commit time. | ||
| */ | ||
| CatalogPlugin catalog(); | ||
|
|
||
| /** | ||
| * Commits the transaction. All writes performed under it become visible to other readers. | ||
| * <p> | ||
| * The connector is responsible for detecting and resolving conflicting commits or throwing | ||
| * an exception if resolution is not possible. | ||
| * <p> | ||
| * This method must be called exactly once. Spark calls {@link #close()} immediately after | ||
| * this method returns, so implementations should not release resources inside | ||
| * {@code commit()} itself. | ||
| */ | ||
| void commit(); | ||
|
|
||
| /** | ||
| * Aborts the transaction, discarding any staged changes. | ||
| * <p> | ||
| * This method must be idempotent. If the transaction has already been committed or aborted, | ||
| * invoking it must have no effect. | ||
| * <p> | ||
| * Spark calls {@link #close()} immediately after this method returns. | ||
| */ | ||
| void abort(); | ||
|
|
||
| /** | ||
| * Releases any resources held by this transaction. | ||
| * <p> | ||
| * Spark always calls this method after {@link #commit()} or {@link #abort()}, regardless of | ||
| * whether those methods succeed or not. | ||
| * <p> | ||
| * This method must be idempotent. If the transaction has already been closed, | ||
| * invoking it must have no effect. | ||
| */ | ||
| @Override | ||
| void close(); | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
...st/src/main/java/org/apache/spark/sql/connector/catalog/transactions/TransactionInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * 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.spark.sql.connector.catalog.transactions; | ||
|
|
||
| /** | ||
| * Metadata about a transaction. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| public interface TransactionInfo { | ||
|
|
||
| /** | ||
| * Returns a unique identifier for this transaction. | ||
| */ | ||
| String id(); | ||
| } |
55 changes: 55 additions & 0 deletions
55
...catalyst/src/main/scala/org/apache/spark/sql/catalyst/transactions/TransactionUtils.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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.spark.sql.catalyst.transactions | ||
|
|
||
| import java.util.UUID | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.TransactionalCatalogPlugin | ||
| import org.apache.spark.sql.connector.catalog.transactions.{Transaction, TransactionInfoImpl} | ||
| import org.apache.spark.util.Utils | ||
|
|
||
| object TransactionUtils { | ||
| def commit(transaction: Transaction): Unit = { | ||
| Utils.tryWithSafeFinally { | ||
| transaction.commit() | ||
| } { | ||
| transaction.close() | ||
| } | ||
| } | ||
|
|
||
| def abort(transaction: Transaction): Unit = { | ||
| Utils.tryWithSafeFinally { | ||
| transaction.abort() | ||
| } { | ||
| transaction.close() | ||
| } | ||
| } | ||
|
|
||
| def beginTransaction(catalog: TransactionalCatalogPlugin): Transaction = { | ||
| val info = TransactionInfoImpl(id = UUID.randomUUID.toString) | ||
| val transaction = catalog.beginTransaction(info) | ||
| if (transaction.catalog.name != catalog.name) { | ||
| abort(transaction) | ||
| throw new IllegalStateException( | ||
| s"""Transaction catalog name (${transaction.catalog.name}) | ||
| |must match original catalog name (${catalog.name}). | ||
| |""".stripMargin) | ||
| } | ||
| transaction | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
.../main/scala/org/apache/spark/sql/connector/catalog/transactions/TransactionInfoImpl.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * 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.spark.sql.connector.catalog.transactions | ||
|
|
||
| case class TransactionInfoImpl(id: String) extends TransactionInfo |
124 changes: 124 additions & 0 deletions
124
...yst/src/test/scala/org/apache/spark/sql/catalyst/transactions/TransactionUtilsSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * 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.spark.sql.catalyst.transactions | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.connector.catalog.{CatalogPlugin, TransactionalCatalogPlugin} | ||
| import org.apache.spark.sql.connector.catalog.transactions.{Transaction, TransactionInfo} | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| class TransactionUtilsSuite extends SparkFunSuite { | ||
| val testCatalogName = "test_catalog" | ||
|
|
||
| // --- Helpers --------------------------------------------------------------- | ||
| private def mockCatalog(catalogName: String): CatalogPlugin = new CatalogPlugin { | ||
| override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = () | ||
| override def name(): String = catalogName | ||
| } | ||
|
|
||
| private val emptyFunction = () => () | ||
| private class TestTransaction( | ||
| catalogName: String, | ||
| onCommit: () => Unit = emptyFunction, | ||
| onAbort: () => Unit = emptyFunction, | ||
| onClose: () => Unit = emptyFunction) extends Transaction { | ||
| var committed = false | ||
| var aborted = false | ||
| var closed = false | ||
|
|
||
| override def catalog(): CatalogPlugin = mockCatalog(catalogName) | ||
| override def commit(): Unit = { committed = true; onCommit() } | ||
| override def abort(): Unit = { aborted = true; onAbort() } | ||
| override def close(): Unit = { closed = true; onClose() } | ||
| } | ||
|
|
||
| private def mockTransactionalCatalog( | ||
| catalogName: String, | ||
| txnCatalogName: String = null): TransactionalCatalogPlugin = { | ||
| val resolvedTxnCatalogName = Option(txnCatalogName).getOrElse(catalogName) | ||
| new TransactionalCatalogPlugin { | ||
| override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = () | ||
| override def name(): String = catalogName | ||
| override def beginTransaction(info: TransactionInfo): Transaction = | ||
| new TestTransaction(resolvedTxnCatalogName) | ||
| } | ||
| } | ||
|
|
||
| // --- Commit ---------------------------------------------------------------- | ||
| test("commit: calls commit then close") { | ||
| val txn = new TestTransaction(testCatalogName) | ||
| TransactionUtils.commit(txn) | ||
| assert(txn.committed) | ||
| assert(txn.closed) | ||
| } | ||
|
|
||
| test("commit: close is called even if commit fails") { | ||
| val txn = new TestTransaction( | ||
| testCatalogName, onCommit = () => throw new RuntimeException("commit failed")) | ||
| intercept[RuntimeException] { TransactionUtils.commit(txn) } | ||
| assert(txn.closed) | ||
| } | ||
|
|
||
| // --- Abort ----------------------------------------------------------------- | ||
| test("abort: calls abort then close") { | ||
| val txn = new TestTransaction(testCatalogName) | ||
| TransactionUtils.abort(txn) | ||
| assert(txn.aborted) | ||
| assert(txn.closed) | ||
| } | ||
|
|
||
| test("abort: close is called even if abort fails") { | ||
| val txn = new TestTransaction(testCatalogName, | ||
| onAbort = () => throw new RuntimeException("abort failed")) | ||
| intercept[RuntimeException] { TransactionUtils.abort(txn) } | ||
| assert(txn.closed) | ||
| } | ||
|
|
||
| // --- Begin Transaction ----------------------------------------------------- | ||
| test("beginTransaction: returns transaction when catalog names match") { | ||
| val catalog = mockTransactionalCatalog(testCatalogName) | ||
| val txn = TransactionUtils.beginTransaction(catalog) | ||
| assert(txn.catalog().name() == testCatalogName) | ||
| } | ||
|
|
||
| test("beginTransaction: fails when transaction catalog name does not match") { | ||
| val catalog = mockTransactionalCatalog(catalogName = testCatalogName, txnCatalogName = "other") | ||
| val e = intercept[IllegalStateException] { | ||
| TransactionUtils.beginTransaction(catalog) | ||
| } | ||
| assert(e.getMessage.contains("other")) | ||
| assert(e.getMessage.contains(testCatalogName)) | ||
| } | ||
|
|
||
| test("beginTransaction: aborts and closes transaction on catalog name mismatch") { | ||
| var aborted = false | ||
| var closed = false | ||
| val catalog = new TransactionalCatalogPlugin { | ||
| override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = () | ||
| override def name(): String = testCatalogName | ||
| override def beginTransaction(info: TransactionInfo): Transaction = | ||
| new TestTransaction( | ||
| "other", | ||
| onAbort = () => { aborted = true }, | ||
| onClose = () => { closed = true }) | ||
| } | ||
| intercept[IllegalStateException] { TransactionUtils.beginTransaction(catalog) } | ||
| assert(aborted) | ||
| assert(closed) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Must be called exactly once" sounds like it is controlled by connector. I think a better way to say "will be called exactly once".
I am also not sure about the last sentence on releasing resources. Instead of saying "should not release resources", I think we better describe the sequencing of calls so that connectors make an informed call on their end when to do what.