vldpyatkov commented on code in PR #13080: URL: https://github.com/apache/ignite/pull/13080#discussion_r3161075363
########## 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: These are different tests. The first creates an empty context savepoint, acquires keys, and rolls back to the empty one. The second acquires a key and then releases the lock by restoring a savepoint in the past and then acquires it again. -- 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]
