tkalkirill commented on code in PR #13080: URL: https://github.com/apache/ignite/pull/13080#discussion_r3155508276
########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointPessimisticTest.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.junit.Test; + +import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC; +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests transaction savepoint functionality. + * Currently, savepoint API is supported only for pessimistic transactions. + */ +public class TxSavepointPessimisticTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName) + .setCacheConfiguration(new CacheConfiguration<>(DEFAULT_CACHE_NAME) + .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL) + .setBackups(1) + .setCacheMode(CacheMode.PARTITIONED)); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { Review Comment: The same `beforeTest` method is missing. ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointPessimisticTest.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.junit.Test; + +import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC; +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests transaction savepoint functionality. + * Currently, savepoint API is supported only for pessimistic transactions. + */ +public class TxSavepointPessimisticTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName) + .setCacheConfiguration(new CacheConfiguration<>(DEFAULT_CACHE_NAME) + .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL) + .setBackups(1) + .setCacheMode(CacheMode.PARTITIONED)); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testSavepointRejectedForOptimisticTx() throws Exception { + Ignite ignite = startGrid(0); + + try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) { + GridTestUtils.assertThrowsAnyCause(log, + () -> { + tx.savepoint("sp"); + + return null; + }, + IgniteCheckedException.class, + "Savepoints are supported only for PESSIMISTIC transactions."); + } + } + + /** + * @throws Exception If failed. + */ + @Test + public void testSeveralSavepoints() throws Exception { Review Comment: ```suggestion public void testRollabackSeveralSavepoints() throws Exception { ``` ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointPessimisticTest.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.junit.Test; + +import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC; +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests transaction savepoint functionality. + * Currently, savepoint API is supported only for pessimistic transactions. + */ +public class TxSavepointPessimisticTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName) + .setCacheConfiguration(new CacheConfiguration<>(DEFAULT_CACHE_NAME) + .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL) + .setBackups(1) + .setCacheMode(CacheMode.PARTITIONED)); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testSavepointRejectedForOptimisticTx() throws Exception { + Ignite ignite = startGrid(0); Review Comment: Maybe create one node for all tests? ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointPessimisticTest.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.junit.Test; + +import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC; +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests transaction savepoint functionality. + * Currently, savepoint API is supported only for pessimistic transactions. + */ +public class TxSavepointPessimisticTest extends GridCommonAbstractTest { Review Comment: There is not test with release with multiple savepoints. ########## modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java: ########## @@ -290,6 +290,57 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport { */ public void suspend() throws IgniteException; + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. The savepoint keeps the current transaction state and can + * later be used by {@link #rollbackToSavepoint(String)} to discard changes made after it was created. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the same name already exists. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name); + + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. If {@code overwrite} is {@code true} and a savepoint with + * the same name exists, the existing savepoint is replaced with a snapshot of the current transaction state. + * + * @param name Savepoint name. + * @param overwrite Whether to overwrite an existing savepoint with the same name. + * @throws TransactionException If savepoint with the same name already exists and {@code overwrite} is + * {@code false}. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name, boolean overwrite); + + /** + * Rolls back transaction changes to the specified savepoint. + * <p> + * Changes made after the savepoint was created are discarded. Savepoints created after the specified savepoint are + * released. The transaction remains active and can be committed or rolled back after this method returns. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the given name does not exist. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void rollbackToSavepoint(String name); Review Comment: I think it's worth adding `@IgniteExperimental`, the api doesn't seem finished yet. ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointParameterizedTest.java: ########## @@ -0,0 +1,711 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.apache.ignite.transactions.TransactionIsolation; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests for transaction savepoint API. + */ +@RunWith(Parameterized.class) +public class TxSavepointParameterizedTest extends GridCommonAbstractTest { + /** */ + private static Ignite ignite0; + + /** */ + private static Ignite ignite1; + + /** */ + private static Ignite ignite2; + + /** */ + private static Ignite ignite3; + + /** */ + private static Ignite client; + + /** */ + @Parameter(0) + public boolean initKeies; + + /** */ + @Parameter(1) + public boolean useNearCache; + + /** */ + @Parameter(2) + public int backups; + + /** */ + @Parameter(3) + public boolean spKeyOnTxInitiator; + + /** */ + @Parameter(4) + public boolean replicated; + + /** */ + @Parameter(5) + public TransactionIsolation transactionIsolation; + + /** + * Returns data for test. + * @return Test parameters. + */ + @Parameters(name = "initKeies={0}, useNearCache={1}, backups={2}, spKeyOnTxInitiator={3}, replicated={4}, txIsolation={5}") + public static Collection<Object[]> testData() { + return List.of(new Object[][] { + // READ_COMMITTED + // backups = 0 + {true, true, 0, true, false, READ_COMMITTED}, + {true, true, 0, false, false, READ_COMMITTED}, + {true, false, 0, true, false, READ_COMMITTED}, + {true, false, 0, false, false, READ_COMMITTED}, + {false, true, 0, true, false, READ_COMMITTED}, + {false, true, 0, false, false, READ_COMMITTED}, + {false, false, 0, true, false, READ_COMMITTED}, + {false, false, 0, false, false, READ_COMMITTED}, + + // backups = 1 + {true, true, 1, true, false, READ_COMMITTED}, + {true, true, 1, false, false, READ_COMMITTED}, + {true, false, 1, true, false, READ_COMMITTED}, + {true, false, 1, false, false, READ_COMMITTED}, + {false, true, 1, true, false, READ_COMMITTED}, + {false, true, 1, false, false, READ_COMMITTED}, + {false, false, 1, true, false, READ_COMMITTED}, + {false, false, 1, false, false, READ_COMMITTED}, + + // backups = 2 + {true, true, 2, true, false, READ_COMMITTED}, + {true, true, 2, false, false, READ_COMMITTED}, + {true, false, 2, true, false, READ_COMMITTED}, + {true, false, 2, false, false, READ_COMMITTED}, + {false, true, 2, true, false, READ_COMMITTED}, + {false, true, 2, false, false, READ_COMMITTED}, + {false, false, 2, true, false, READ_COMMITTED}, + {false, false, 2, false, false, READ_COMMITTED}, + + // replicated cache. + {true, true, 0, true, true, READ_COMMITTED}, + {true, false, 0, true, true, READ_COMMITTED}, + {false, true, 0, true, true, READ_COMMITTED}, + {false, false, 0, true, true, READ_COMMITTED}, + + // REPEATABLE_READ + // backups = 0 + {true, true, 0, true, false, REPEATABLE_READ}, + {true, true, 0, false, false, REPEATABLE_READ}, + {true, false, 0, true, false, REPEATABLE_READ}, + {true, false, 0, false, false, REPEATABLE_READ}, + {false, true, 0, true, false, REPEATABLE_READ}, + {false, true, 0, false, false, REPEATABLE_READ}, + {false, false, 0, true, false, REPEATABLE_READ}, + {false, false, 0, false, false, REPEATABLE_READ}, + + // backups = 1 + {true, true, 1, true, false, REPEATABLE_READ}, + {true, true, 1, false, false, REPEATABLE_READ}, + {true, false, 1, true, false, REPEATABLE_READ}, + {true, false, 1, false, false, REPEATABLE_READ}, + {false, true, 1, true, false, REPEATABLE_READ}, + {false, true, 1, false, false, REPEATABLE_READ}, + {false, false, 1, true, false, REPEATABLE_READ}, + {false, false, 1, false, false, REPEATABLE_READ}, + + // backups = 2 + {true, true, 2, true, false, REPEATABLE_READ}, + {true, true, 2, false, false, REPEATABLE_READ}, + {true, false, 2, true, false, REPEATABLE_READ}, + {true, false, 2, false, false, REPEATABLE_READ}, + {false, true, 2, true, false, REPEATABLE_READ}, + {false, true, 2, false, false, REPEATABLE_READ}, + {false, false, 2, true, false, REPEATABLE_READ}, + {false, false, 2, false, false, REPEATABLE_READ}, + + // replicated cache. + {true, true, 0, true, true, REPEATABLE_READ}, + {true, false, 0, true, true, REPEATABLE_READ}, + {false, true, 0, true, true, REPEATABLE_READ}, + {false, false, 0, true, true, REPEATABLE_READ}, + }); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + ignite0 = startGrid(0); + ignite1 = startGrid(1); + ignite2 = startGrid(2); + ignite3 = startGrid(3); + client = startClientGrid(); + + awaitPartitionMapExchange(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite0.destroyCache(DEFAULT_CACHE_NAME); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesRemoteDhtLockAcquireAgain() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int node0Key = primaryKey(cache0); + int node1Key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(node0Key, -1); + cache0.put(node1Key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + cache0.put(node0Key, 1); + + tx.savepoint("sp"); + + cache0.put(node1Key, 1); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + cache0.put(node1Key, 2); + + tx.commit(); + } + }); + + ableToUpdate(node1Key); + + assertFalse(fut.isDone()); + + // TODO: IGNITE-28612 Entry visibility violation in transactional replication cache with one backup and near. + if (initKeies && useNearCache && backups == 1 && spKeyOnTxInitiator && !replicated) + GridTestUtils.waitForCondition(() -> Integer.valueOf(42).equals(cache0.get(node1Key)), 10_000); + + assertEquals(Integer.valueOf(42), cache0.get(node1Key)); + + finishFirstTxLatch.countDown(); + + fut.get(10_000); + + assertEquals(Integer.valueOf(2), cache0.get(node1Key)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testOverwriteAndReleaseSavepoint() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(key, -1); + } + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + cache0.put(key, 1); + + tx.savepoint("sp"); + + cache0.put(key, 2); + + tx.savepoint("sp", true); + + cache0.put(key, 3); + + tx.rollbackToSavepoint("sp"); + + tx.commit(); + } + + assertEquals(Integer.valueOf(2), cache0.get(key)); + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + tx.savepoint("sp"); + + tx.releaseSavepoint("sp"); + + GridTestUtils.assertThrowsAnyCause(log, + () -> { + tx.rollbackToSavepoint("sp"); + + return null; + }, + IllegalArgumentException.class, + "No such savepoint"); + } + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesLockForPutRemoveEntry() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + tx.savepoint("sp"); + + cache0.put(key, 1); + cache0.remove(key); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + tx.commit(); + } + }); + + assertTrue(savepointRolledBackLatch.await(10, TimeUnit.SECONDS)); + + ableToUpdate(key); + + finishFirstTxLatch.countDown(); + + fut.get(10_000); + + assertEquals(Integer.valueOf(42), cache0.get(key)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepoint() { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key1 = keyForPrimaryAndBackup(ignite0, ignite1); + int key2 = keyForPrimaryAndBackup(ignite0, ignite2); + int key3 = keyForPrimaryAndBackup(ignite0, ignite3); + + if (initKeies) { + cache0.put(key1, -1); + cache0.put(key2, -1); + cache0.put(key3, -1); + } + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 3)) { + cache0.put(key1, 1); + + tx.savepoint("sp"); + + cache0.put(key2, 2); + + tx.rollbackToSavepoint("sp"); + + cache0.put(key3, 3); + + tx.commit(); + } + + assertEquals(Integer.valueOf(1), cache0.get(key1)); + + if (initKeies) + assertEquals(Integer.valueOf(-1), cache0.get(key2)); + else + assertNull(cache0.get(key2)); + + assertEquals(Integer.valueOf(3), cache0.get(key3)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testClientInitiator() throws Exception { + // A client node can not be a primary node for any key. + if (spKeyOnTxInitiator) + return; + + IgniteCache<Integer, Integer> cache = transactionalCache(client); + + int key1 = keyForPrimaryAndBackup(client, ignite0); + int key2 = keyForPrimaryAndBackup(client, ignite1); + + if (initKeies) { + cache.put(key1, -1); + cache.put(key2, -1); + } + + GridCacheVersion[] nearVerRef = new GridCacheVersion[1]; + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = client.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + nearVerRef[0] = ((TransactionProxyImpl<?, ?>)tx).tx().nearXidVersion(); + + Integer val1 = cache.get(key1); + Integer val2 = cache.get(key2); + + if (initKeies) { + assertEquals(-1, val1.intValue()); + assertEquals(-1, val2.intValue()); + } + else { + assertNull(val1); + assertNull(val2); + } + + cache.put(key1, 1); + cache.put(key2, 1); + + tx.savepoint("sp"); + + cache.put(key1, 2); + cache.put(key2, 2); + + tx.rollbackToSavepoint("sp"); + + val1 = cache.get(key1); + val2 = cache.get(key2); + + assertEquals(1, val1.intValue()); + assertEquals(1, val2.intValue()); + + tx.commit(); + } + }); + + fut.get(10_000); + + Integer val1 = cache.get(key1); + Integer val2 = cache.get(key2); + + assertEquals(1, val1.intValue()); + assertEquals(1, val2.intValue()); + + assertNoActiveTx(nearVerRef[0]); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackAllKey() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int keyOnInitiator = primaryKey(cache0); + int keyOnOtherNode = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(keyOnInitiator, -1); + cache0.put(keyOnOtherNode, -1); + } + + CountDownLatch rollbackDoneLatch = new CountDownLatch(1); + CountDownLatch finishTxLatch = new CountDownLatch(1); + GridCacheVersion[] nearVerRef = new GridCacheVersion[1]; + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + nearVerRef[0] = ((TransactionProxyImpl<?, ?>)tx).tx().nearXidVersion(); + + tx.savepoint("sp"); + + Integer val1 = cache0.get(keyOnInitiator); + Integer val2 = cache0.get(keyOnOtherNode); + + if (initKeies) { + assertEquals(-1, val1.intValue()); + assertEquals(-1, val2.intValue()); + } + else { + assertNull(val1); + assertNull(val2); + } + + cache0.put(keyOnInitiator, 1); + cache0.put(keyOnOtherNode, 1); + + tx.rollbackToSavepoint("sp"); + + rollbackDoneLatch.countDown(); + + assertTrue(finishTxLatch.await(10, TimeUnit.SECONDS)); + + val1 = cache0.get(keyOnInitiator); + val2 = cache0.get(keyOnOtherNode); + + assertEquals(42, val1.intValue()); + assertEquals(42, val2.intValue()); + + tx.commit(); + } + }); + + assertTrue(rollbackDoneLatch.await(10, TimeUnit.SECONDS)); + + assertNoTxStateKeyOnNode(nearVerRef[0], keyOnInitiator); + assertNoTxStateKeyOnNode(nearVerRef[0], keyOnOtherNode); + + ableToUpdate(keyOnInitiator); + ableToUpdate(keyOnOtherNode); + + finishTxLatch.countDown(); + + fut.get(10_000); + + assertNoActiveTx(nearVerRef[0]); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testDhtEntriesAfterRollbackToSavepoint() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int keepTxAliveKey = primaryKey(cache0); + int dhtKey = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(keepTxAliveKey, -1); + cache0.put(dhtKey, -1); + } + + CountDownLatch dhtKeyWrittenLatch = new CountDownLatch(1); + CountDownLatch proceedRollbackLatch = new CountDownLatch(1); + CountDownLatch rollbackDoneLatch = new CountDownLatch(1); + CountDownLatch finishTxLatch = new CountDownLatch(1); + GridCacheVersion[] nearVerRef = new GridCacheVersion[1]; + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + nearVerRef[0] = ((TransactionProxyImpl<?, ?>)tx).tx().nearXidVersion(); + + cache0.put(keepTxAliveKey, 1); + + tx.savepoint("sp"); + + cache0.put(dhtKey, 1); + + dhtKeyWrittenLatch.countDown(); + + assertTrue(proceedRollbackLatch.await(10, TimeUnit.SECONDS)); + + tx.rollbackToSavepoint("sp"); + + rollbackDoneLatch.countDown(); + + assertTrue(finishTxLatch.await(10, TimeUnit.SECONDS)); + + tx.commit(); + } + }); + + assertTrue(dhtKeyWrittenLatch.await(10, TimeUnit.SECONDS)); + assertNotNull(nearVerRef[0]); + + proceedRollbackLatch.countDown(); + + assertTrue(rollbackDoneLatch.await(10, TimeUnit.SECONDS)); + + assertNoTxStateKeyOnNode(nearVerRef[0], dhtKey); + + finishTxLatch.countDown(); + + fut.get(10_000); + + assertNoActiveTx(nearVerRef[0]); + } + + /** + * Checks that a key is able to be updated. + * + * @param key Key to update. + * @throws Exception If failed. + */ + private void ableToUpdate(int key) { Review Comment: The method seems odd. The description says it checks, but it actually updates, and the title seems to imply the question. Please correct the name and description. ########## modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java: ########## @@ -290,6 +290,57 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport { */ public void suspend() throws IgniteException; + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. The savepoint keeps the current transaction state and can + * later be used by {@link #rollbackToSavepoint(String)} to discard changes made after it was created. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the same name already exists. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name); + + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. If {@code overwrite} is {@code true} and a savepoint with + * the same name exists, the existing savepoint is replaced with a snapshot of the current transaction state. + * + * @param name Savepoint name. + * @param overwrite Whether to overwrite an existing savepoint with the same name. + * @throws TransactionException If savepoint with the same name already exists and {@code overwrite} is + * {@code false}. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name, boolean overwrite); Review Comment: I think it's worth adding `@IgniteExperimental`, the api doesn't seem finished yet. ########## modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/GridNearUnlockRequest.java: ########## @@ -36,6 +36,10 @@ public class GridNearUnlockRequest extends GridDistributedBaseMessage { @Order(0) public List<KeyCacheObject> keys; + /** Savepoint rollback flag. */ + @Order(1) + public boolean forSavepoint; Review Comment: Check `Rolling Upgrade check` suggests there might be a problem. Let's figure out what might be wrong with the feature authors. ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointPessimisticTest.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.junit.Test; + +import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC; +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests transaction savepoint functionality. + * Currently, savepoint API is supported only for pessimistic transactions. + */ +public class TxSavepointPessimisticTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName) + .setCacheConfiguration(new CacheConfiguration<>(DEFAULT_CACHE_NAME) + .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL) + .setBackups(1) + .setCacheMode(CacheMode.PARTITIONED)); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testSavepointRejectedForOptimisticTx() throws Exception { + Ignite ignite = startGrid(0); + + try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) { + GridTestUtils.assertThrowsAnyCause(log, + () -> { + tx.savepoint("sp"); + + return null; + }, + IgniteCheckedException.class, + "Savepoints are supported only for PESSIMISTIC transactions."); + } + } + + /** + * @throws Exception If failed. + */ + @Test + public void testSeveralSavepoints() throws Exception { + Ignite ignite = startGrid(0); + IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME); + + int key = 1; + + try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) { + cache.put(key, 1); + + tx.savepoint("sp1"); + + cache.put(key, 2); + + tx.savepoint("sp2"); + + cache.put(key, 3); + + tx.savepoint("sp3"); + + cache.put(key, 4); + + tx.rollbackToSavepoint("sp1"); + + tx.commit(); + } + + assertEquals(Integer.valueOf(1), cache.get(key)); Review Comment: ```suggestion assertEquals(1, cache.get(key)); ``` ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointParameterizedTest.java: ########## @@ -0,0 +1,711 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.apache.ignite.transactions.TransactionIsolation; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests for transaction savepoint API. + */ +@RunWith(Parameterized.class) +public class TxSavepointParameterizedTest extends GridCommonAbstractTest { + /** */ + private static Ignite ignite0; + + /** */ + private static Ignite ignite1; + + /** */ + private static Ignite ignite2; + + /** */ + private static Ignite ignite3; + + /** */ + private static Ignite client; + + /** */ + @Parameter(0) + public boolean initKeies; + + /** */ + @Parameter(1) + public boolean useNearCache; + + /** */ + @Parameter(2) + public int backups; + + /** */ + @Parameter(3) + public boolean spKeyOnTxInitiator; + + /** */ + @Parameter(4) + public boolean replicated; + + /** */ + @Parameter(5) + public TransactionIsolation transactionIsolation; + + /** + * Returns data for test. + * @return Test parameters. + */ + @Parameters(name = "initKeies={0}, useNearCache={1}, backups={2}, spKeyOnTxInitiator={3}, replicated={4}, txIsolation={5}") + public static Collection<Object[]> testData() { + return List.of(new Object[][] { + // READ_COMMITTED + // backups = 0 + {true, true, 0, true, false, READ_COMMITTED}, + {true, true, 0, false, false, READ_COMMITTED}, + {true, false, 0, true, false, READ_COMMITTED}, + {true, false, 0, false, false, READ_COMMITTED}, + {false, true, 0, true, false, READ_COMMITTED}, + {false, true, 0, false, false, READ_COMMITTED}, + {false, false, 0, true, false, READ_COMMITTED}, + {false, false, 0, false, false, READ_COMMITTED}, + + // backups = 1 + {true, true, 1, true, false, READ_COMMITTED}, + {true, true, 1, false, false, READ_COMMITTED}, + {true, false, 1, true, false, READ_COMMITTED}, + {true, false, 1, false, false, READ_COMMITTED}, + {false, true, 1, true, false, READ_COMMITTED}, + {false, true, 1, false, false, READ_COMMITTED}, + {false, false, 1, true, false, READ_COMMITTED}, + {false, false, 1, false, false, READ_COMMITTED}, + + // backups = 2 + {true, true, 2, true, false, READ_COMMITTED}, + {true, true, 2, false, false, READ_COMMITTED}, + {true, false, 2, true, false, READ_COMMITTED}, + {true, false, 2, false, false, READ_COMMITTED}, + {false, true, 2, true, false, READ_COMMITTED}, + {false, true, 2, false, false, READ_COMMITTED}, + {false, false, 2, true, false, READ_COMMITTED}, + {false, false, 2, false, false, READ_COMMITTED}, + + // replicated cache. + {true, true, 0, true, true, READ_COMMITTED}, + {true, false, 0, true, true, READ_COMMITTED}, + {false, true, 0, true, true, READ_COMMITTED}, + {false, false, 0, true, true, READ_COMMITTED}, + + // REPEATABLE_READ + // backups = 0 + {true, true, 0, true, false, REPEATABLE_READ}, + {true, true, 0, false, false, REPEATABLE_READ}, + {true, false, 0, true, false, REPEATABLE_READ}, + {true, false, 0, false, false, REPEATABLE_READ}, + {false, true, 0, true, false, REPEATABLE_READ}, + {false, true, 0, false, false, REPEATABLE_READ}, + {false, false, 0, true, false, REPEATABLE_READ}, + {false, false, 0, false, false, REPEATABLE_READ}, + + // backups = 1 + {true, true, 1, true, false, REPEATABLE_READ}, + {true, true, 1, false, false, REPEATABLE_READ}, + {true, false, 1, true, false, REPEATABLE_READ}, + {true, false, 1, false, false, REPEATABLE_READ}, + {false, true, 1, true, false, REPEATABLE_READ}, + {false, true, 1, false, false, REPEATABLE_READ}, + {false, false, 1, true, false, REPEATABLE_READ}, + {false, false, 1, false, false, REPEATABLE_READ}, + + // backups = 2 + {true, true, 2, true, false, REPEATABLE_READ}, + {true, true, 2, false, false, REPEATABLE_READ}, + {true, false, 2, true, false, REPEATABLE_READ}, + {true, false, 2, false, false, REPEATABLE_READ}, + {false, true, 2, true, false, REPEATABLE_READ}, + {false, true, 2, false, false, REPEATABLE_READ}, + {false, false, 2, true, false, REPEATABLE_READ}, + {false, false, 2, false, false, REPEATABLE_READ}, + + // replicated cache. + {true, true, 0, true, true, REPEATABLE_READ}, + {true, false, 0, true, true, REPEATABLE_READ}, + {false, true, 0, true, true, REPEATABLE_READ}, + {false, false, 0, true, true, REPEATABLE_READ}, + }); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + ignite0 = startGrid(0); + ignite1 = startGrid(1); + ignite2 = startGrid(2); + ignite3 = startGrid(3); + client = startClientGrid(); + + awaitPartitionMapExchange(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite0.destroyCache(DEFAULT_CACHE_NAME); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesRemoteDhtLockAcquireAgain() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int node0Key = primaryKey(cache0); + int node1Key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(node0Key, -1); + cache0.put(node1Key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + cache0.put(node0Key, 1); + + tx.savepoint("sp"); + + cache0.put(node1Key, 1); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + cache0.put(node1Key, 2); + + tx.commit(); + } + }); + + ableToUpdate(node1Key); + + assertFalse(fut.isDone()); + + // TODO: IGNITE-28612 Entry visibility violation in transactional replication cache with one backup and near. + if (initKeies && useNearCache && backups == 1 && spKeyOnTxInitiator && !replicated) + GridTestUtils.waitForCondition(() -> Integer.valueOf(42).equals(cache0.get(node1Key)), 10_000); + + assertEquals(Integer.valueOf(42), cache0.get(node1Key)); + + finishFirstTxLatch.countDown(); + + fut.get(10_000); + + assertEquals(Integer.valueOf(2), cache0.get(node1Key)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testOverwriteAndReleaseSavepoint() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(key, -1); + } + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + cache0.put(key, 1); + + tx.savepoint("sp"); + + cache0.put(key, 2); + + tx.savepoint("sp", true); + + cache0.put(key, 3); + + tx.rollbackToSavepoint("sp"); + + tx.commit(); + } + + assertEquals(Integer.valueOf(2), cache0.get(key)); + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + tx.savepoint("sp"); + + tx.releaseSavepoint("sp"); + + GridTestUtils.assertThrowsAnyCause(log, + () -> { + tx.rollbackToSavepoint("sp"); + + return null; + }, + IllegalArgumentException.class, + "No such savepoint"); + } + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesLockForPutRemoveEntry() throws Exception { Review Comment: Can it be combined with `testRollbackToSavepointReleasesRemoteDhtLockAcquireAgain`? ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointParameterizedTest.java: ########## @@ -0,0 +1,711 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.apache.ignite.transactions.TransactionIsolation; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests for transaction savepoint API. + */ +@RunWith(Parameterized.class) +public class TxSavepointParameterizedTest extends GridCommonAbstractTest { + /** */ + private static Ignite ignite0; + + /** */ + private static Ignite ignite1; + + /** */ + private static Ignite ignite2; + + /** */ + private static Ignite ignite3; + + /** */ + private static Ignite client; + + /** */ + @Parameter(0) + public boolean initKeies; + + /** */ + @Parameter(1) + public boolean useNearCache; + + /** */ + @Parameter(2) + public int backups; + + /** */ + @Parameter(3) + public boolean spKeyOnTxInitiator; + + /** */ + @Parameter(4) + public boolean replicated; + + /** */ + @Parameter(5) + public TransactionIsolation transactionIsolation; + + /** + * Returns data for test. + * @return Test parameters. + */ + @Parameters(name = "initKeies={0}, useNearCache={1}, backups={2}, spKeyOnTxInitiator={3}, replicated={4}, txIsolation={5}") + public static Collection<Object[]> testData() { + return List.of(new Object[][] { + // READ_COMMITTED + // backups = 0 + {true, true, 0, true, false, READ_COMMITTED}, + {true, true, 0, false, false, READ_COMMITTED}, + {true, false, 0, true, false, READ_COMMITTED}, + {true, false, 0, false, false, READ_COMMITTED}, + {false, true, 0, true, false, READ_COMMITTED}, + {false, true, 0, false, false, READ_COMMITTED}, + {false, false, 0, true, false, READ_COMMITTED}, + {false, false, 0, false, false, READ_COMMITTED}, + + // backups = 1 + {true, true, 1, true, false, READ_COMMITTED}, + {true, true, 1, false, false, READ_COMMITTED}, + {true, false, 1, true, false, READ_COMMITTED}, + {true, false, 1, false, false, READ_COMMITTED}, + {false, true, 1, true, false, READ_COMMITTED}, + {false, true, 1, false, false, READ_COMMITTED}, + {false, false, 1, true, false, READ_COMMITTED}, + {false, false, 1, false, false, READ_COMMITTED}, + + // backups = 2 + {true, true, 2, true, false, READ_COMMITTED}, + {true, true, 2, false, false, READ_COMMITTED}, + {true, false, 2, true, false, READ_COMMITTED}, + {true, false, 2, false, false, READ_COMMITTED}, + {false, true, 2, true, false, READ_COMMITTED}, + {false, true, 2, false, false, READ_COMMITTED}, + {false, false, 2, true, false, READ_COMMITTED}, + {false, false, 2, false, false, READ_COMMITTED}, + + // replicated cache. + {true, true, 0, true, true, READ_COMMITTED}, + {true, false, 0, true, true, READ_COMMITTED}, + {false, true, 0, true, true, READ_COMMITTED}, + {false, false, 0, true, true, READ_COMMITTED}, + + // REPEATABLE_READ + // backups = 0 + {true, true, 0, true, false, REPEATABLE_READ}, + {true, true, 0, false, false, REPEATABLE_READ}, + {true, false, 0, true, false, REPEATABLE_READ}, + {true, false, 0, false, false, REPEATABLE_READ}, + {false, true, 0, true, false, REPEATABLE_READ}, + {false, true, 0, false, false, REPEATABLE_READ}, + {false, false, 0, true, false, REPEATABLE_READ}, + {false, false, 0, false, false, REPEATABLE_READ}, + + // backups = 1 + {true, true, 1, true, false, REPEATABLE_READ}, + {true, true, 1, false, false, REPEATABLE_READ}, + {true, false, 1, true, false, REPEATABLE_READ}, + {true, false, 1, false, false, REPEATABLE_READ}, + {false, true, 1, true, false, REPEATABLE_READ}, + {false, true, 1, false, false, REPEATABLE_READ}, + {false, false, 1, true, false, REPEATABLE_READ}, + {false, false, 1, false, false, REPEATABLE_READ}, + + // backups = 2 + {true, true, 2, true, false, REPEATABLE_READ}, + {true, true, 2, false, false, REPEATABLE_READ}, + {true, false, 2, true, false, REPEATABLE_READ}, + {true, false, 2, false, false, REPEATABLE_READ}, + {false, true, 2, true, false, REPEATABLE_READ}, + {false, true, 2, false, false, REPEATABLE_READ}, + {false, false, 2, true, false, REPEATABLE_READ}, + {false, false, 2, false, false, REPEATABLE_READ}, + + // replicated cache. + {true, true, 0, true, true, REPEATABLE_READ}, + {true, false, 0, true, true, REPEATABLE_READ}, + {false, true, 0, true, true, REPEATABLE_READ}, + {false, false, 0, true, true, REPEATABLE_READ}, + }); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + ignite0 = startGrid(0); + ignite1 = startGrid(1); + ignite2 = startGrid(2); + ignite3 = startGrid(3); + client = startClientGrid(); + + awaitPartitionMapExchange(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite0.destroyCache(DEFAULT_CACHE_NAME); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesRemoteDhtLockAcquireAgain() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int node0Key = primaryKey(cache0); + int node1Key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(node0Key, -1); + cache0.put(node1Key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + cache0.put(node0Key, 1); + + tx.savepoint("sp"); + + cache0.put(node1Key, 1); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + cache0.put(node1Key, 2); + + tx.commit(); + } + }); + + ableToUpdate(node1Key); + + assertFalse(fut.isDone()); + + // TODO: IGNITE-28612 Entry visibility violation in transactional replication cache with one backup and near. + if (initKeies && useNearCache && backups == 1 && spKeyOnTxInitiator && !replicated) + GridTestUtils.waitForCondition(() -> Integer.valueOf(42).equals(cache0.get(node1Key)), 10_000); Review Comment: Why without assertTrue/false(waitForCondition) ? ########## docs/_docs/code-snippets/java/src/main/java/org/apache/ignite/snippets/PerformingTransactions.java: ########## @@ -98,6 +99,38 @@ public static void executingTransactionsExample() { } } + public static void transactionSavepointsExample() { + try (Ignite ignite = Ignition.start()) { + // tag::savepoints[] + CacheConfiguration<String, Integer> cfg = new CacheConfiguration<>(); + cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); + cfg.setName("myCache"); + + IgniteCache<String, Integer> cache = ignite.getOrCreateCache(cfg); + + try (Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC, Review Comment: ```suggestion CacheConfiguration<String, Integer> cfg = new CacheConfiguration<>(); cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); cfg.setName("myCache"); IgniteCache<String, Integer> cache = ignite.getOrCreateCache(cfg); // tag::savepoints[] try (Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC, ``` ########## docs/_docs/key-value-api/transactions.adoc: ########## @@ -90,6 +90,34 @@ include::code-snippets/cpp/src/transactions.cpp[tag=transactions-execution,inden It is critical that an Ignite Transaction should be `closed` regardless of its commit state or ocurred exceptions. Using of `try-with-resources` is recommended approach, but you can also explicitly call `rollback` when catching exceptions. Calling of `close`, `commit` or `rollback` ensures that all resources are released and the transaction is no longer bound to the current thread. ==== +== Transaction Savepoints + +Savepoints allow you to mark an intermediate state inside an explicit transaction and later roll back only the changes made after that point. +They are useful when a transaction contains several logical steps and one of the later steps can be discarded without rolling back the whole transaction. + +Ignite supports savepoints only for explicit `PESSIMISTIC` transactions. +Savepoints are local to the transaction that created them and are removed when the transaction is committed or rolled back. + +Use `Transaction.savepoint(name)` to create a savepoint. +If a savepoint with the same name already exists, Ignite throws a `TransactionException`. +Use `Transaction.savepoint(name, true)` to replace an existing savepoint. + +Use `Transaction.rollbackToSavepoint(name)` to roll back transaction changes made after the savepoint was created. +Savepoints created after the target savepoint are released as part of this operation. Review Comment: maybe not released, but rolled back out? ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointParameterizedTest.java: ########## @@ -0,0 +1,711 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.apache.ignite.transactions.TransactionIsolation; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests for transaction savepoint API. + */ +@RunWith(Parameterized.class) +public class TxSavepointParameterizedTest extends GridCommonAbstractTest { + /** */ + private static Ignite ignite0; + + /** */ + private static Ignite ignite1; + + /** */ + private static Ignite ignite2; + + /** */ + private static Ignite ignite3; + + /** */ + private static Ignite client; + + /** */ + @Parameter(0) + public boolean initKeies; + + /** */ + @Parameter(1) + public boolean useNearCache; + + /** */ + @Parameter(2) + public int backups; + + /** */ + @Parameter(3) + public boolean spKeyOnTxInitiator; + + /** */ + @Parameter(4) + public boolean replicated; + + /** */ + @Parameter(5) + public TransactionIsolation transactionIsolation; + + /** + * Returns data for test. + * @return Test parameters. + */ + @Parameters(name = "initKeies={0}, useNearCache={1}, backups={2}, spKeyOnTxInitiator={3}, replicated={4}, txIsolation={5}") + public static Collection<Object[]> testData() { + return List.of(new Object[][] { + // READ_COMMITTED + // backups = 0 + {true, true, 0, true, false, READ_COMMITTED}, + {true, true, 0, false, false, READ_COMMITTED}, + {true, false, 0, true, false, READ_COMMITTED}, + {true, false, 0, false, false, READ_COMMITTED}, + {false, true, 0, true, false, READ_COMMITTED}, + {false, true, 0, false, false, READ_COMMITTED}, + {false, false, 0, true, false, READ_COMMITTED}, + {false, false, 0, false, false, READ_COMMITTED}, + + // backups = 1 + {true, true, 1, true, false, READ_COMMITTED}, + {true, true, 1, false, false, READ_COMMITTED}, + {true, false, 1, true, false, READ_COMMITTED}, + {true, false, 1, false, false, READ_COMMITTED}, + {false, true, 1, true, false, READ_COMMITTED}, + {false, true, 1, false, false, READ_COMMITTED}, + {false, false, 1, true, false, READ_COMMITTED}, + {false, false, 1, false, false, READ_COMMITTED}, + + // backups = 2 + {true, true, 2, true, false, READ_COMMITTED}, + {true, true, 2, false, false, READ_COMMITTED}, + {true, false, 2, true, false, READ_COMMITTED}, + {true, false, 2, false, false, READ_COMMITTED}, + {false, true, 2, true, false, READ_COMMITTED}, + {false, true, 2, false, false, READ_COMMITTED}, + {false, false, 2, true, false, READ_COMMITTED}, + {false, false, 2, false, false, READ_COMMITTED}, + + // replicated cache. + {true, true, 0, true, true, READ_COMMITTED}, + {true, false, 0, true, true, READ_COMMITTED}, + {false, true, 0, true, true, READ_COMMITTED}, + {false, false, 0, true, true, READ_COMMITTED}, + + // REPEATABLE_READ + // backups = 0 + {true, true, 0, true, false, REPEATABLE_READ}, + {true, true, 0, false, false, REPEATABLE_READ}, + {true, false, 0, true, false, REPEATABLE_READ}, + {true, false, 0, false, false, REPEATABLE_READ}, + {false, true, 0, true, false, REPEATABLE_READ}, + {false, true, 0, false, false, REPEATABLE_READ}, + {false, false, 0, true, false, REPEATABLE_READ}, + {false, false, 0, false, false, REPEATABLE_READ}, + + // backups = 1 + {true, true, 1, true, false, REPEATABLE_READ}, + {true, true, 1, false, false, REPEATABLE_READ}, + {true, false, 1, true, false, REPEATABLE_READ}, + {true, false, 1, false, false, REPEATABLE_READ}, + {false, true, 1, true, false, REPEATABLE_READ}, + {false, true, 1, false, false, REPEATABLE_READ}, + {false, false, 1, true, false, REPEATABLE_READ}, + {false, false, 1, false, false, REPEATABLE_READ}, + + // backups = 2 + {true, true, 2, true, false, REPEATABLE_READ}, + {true, true, 2, false, false, REPEATABLE_READ}, + {true, false, 2, true, false, REPEATABLE_READ}, + {true, false, 2, false, false, REPEATABLE_READ}, + {false, true, 2, true, false, REPEATABLE_READ}, + {false, true, 2, false, false, REPEATABLE_READ}, + {false, false, 2, true, false, REPEATABLE_READ}, + {false, false, 2, false, false, REPEATABLE_READ}, + + // replicated cache. + {true, true, 0, true, true, REPEATABLE_READ}, + {true, false, 0, true, true, REPEATABLE_READ}, + {false, true, 0, true, true, REPEATABLE_READ}, + {false, false, 0, true, true, REPEATABLE_READ}, + }); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + ignite0 = startGrid(0); + ignite1 = startGrid(1); + ignite2 = startGrid(2); + ignite3 = startGrid(3); + client = startClientGrid(); + + awaitPartitionMapExchange(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite0.destroyCache(DEFAULT_CACHE_NAME); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesRemoteDhtLockAcquireAgain() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int node0Key = primaryKey(cache0); + int node1Key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(node0Key, -1); + cache0.put(node1Key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + cache0.put(node0Key, 1); + + tx.savepoint("sp"); + + cache0.put(node1Key, 1); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + cache0.put(node1Key, 2); + + tx.commit(); + } + }); + + ableToUpdate(node1Key); + + assertFalse(fut.isDone()); + + // TODO: IGNITE-28612 Entry visibility violation in transactional replication cache with one backup and near. + if (initKeies && useNearCache && backups == 1 && spKeyOnTxInitiator && !replicated) + GridTestUtils.waitForCondition(() -> Integer.valueOf(42).equals(cache0.get(node1Key)), 10_000); + + assertEquals(Integer.valueOf(42), cache0.get(node1Key)); + + finishFirstTxLatch.countDown(); + + fut.get(10_000); + + assertEquals(Integer.valueOf(2), cache0.get(node1Key)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testOverwriteAndReleaseSavepoint() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(key, -1); + } + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + cache0.put(key, 1); + + tx.savepoint("sp"); + + cache0.put(key, 2); + + tx.savepoint("sp", true); + + cache0.put(key, 3); + + tx.rollbackToSavepoint("sp"); + + tx.commit(); + } + + assertEquals(Integer.valueOf(2), cache0.get(key)); + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { Review Comment: I suggest putting it into a separate test method. ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointParameterizedTest.java: ########## @@ -0,0 +1,711 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.apache.ignite.transactions.TransactionIsolation; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests for transaction savepoint API. + */ +@RunWith(Parameterized.class) +public class TxSavepointParameterizedTest extends GridCommonAbstractTest { + /** */ + private static Ignite ignite0; + + /** */ + private static Ignite ignite1; + + /** */ + private static Ignite ignite2; + + /** */ + private static Ignite ignite3; + + /** */ + private static Ignite client; + + /** */ + @Parameter(0) + public boolean initKeies; + + /** */ + @Parameter(1) + public boolean useNearCache; + + /** */ + @Parameter(2) + public int backups; + + /** */ + @Parameter(3) + public boolean spKeyOnTxInitiator; + + /** */ + @Parameter(4) + public boolean replicated; + + /** */ + @Parameter(5) + public TransactionIsolation transactionIsolation; + + /** + * Returns data for test. + * @return Test parameters. + */ + @Parameters(name = "initKeies={0}, useNearCache={1}, backups={2}, spKeyOnTxInitiator={3}, replicated={4}, txIsolation={5}") + public static Collection<Object[]> testData() { + return List.of(new Object[][] { + // READ_COMMITTED + // backups = 0 + {true, true, 0, true, false, READ_COMMITTED}, + {true, true, 0, false, false, READ_COMMITTED}, + {true, false, 0, true, false, READ_COMMITTED}, + {true, false, 0, false, false, READ_COMMITTED}, + {false, true, 0, true, false, READ_COMMITTED}, + {false, true, 0, false, false, READ_COMMITTED}, + {false, false, 0, true, false, READ_COMMITTED}, + {false, false, 0, false, false, READ_COMMITTED}, + + // backups = 1 + {true, true, 1, true, false, READ_COMMITTED}, + {true, true, 1, false, false, READ_COMMITTED}, + {true, false, 1, true, false, READ_COMMITTED}, + {true, false, 1, false, false, READ_COMMITTED}, + {false, true, 1, true, false, READ_COMMITTED}, + {false, true, 1, false, false, READ_COMMITTED}, + {false, false, 1, true, false, READ_COMMITTED}, + {false, false, 1, false, false, READ_COMMITTED}, + + // backups = 2 + {true, true, 2, true, false, READ_COMMITTED}, + {true, true, 2, false, false, READ_COMMITTED}, + {true, false, 2, true, false, READ_COMMITTED}, + {true, false, 2, false, false, READ_COMMITTED}, + {false, true, 2, true, false, READ_COMMITTED}, + {false, true, 2, false, false, READ_COMMITTED}, + {false, false, 2, true, false, READ_COMMITTED}, + {false, false, 2, false, false, READ_COMMITTED}, + + // replicated cache. + {true, true, 0, true, true, READ_COMMITTED}, + {true, false, 0, true, true, READ_COMMITTED}, + {false, true, 0, true, true, READ_COMMITTED}, + {false, false, 0, true, true, READ_COMMITTED}, + + // REPEATABLE_READ + // backups = 0 + {true, true, 0, true, false, REPEATABLE_READ}, + {true, true, 0, false, false, REPEATABLE_READ}, + {true, false, 0, true, false, REPEATABLE_READ}, + {true, false, 0, false, false, REPEATABLE_READ}, + {false, true, 0, true, false, REPEATABLE_READ}, + {false, true, 0, false, false, REPEATABLE_READ}, + {false, false, 0, true, false, REPEATABLE_READ}, + {false, false, 0, false, false, REPEATABLE_READ}, + + // backups = 1 + {true, true, 1, true, false, REPEATABLE_READ}, + {true, true, 1, false, false, REPEATABLE_READ}, + {true, false, 1, true, false, REPEATABLE_READ}, + {true, false, 1, false, false, REPEATABLE_READ}, + {false, true, 1, true, false, REPEATABLE_READ}, + {false, true, 1, false, false, REPEATABLE_READ}, + {false, false, 1, true, false, REPEATABLE_READ}, + {false, false, 1, false, false, REPEATABLE_READ}, + + // backups = 2 + {true, true, 2, true, false, REPEATABLE_READ}, + {true, true, 2, false, false, REPEATABLE_READ}, + {true, false, 2, true, false, REPEATABLE_READ}, + {true, false, 2, false, false, REPEATABLE_READ}, + {false, true, 2, true, false, REPEATABLE_READ}, + {false, true, 2, false, false, REPEATABLE_READ}, + {false, false, 2, true, false, REPEATABLE_READ}, + {false, false, 2, false, false, REPEATABLE_READ}, + + // replicated cache. + {true, true, 0, true, true, REPEATABLE_READ}, + {true, false, 0, true, true, REPEATABLE_READ}, + {false, true, 0, true, true, REPEATABLE_READ}, + {false, false, 0, true, true, REPEATABLE_READ}, + }); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + ignite0 = startGrid(0); + ignite1 = startGrid(1); + ignite2 = startGrid(2); + ignite3 = startGrid(3); + client = startClientGrid(); + + awaitPartitionMapExchange(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite0.destroyCache(DEFAULT_CACHE_NAME); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesRemoteDhtLockAcquireAgain() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int node0Key = primaryKey(cache0); + int node1Key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(node0Key, -1); + cache0.put(node1Key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + cache0.put(node0Key, 1); + + tx.savepoint("sp"); + + cache0.put(node1Key, 1); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + cache0.put(node1Key, 2); + + tx.commit(); + } + }); + + ableToUpdate(node1Key); + + assertFalse(fut.isDone()); + + // TODO: IGNITE-28612 Entry visibility violation in transactional replication cache with one backup and near. + if (initKeies && useNearCache && backups == 1 && spKeyOnTxInitiator && !replicated) + GridTestUtils.waitForCondition(() -> Integer.valueOf(42).equals(cache0.get(node1Key)), 10_000); + + assertEquals(Integer.valueOf(42), cache0.get(node1Key)); + + finishFirstTxLatch.countDown(); + + fut.get(10_000); + + assertEquals(Integer.valueOf(2), cache0.get(node1Key)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testOverwriteAndReleaseSavepoint() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(key, -1); + } + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + cache0.put(key, 1); + + tx.savepoint("sp"); + + cache0.put(key, 2); + + tx.savepoint("sp", true); + + cache0.put(key, 3); + + tx.rollbackToSavepoint("sp"); + + tx.commit(); + } + + assertEquals(Integer.valueOf(2), cache0.get(key)); + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + tx.savepoint("sp"); + + tx.releaseSavepoint("sp"); + + GridTestUtils.assertThrowsAnyCause(log, + () -> { + tx.rollbackToSavepoint("sp"); + + return null; + }, + IllegalArgumentException.class, + "No such savepoint"); + } + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepointReleasesLockForPutRemoveEntry() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(key, -1); + } + + CountDownLatch savepointRolledBackLatch = new CountDownLatch(1); + CountDownLatch finishFirstTxLatch = new CountDownLatch(1); + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 1)) { + tx.savepoint("sp"); + + cache0.put(key, 1); + cache0.remove(key); + + tx.rollbackToSavepoint("sp"); + + savepointRolledBackLatch.countDown(); + + assertTrue(finishFirstTxLatch.await(10, TimeUnit.SECONDS)); + + tx.commit(); + } + }); + + assertTrue(savepointRolledBackLatch.await(10, TimeUnit.SECONDS)); + + ableToUpdate(key); + + finishFirstTxLatch.countDown(); + + fut.get(10_000); + + assertEquals(Integer.valueOf(42), cache0.get(key)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackToSavepoint() { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int key1 = keyForPrimaryAndBackup(ignite0, ignite1); + int key2 = keyForPrimaryAndBackup(ignite0, ignite2); + int key3 = keyForPrimaryAndBackup(ignite0, ignite3); + + if (initKeies) { + cache0.put(key1, -1); + cache0.put(key2, -1); + cache0.put(key3, -1); + } + + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 3)) { + cache0.put(key1, 1); + + tx.savepoint("sp"); + + cache0.put(key2, 2); + + tx.rollbackToSavepoint("sp"); + + cache0.put(key3, 3); + + tx.commit(); + } + + assertEquals(Integer.valueOf(1), cache0.get(key1)); + + if (initKeies) + assertEquals(Integer.valueOf(-1), cache0.get(key2)); + else + assertNull(cache0.get(key2)); + + assertEquals(Integer.valueOf(3), cache0.get(key3)); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testClientInitiator() throws Exception { + // A client node can not be a primary node for any key. + if (spKeyOnTxInitiator) + return; + + IgniteCache<Integer, Integer> cache = transactionalCache(client); + + int key1 = keyForPrimaryAndBackup(client, ignite0); + int key2 = keyForPrimaryAndBackup(client, ignite1); + + if (initKeies) { + cache.put(key1, -1); + cache.put(key2, -1); + } + + GridCacheVersion[] nearVerRef = new GridCacheVersion[1]; + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = client.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + nearVerRef[0] = ((TransactionProxyImpl<?, ?>)tx).tx().nearXidVersion(); + + Integer val1 = cache.get(key1); + Integer val2 = cache.get(key2); + + if (initKeies) { + assertEquals(-1, val1.intValue()); + assertEquals(-1, val2.intValue()); + } + else { + assertNull(val1); + assertNull(val2); + } + + cache.put(key1, 1); + cache.put(key2, 1); + + tx.savepoint("sp"); + + cache.put(key1, 2); + cache.put(key2, 2); + + tx.rollbackToSavepoint("sp"); + + val1 = cache.get(key1); + val2 = cache.get(key2); + + assertEquals(1, val1.intValue()); + assertEquals(1, val2.intValue()); + + tx.commit(); + } + }); + + fut.get(10_000); + + Integer val1 = cache.get(key1); + Integer val2 = cache.get(key2); + + assertEquals(1, val1.intValue()); + assertEquals(1, val2.intValue()); + + assertNoActiveTx(nearVerRef[0]); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testRollbackAllKey() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int keyOnInitiator = primaryKey(cache0); + int keyOnOtherNode = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(keyOnInitiator, -1); + cache0.put(keyOnOtherNode, -1); + } + + CountDownLatch rollbackDoneLatch = new CountDownLatch(1); + CountDownLatch finishTxLatch = new CountDownLatch(1); + GridCacheVersion[] nearVerRef = new GridCacheVersion[1]; + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + nearVerRef[0] = ((TransactionProxyImpl<?, ?>)tx).tx().nearXidVersion(); + + tx.savepoint("sp"); + + Integer val1 = cache0.get(keyOnInitiator); + Integer val2 = cache0.get(keyOnOtherNode); + + if (initKeies) { + assertEquals(-1, val1.intValue()); + assertEquals(-1, val2.intValue()); + } + else { + assertNull(val1); + assertNull(val2); + } + + cache0.put(keyOnInitiator, 1); + cache0.put(keyOnOtherNode, 1); + + tx.rollbackToSavepoint("sp"); + + rollbackDoneLatch.countDown(); + + assertTrue(finishTxLatch.await(10, TimeUnit.SECONDS)); + + val1 = cache0.get(keyOnInitiator); + val2 = cache0.get(keyOnOtherNode); + + assertEquals(42, val1.intValue()); + assertEquals(42, val2.intValue()); + + tx.commit(); + } + }); + + assertTrue(rollbackDoneLatch.await(10, TimeUnit.SECONDS)); + + assertNoTxStateKeyOnNode(nearVerRef[0], keyOnInitiator); + assertNoTxStateKeyOnNode(nearVerRef[0], keyOnOtherNode); + + ableToUpdate(keyOnInitiator); + ableToUpdate(keyOnOtherNode); + + finishTxLatch.countDown(); + + fut.get(10_000); + + assertNoActiveTx(nearVerRef[0]); + } + + /** + * @throws Exception If failed. + */ + @Test + public void testDhtEntriesAfterRollbackToSavepoint() throws Exception { + IgniteCache<Integer, Integer> cache0 = transactionalCache(ignite0); + + int keepTxAliveKey = primaryKey(cache0); + int dhtKey = keyForPrimaryAndBackup(ignite0, ignite1); + + if (initKeies) { + cache0.put(keepTxAliveKey, -1); + cache0.put(dhtKey, -1); + } + + CountDownLatch dhtKeyWrittenLatch = new CountDownLatch(1); + CountDownLatch proceedRollbackLatch = new CountDownLatch(1); + CountDownLatch rollbackDoneLatch = new CountDownLatch(1); + CountDownLatch finishTxLatch = new CountDownLatch(1); + GridCacheVersion[] nearVerRef = new GridCacheVersion[1]; + + IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> { + try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, transactionIsolation, 30_000, 2)) { + nearVerRef[0] = ((TransactionProxyImpl<?, ?>)tx).tx().nearXidVersion(); + + cache0.put(keepTxAliveKey, 1); + + tx.savepoint("sp"); + + cache0.put(dhtKey, 1); + + dhtKeyWrittenLatch.countDown(); + + assertTrue(proceedRollbackLatch.await(10, TimeUnit.SECONDS)); + + tx.rollbackToSavepoint("sp"); + + rollbackDoneLatch.countDown(); + + assertTrue(finishTxLatch.await(10, TimeUnit.SECONDS)); + + tx.commit(); + } + }); + + assertTrue(dhtKeyWrittenLatch.await(10, TimeUnit.SECONDS)); + assertNotNull(nearVerRef[0]); + + proceedRollbackLatch.countDown(); + + assertTrue(rollbackDoneLatch.await(10, TimeUnit.SECONDS)); + + assertNoTxStateKeyOnNode(nearVerRef[0], dhtKey); + + finishTxLatch.countDown(); + + fut.get(10_000); + + assertNoActiveTx(nearVerRef[0]); + } + + /** + * Checks that a key is able to be updated. + * + * @param key Key to update. + * @throws Exception If failed. + */ + private void ableToUpdate(int key) { + Ignite updateNode = primaryNode(key, DEFAULT_CACHE_NAME); + + updateNode.cache(DEFAULT_CACHE_NAME).put(key, 42); + } + + /** + * Asserts that no active transaction with the given near version contains the specified key + * in its write-set or entry map on any node. + * + * @param nearVer Near transaction version. + * @param key Key that must be absent from tx state. + * @throws Exception If waiting fails. + */ + private void assertNoTxStateKeyOnNode(GridCacheVersion nearVer, int key) throws Exception { + for (Ignite ignite : G.allGrids()) { + if (ignite.configuration().isClientMode()) + continue; + + assertTrue(GridTestUtils.waitForCondition(() -> { + GridCacheContext<?, ?> cctx = ((IgniteKernal)ignite).internalCache(DEFAULT_CACHE_NAME).context(); + IgniteTxKey txKey = cctx.txKey(cctx.toCacheKeyObject(key)); + + for (IgniteInternalTx tx : cctx.tm().activeTransactions()) { + if (!nearVer.equals(tx.nearXidVersion())) + continue; + + if (tx.hasWriteKey(txKey) || tx.entry(txKey) != null) + return false; + } + + return true; + }, 10_000)); + } + } + + /** + * Asserts that no active transaction remains with the near version. + * + * @param nearVer Near transaction version. + * @throws Exception If waiting fails. + */ + private void assertNoActiveTx(GridCacheVersion nearVer) throws Exception { + for (Ignite ignite : G.allGrids()) { + if (ignite.configuration().isClientMode()) + continue; + + assertTrue(GridTestUtils.waitForCondition(() -> { + GridCacheContext<?, ?> cctx = ((IgniteKernal)ignite).internalCache(DEFAULT_CACHE_NAME).context(); + for (IgniteInternalTx tx : cctx.tm().activeTransactions()) { + if (nearVer.equals(tx.nearXidVersion())) + return false; + } + + return true; + }, 10_000)); + } + } + + /** + * Creates transactional cache. + * + * @param ignite Node. + * @return Transactional cache. + */ + private IgniteCache<Integer, Integer> transactionalCache(Ignite ignite) { + CacheConfiguration<?, ?> ccfg = + new CacheConfiguration<>(DEFAULT_CACHE_NAME) + .setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC) + .setNearConfiguration(useNearCache ? new NearCacheConfiguration<>() : null) + .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL) + .setCacheMode(replicated ? CacheMode.REPLICATED : CacheMode.PARTITIONED) + .setBackups(backups); + + return (IgniteCache<Integer, Integer>)ignite.createCache(ccfg); + } + + /** + * Gets key mapped to a given primary. Review Comment: The description and the name are slightly different, the description only talks about the primary node in the backup name, it is very confusing. ########## modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java: ########## @@ -290,6 +290,57 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport { */ public void suspend() throws IgniteException; + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. The savepoint keeps the current transaction state and can + * later be used by {@link #rollbackToSavepoint(String)} to discard changes made after it was created. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the same name already exists. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name); + + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. If {@code overwrite} is {@code true} and a savepoint with + * the same name exists, the existing savepoint is replaced with a snapshot of the current transaction state. + * + * @param name Savepoint name. + * @param overwrite Whether to overwrite an existing savepoint with the same name. + * @throws TransactionException If savepoint with the same name already exists and {@code overwrite} is + * {@code false}. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name, boolean overwrite); + + /** + * Rolls back transaction changes to the specified savepoint. + * <p> + * Changes made after the savepoint was created are discarded. Savepoints created after the specified savepoint are + * released. The transaction remains active and can be committed or rolled back after this method returns. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the given name does not exist. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void rollbackToSavepoint(String name); + + /** + * Releases a savepoint. If savepoint does not exist this operation does nothing. + * <p> + * Releasing a savepoint does not roll back transaction changes. It removes the specified savepoint and all + * savepoints created after it, so none of them can be used by {@link #rollbackToSavepoint(String)} anymore. + * + * @param name Savepoint name. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void releaseSavepoint(String name); Review Comment: I think it's worth adding `@IgniteExperimental`, the api doesn't seem finished yet. ########## modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxSavepointParameterizedTest.java: ########## @@ -0,0 +1,711 @@ +/* + * 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.ignite.internal.processors.cache.transactions; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.NearCacheConfiguration; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.transactions.Transaction; +import org.apache.ignite.transactions.TransactionIsolation; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; +import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; + +/** + * Tests for transaction savepoint API. + */ +@RunWith(Parameterized.class) +public class TxSavepointParameterizedTest extends GridCommonAbstractTest { + /** */ + private static Ignite ignite0; + + /** */ + private static Ignite ignite1; + + /** */ + private static Ignite ignite2; + + /** */ + private static Ignite ignite3; + + /** */ + private static Ignite client; + + /** */ + @Parameter(0) + public boolean initKeies; + + /** */ + @Parameter(1) + public boolean useNearCache; + + /** */ + @Parameter(2) + public int backups; + + /** */ + @Parameter(3) + public boolean spKeyOnTxInitiator; + + /** */ + @Parameter(4) + public boolean replicated; + + /** */ + @Parameter(5) + public TransactionIsolation transactionIsolation; + + /** + * Returns data for test. + * @return Test parameters. + */ + @Parameters(name = "initKeies={0}, useNearCache={1}, backups={2}, spKeyOnTxInitiator={3}, replicated={4}, txIsolation={5}") + public static Collection<Object[]> testData() { + return List.of(new Object[][] { + // READ_COMMITTED + // backups = 0 + {true, true, 0, true, false, READ_COMMITTED}, + {true, true, 0, false, false, READ_COMMITTED}, + {true, false, 0, true, false, READ_COMMITTED}, + {true, false, 0, false, false, READ_COMMITTED}, + {false, true, 0, true, false, READ_COMMITTED}, + {false, true, 0, false, false, READ_COMMITTED}, + {false, false, 0, true, false, READ_COMMITTED}, + {false, false, 0, false, false, READ_COMMITTED}, + + // backups = 1 + {true, true, 1, true, false, READ_COMMITTED}, + {true, true, 1, false, false, READ_COMMITTED}, + {true, false, 1, true, false, READ_COMMITTED}, + {true, false, 1, false, false, READ_COMMITTED}, + {false, true, 1, true, false, READ_COMMITTED}, + {false, true, 1, false, false, READ_COMMITTED}, + {false, false, 1, true, false, READ_COMMITTED}, + {false, false, 1, false, false, READ_COMMITTED}, + + // backups = 2 + {true, true, 2, true, false, READ_COMMITTED}, + {true, true, 2, false, false, READ_COMMITTED}, + {true, false, 2, true, false, READ_COMMITTED}, + {true, false, 2, false, false, READ_COMMITTED}, + {false, true, 2, true, false, READ_COMMITTED}, + {false, true, 2, false, false, READ_COMMITTED}, + {false, false, 2, true, false, READ_COMMITTED}, + {false, false, 2, false, false, READ_COMMITTED}, + + // replicated cache. + {true, true, 0, true, true, READ_COMMITTED}, + {true, false, 0, true, true, READ_COMMITTED}, + {false, true, 0, true, true, READ_COMMITTED}, + {false, false, 0, true, true, READ_COMMITTED}, + + // REPEATABLE_READ + // backups = 0 + {true, true, 0, true, false, REPEATABLE_READ}, + {true, true, 0, false, false, REPEATABLE_READ}, + {true, false, 0, true, false, REPEATABLE_READ}, + {true, false, 0, false, false, REPEATABLE_READ}, + {false, true, 0, true, false, REPEATABLE_READ}, + {false, true, 0, false, false, REPEATABLE_READ}, + {false, false, 0, true, false, REPEATABLE_READ}, + {false, false, 0, false, false, REPEATABLE_READ}, + + // backups = 1 + {true, true, 1, true, false, REPEATABLE_READ}, + {true, true, 1, false, false, REPEATABLE_READ}, + {true, false, 1, true, false, REPEATABLE_READ}, + {true, false, 1, false, false, REPEATABLE_READ}, + {false, true, 1, true, false, REPEATABLE_READ}, + {false, true, 1, false, false, REPEATABLE_READ}, + {false, false, 1, true, false, REPEATABLE_READ}, + {false, false, 1, false, false, REPEATABLE_READ}, + + // backups = 2 + {true, true, 2, true, false, REPEATABLE_READ}, + {true, true, 2, false, false, REPEATABLE_READ}, + {true, false, 2, true, false, REPEATABLE_READ}, + {true, false, 2, false, false, REPEATABLE_READ}, + {false, true, 2, true, false, REPEATABLE_READ}, + {false, true, 2, false, false, REPEATABLE_READ}, + {false, false, 2, true, false, REPEATABLE_READ}, + {false, false, 2, false, false, REPEATABLE_READ}, + + // replicated cache. + {true, true, 0, true, true, REPEATABLE_READ}, + {true, false, 0, true, true, REPEATABLE_READ}, + {false, true, 0, true, true, REPEATABLE_READ}, + {false, false, 0, true, true, REPEATABLE_READ}, + }); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + ignite0 = startGrid(0); + ignite1 = startGrid(1); + ignite2 = startGrid(2); + ignite3 = startGrid(3); + client = startClientGrid(); + + awaitPartitionMapExchange(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { Review Comment: missing `beforeTestsStarted` ########## modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxEntry.java: ########## @@ -378,6 +378,104 @@ public IgniteTxEntry cleanCopy(GridCacheContext<?, ?> ctx) { return cp; } + /** + * @return Deep-enough copy of this entry to restore its state later. + */ + public IgniteTxEntry copy() { + IgniteTxEntry cp = new IgniteTxEntry(); + + cp.tx = tx; + cp.key = key; + cp.cacheId = cacheId; + cp.txKey = txKey; + cp.ctx = ctx; + cp.val = copyHolder(val); + cp.prevVal.value(prevVal.operation(), prevVal.value(), prevVal.hasWriteValue(), prevVal.hasReadValue()); + cp.oldVal = copyHolder(oldVal); + cp.entryProcessorsCol = entryProcessorsCol == null ? null : new LinkedList<>(entryProcessorsCol); + cp.entryProcessorCalcVal = entryProcessorCalcVal; + cp.transformClosBytes = transformClosBytes; + cp.ttl = ttl; + cp.conflictExpireTime = conflictExpireTime; + cp.conflictVer = conflictVer; + cp.explicitVer = explicitVer; + cp.dhtVer = dhtVer; + cp.filters = filters; + cp.filtersPassed = filtersPassed; + cp.filtersSet = filtersSet; + cp.entry = entry; + cp.prepared = prepared; + cp.locked = locked; + cp.nodeId = nodeId; + cp.locMapped = locMapped; + cp.expiryPlc = expiryPlc; + cp.transferExpiryPlc = transferExpiryPlc; + cp.expiryPlcBytes = expiryPlcBytes; + cp.flags = flags; + cp.partUpdateCntr = partUpdateCntr; + cp.serReadVer = serReadVer; + + return cp; + } + + /** + * Restores this entry from a previously captured snapshot. + * + * @param snapshot Snapshot. + */ + public void restoreFrom(IgniteTxEntry snapshot) { + tx = snapshot.tx; + key = snapshot.key; + cacheId = snapshot.cacheId; + txKey = snapshot.txKey; + ctx = snapshot.ctx; + val = copyHolder(snapshot.val); + prevVal.value( + snapshot.prevVal.operation(), + snapshot.prevVal.value(), + snapshot.prevVal.hasWriteValue(), + snapshot.prevVal.hasReadValue() + ); + oldVal = copyHolder(snapshot.oldVal); + entryProcessorsCol = snapshot.entryProcessorsCol == null ? null : new LinkedList<>(snapshot.entryProcessorsCol); + entryProcessorCalcVal = snapshot.entryProcessorCalcVal; + transformClosBytes = snapshot.transformClosBytes; + ttl = snapshot.ttl; + conflictExpireTime = snapshot.conflictExpireTime; + conflictVer = snapshot.conflictVer; + explicitVer = snapshot.explicitVer; + dhtVer = snapshot.dhtVer; + filters = snapshot.filters; + filtersPassed = snapshot.filtersPassed; + filtersSet = snapshot.filtersSet; + entry = snapshot.entry; + prepared = snapshot.prepared; + locked = snapshot.locked; + nodeId = snapshot.nodeId; + locMapped = snapshot.locMapped; + expiryPlc = snapshot.expiryPlc; + transferExpiryPlc = snapshot.transferExpiryPlc; + expiryPlcBytes = snapshot.expiryPlcBytes; + flags = snapshot.flags; + partUpdateCntr = snapshot.partUpdateCntr; + serReadVer = snapshot.serReadVer; + } + + /** + * @param holder Holder to copy. + * @return Copy. + */ + private static TxEntryValueHolder copyHolder(@Nullable TxEntryValueHolder holder) { Review Comment: ```suggestion private static @Nullable TxEntryValueHolder copyHolder(@Nullable TxEntryValueHolder holder) { ``` ########## modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java: ########## @@ -290,6 +290,57 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport { */ public void suspend() throws IgniteException; + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. The savepoint keeps the current transaction state and can + * later be used by {@link #rollbackToSavepoint(String)} to discard changes made after it was created. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the same name already exists. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name); Review Comment: I think it's worth adding `@IgniteExperimental`, the api doesn't seem finished yet. ########## modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java: ########## @@ -290,6 +290,57 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport { */ public void suspend() throws IgniteException; + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. The savepoint keeps the current transaction state and can + * later be used by {@link #rollbackToSavepoint(String)} to discard changes made after it was created. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the same name already exists. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name); + + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. If {@code overwrite} is {@code true} and a savepoint with + * the same name exists, the existing savepoint is replaced with a snapshot of the current transaction state. + * + * @param name Savepoint name. + * @param overwrite Whether to overwrite an existing savepoint with the same name. + * @throws TransactionException If savepoint with the same name already exists and {@code overwrite} is + * {@code false}. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name, boolean overwrite); + + /** + * Rolls back transaction changes to the specified savepoint. + * <p> + * Changes made after the savepoint was created are discarded. Savepoints created after the specified savepoint are + * released. The transaction remains active and can be committed or rolled back after this method returns. Review Comment: Maybe not `released` but "rolled back" ? ########## modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java: ########## @@ -290,6 +290,57 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport { */ public void suspend() throws IgniteException; + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. The savepoint keeps the current transaction state and can + * later be used by {@link #rollbackToSavepoint(String)} to discard changes made after it was created. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the same name already exists. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name); + + /** + * Creates a savepoint in the current transaction. + * <p> + * Savepoints are supported only for explicit transactions with + * {@link TransactionConcurrency#PESSIMISTIC} concurrency. If {@code overwrite} is {@code true} and a savepoint with + * the same name exists, the existing savepoint is replaced with a snapshot of the current transaction state. + * + * @param name Savepoint name. + * @param overwrite Whether to overwrite an existing savepoint with the same name. + * @throws TransactionException If savepoint with the same name already exists and {@code overwrite} is + * {@code false}. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void savepoint(String name, boolean overwrite); + + /** + * Rolls back transaction changes to the specified savepoint. + * <p> + * Changes made after the savepoint was created are discarded. Savepoints created after the specified savepoint are + * released. The transaction remains active and can be committed or rolled back after this method returns. + * + * @param name Savepoint name. + * @throws TransactionException If savepoint with the given name does not exist. + * @throws IgniteException If savepoints are not supported for this transaction. + */ + public void rollbackToSavepoint(String name); + + /** + * Releases a savepoint. If savepoint does not exist this operation does nothing. + * <p> + * Releasing a savepoint does not roll back transaction changes. It removes the specified savepoint and all Review Comment: Maybe indicate that the resources used by the savepoint are being freed? ########## modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java: ########## @@ -219,6 +224,9 @@ public class GridNearTxLocal extends GridDhtTxLocalAdapter implements GridTimeou /** Tx label. */ @Nullable private final String lb; + /** Savepoints created for this transaction. */ Review Comment: ```suggestion /** Savepoints created for this transaction. Guarded by {@code this}. */ ``` ########## modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java: ########## @@ -219,6 +224,9 @@ public class GridNearTxLocal extends GridDhtTxLocalAdapter implements GridTimeou /** Tx label. */ @Nullable private final String lb; + /** Savepoints created for this transaction. */ + private LinkedList<TxSavepoint> savepoints; Review Comment: `LinkedList` is quite outdated, I think we can replace it with `List` and use `ArrayList`. -- 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]
