ignite-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [ignite] Jokser commented on a change in pull request #6483: IGNITE-5714 Implementation of suspend/resume for pessimistic transactions
Date Fri, 07 Jun 2019 15:14:35 GMT
Jokser commented on a change in pull request #6483: IGNITE-5714 Implementation of suspend/resume
for pessimistic transactions
URL: https://github.com/apache/ignite/pull/6483#discussion_r291637190
 
 

 ##########
 File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/IgniteAbstractTxSuspendResumeTest.java
 ##########
 @@ -0,0 +1,771 @@
+/*
+ * 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.distributed;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.NearCacheConfiguration;
+import org.apache.ignite.internal.util.typedef.CI1;
+import org.apache.ignite.internal.util.typedef.CI2;
+import org.apache.ignite.internal.util.typedef.PA;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.internal.util.typedef.internal.U;
+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.TransactionConcurrency;
+import org.apache.ignite.transactions.TransactionIsolation;
+import org.apache.ignite.transactions.TransactionTimeoutException;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.LOCAL;
+import static org.apache.ignite.cache.CacheMode.PARTITIONED;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
+import static org.apache.ignite.transactions.TransactionState.ACTIVE;
+import static org.apache.ignite.transactions.TransactionState.COMMITTED;
+import static org.apache.ignite.transactions.TransactionState.ROLLED_BACK;
+import static org.apache.ignite.transactions.TransactionState.SUSPENDED;
+
+/**
+ *
+ */
+public abstract class IgniteAbstractTxSuspendResumeTest extends GridCommonAbstractTest {
+    /** Force mvcc. */
+    protected static final boolean FORCE_MVCC =
+        IgniteSystemProperties.getBoolean(IgniteSystemProperties.IGNITE_FORCE_MVCC_MODE_IN_TESTS,
false);
+
+    /** Transaction timeout. */
+    private static final long TX_TIMEOUT = 400L;
+
+    /** Future timeout */
+    protected static final int FUT_TIMEOUT = 5000;
+
+    /** */
+    protected static final int CLIENT_CNT = 2;
+
+    /** */
+    protected static final int SERVER_CNT = 4;
+
+    /** */
+    protected static final int GRID_CNT = CLIENT_CNT + SERVER_CNT;
+
+    /**
+     * List of closures to execute transaction operation that prohibited in suspended state.
+     */
+    private static final List<CI1Exc<Transaction>> SUSPENDED_TX_PROHIBITED_OPS
= Arrays.asList(
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.suspend();
+            }
+        },
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.close();
+            }
+        },
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.commit();
+            }
+        },
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.commitAsync().get(FUT_TIMEOUT);
+            }
+        },
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.rollback();
+            }
+        },
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.rollbackAsync().get(FUT_TIMEOUT);
+            }
+        },
+        new CI1Exc<Transaction>() {
+            @Override public void applyx(Transaction tx) throws Exception {
+                tx.setRollbackOnly();
+            }
+        }
+    );
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws
Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        int idx = getTestIgniteInstanceIndex(igniteInstanceName);
+
+        boolean client = idx >= SERVER_CNT && idx < GRID_CNT;
+
+        cfg.setClientMode(client);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        startGridsMultiThreaded(gridCount());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        Ignite client = ignite(gridCount() - 1);
+
+        assertTrue(client.cluster().localNode().isClient());
+
+        for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
+            grid(0).createCache(ccfg);
+
+            if (ccfg.getCacheMode() != LOCAL && !FORCE_MVCC)
+                client.createNearCache(ccfg.getName(), new NearCacheConfiguration<>());
+        }
+
+        awaitPartitionMapExchange();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        for (CacheConfiguration ccfg : cacheConfigurations())
+            ignite(0).destroyCache(ccfg.getName());
+
+        super.afterTest();
+    }
+
+    /**
+     * @return Number of server nodes.
+     */
+    protected int gridCount() {
+        return GRID_CNT;
+    }
+
+    /**
+     * @return Transaction concurrency.
+     */
+    protected abstract TransactionConcurrency transactionConcurrency();
+
+    /**
+     * Test for transaction starting in one thread, continuing in another.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testResumeTxInAnotherThread() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    final Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation);
+
+                    final AtomicInteger cntr = new AtomicInteger(0);
+
+                    for (int j = -1; j > -10; j--)
+                        cache.put(j, j);
+
+                    cache.put(cntr.get(), cntr.getAndIncrement());
+
+                    tx.suspend();
+
+                    assertEquals(SUSPENDED, tx.state());
+
+                    assertNull("Thread already have tx", ignite.transactions().tx());
+
+                    assertNull(cache.get(-1));
+                    assertNull(cache.get(cntr.get()));
+
+                    for (int i = 0; i < 10; i++) {
+                        GridTestUtils.runAsync(new Runnable() {
+                            @Override public void run() {
+                                assertEquals(SUSPENDED, tx.state());
+
+                                tx.resume();
+
+                                assertEquals(ACTIVE, tx.state());
+
+                                for (int j = -1; j > -10; j--)
+                                    cache.put(j, j);
+
+                                cache.put(cntr.get(), cntr.getAndIncrement());
+
+                                tx.suspend();
+                            }
+                        }).get(FUT_TIMEOUT);
+                    }
+
+                    tx.resume();
+
+                    for (int j = -1; j > -10; j--)
+                        cache.remove(j);
+
+                    tx.commit();
+
+                    assertEquals(COMMITTED, tx.state());
+
+                    for (int i = 0; i < cntr.get(); i++)
+                        assertEquals(i, (int)cache.get(i));
+
+                    assertFalse(cache.containsKey(-1));
+
+                    cache.removeAll();
+                }
+            }
+        });
+    }
+
+    /**
+     * Test for transaction starting in one thread, continuing in another, and resuming in
initiating thread.
+     * Cache operations performed for a couple of caches.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testCrossCacheTxInAnotherThread() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                // TODO: IGNITE-9110 Optimistic tx hangs in cross-cache operations with LOCAL
and non LOCAL caches.
+                if (transactionConcurrency() == TransactionConcurrency.OPTIMISTIC
+                    && cache.getConfiguration(CacheConfiguration.class).getCacheMode()
== LOCAL)
+                    return;
+
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    final IgniteCache<Integer, Integer> otherCache = ignite.getOrCreateCache(
+                        cacheConfiguration("otherCache", PARTITIONED, 0, false));
+
+                    final Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation);
+
+                    final AtomicInteger cntr = new AtomicInteger(0);
+
+                    cache.put(-1, -1);
+                    otherCache.put(-1, -1);
+
+                    tx.suspend();
+
+                    for (int i = 0; i < 10; i++) {
+                        GridTestUtils.runAsync(new Runnable() {
+                            @Override public void run() {
+                                tx.resume();
+
+                                assertEquals(ACTIVE, tx.state());
+
+                                cache.put(-1, -1);
+                                otherCache.put(-1, -1);
+
+                                cache.put(cntr.get(), cntr.get());
+                                otherCache.put(cntr.get(), cntr.getAndIncrement());
+
+                                tx.suspend();
+                            }
+                        }).get(FUT_TIMEOUT);
+                    }
+
+                    tx.resume();
+
+                    cache.remove(-1);
+                    otherCache.remove(-1);
+
+                    tx.commit();
+
+                    assertEquals(COMMITTED, tx.state());
+
+                    for (int i = 0; i < cntr.get(); i++) {
+                        assertEquals(i, (int)cache.get(i));
+                        assertEquals(i, (int)otherCache.get(i));
+                    }
+
+                    assertFalse(cache.containsKey(-1));
+                    assertFalse(otherCache.containsKey(-1));
+
+                    cache.removeAll();
+                    otherCache.removeAll();
+                }
+            }
+        });
+    }
+
+    /**
+     * Test for transaction rollback.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testTxRollback() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    final Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation);
+
+                    cache.put(1, 1);
+                    cache.put(2, 2);
+
+                    tx.suspend();
+
+                    assertNull("There is no transaction for current thread", ignite.transactions().tx());
+
+                    assertEquals(SUSPENDED, tx.state());
+
+                    GridTestUtils.runAsync(new Runnable() {
+                        @Override public void run() {
+                            tx.resume();
+
+                            assertEquals(ACTIVE, tx.state());
+
+                            cache.put(3, 3);
+
+                            tx.rollback();
+                        }
+                    }).get(FUT_TIMEOUT);
+
+                    assertTrue(GridTestUtils.waitForCondition(new PA() {
+                        @Override public boolean apply() {
+                            return tx.state() == ROLLED_BACK;
+                        }
+                    }, getTestTimeout()));
+
+                    assertEquals(ROLLED_BACK, tx.state());
+
+                    assertFalse(cache.containsKey(1));
+                    assertFalse(cache.containsKey(2));
+                    assertFalse(cache.containsKey(3));
+
+                    cache.removeAll();
+                }
+            }
+        });
+    }
+
+    /**
+     * Test for starting and suspending transactions, and then resuming and committing in
another thread.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testMultiTxSuspendResume() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    final List<Transaction> clientTxs = new ArrayList<>();
+
+                    for (int i = 0; i < 10; i++) {
+                        Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation);
+
+                        cache.put(i, i);
+
+                        tx.suspend();
+
+                        clientTxs.add(tx);
+                    }
+
+                    GridTestUtils.runMultiThreaded(new CI1Exc<Integer>() {
+                        @Override public void applyx(Integer idx) throws Exception {
+                            Transaction tx = clientTxs.get(idx);
+
+                            assertEquals(SUSPENDED, tx.state());
+
+                            tx.resume();
+
+                            assertEquals(ACTIVE, tx.state());
+
+                            tx.commit();
+                        }
+                    }, 10, "th-suspend");
+
+                    for (int i = 0; i < 10; i++)
+                        assertEquals(i, (int)cache.get(i));
+
+                    cache.removeAll();
+                }
+            }
+        });
+    }
+
+    /**
+     * Test checking all operations(exception resume) on suspended transaction from the other
thread are prohibited.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testOpsProhibitedOnSuspendedTxFromOtherThread() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (final CI1Exc<Transaction> txOperation : SUSPENDED_TX_PROHIBITED_OPS)
{
+                    for (TransactionIsolation isolation : TransactionIsolation.values())
{
+                        final Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation);
+
+                        cache.put(1, 1);
+
+                        tx.suspend();
+
+                        multithreaded(new RunnableX() {
+                            @Override public void runx() throws Exception {
+                                GridTestUtils.assertThrowsWithCause(txOperation, tx, IgniteException.class);
+                            }
+                        }, 1);
+
+                        tx.resume();
+                        tx.close();
+
+                        assertNull(cache.get(1));
+                    }
+                }
+            }
+        });
+    }
+
+    /**
+     * Test checking all operations(exception resume) on suspended transaction are prohibited.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testOpsProhibitedOnSuspendedTx() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (CI1Exc<Transaction> txOperation : SUSPENDED_TX_PROHIBITED_OPS)
{
+                    for (TransactionIsolation isolation : TransactionIsolation.values())
{
+                        Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation);
+
+                        cache.put(1, 1);
+
+                        tx.suspend();
+
+                        GridTestUtils.assertThrowsWithCause(txOperation, tx, IgniteException.class);
+
+                        tx.resume();
+                        tx.close();
+
+                        assertNull(cache.get(1));
+                    }
+                }
+            }
+        });
+    }
+
+    /**
+     * Test checking timeout on resumed transaction.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testTxTimeoutOnResumed() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    final Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
+                        isolation, TX_TIMEOUT, 0);
+
+                    cache.put(1, 1);
+
+                    tx.suspend();
+
+                    GridTestUtils.runAsync(() -> {
+                            tx.resume();
+
+                            cache.put(1, 1);
+                            cache.put(2, 2);
+
+                            tx.suspend();
+                    }).get(FUT_TIMEOUT);
+
+                    tx.resume();
+
+                    cache.put(1, 1);
+                    cache.put(2, 2);
+                    cache.put(3, 3);
+
+                    tx.suspend();
+
+                    U.sleep(TX_TIMEOUT + 100L);
+
+                    GridTestUtils.assertThrowsWithCause(new Callable<Object>() {
+                        @Override public Object call() throws Exception {
+                            tx.resume();
+
+                            return null;
+                        }
+                    }, TransactionTimeoutException.class);
+
+                    assertTrue(GridTestUtils.waitForCondition(new PA() {
+                        @Override public boolean apply() {
+                            return tx.state() == ROLLED_BACK;
+                        }
+                    }, getTestTimeout()));
+
+                    assertEquals(ROLLED_BACK, tx.state());
+
+                    // Here we check that we can start any transactional operation in the
same thread after a suspended
+                    // transaction is timed-out.
+                    assertFalse(cache.containsKey(1));
+
+                    tx.close();
+
+                    assertFalse(cache.containsKey(1));
+                    assertFalse(cache.containsKey(2));
+                    assertFalse(cache.containsKey(3));
+                }
+            }
+        });
+    }
+
+    /**
+     * Test checking timeout on suspended transaction.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testTxTimeoutOnSuspend() throws Exception {
+        executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>()
{
+            @Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer>
cache) throws Exception {
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    final Transaction tx = ignite.transactions().txStart(transactionConcurrency(),
isolation,
+                        TX_TIMEOUT, 0);
+
+                    cache.put(1, 1);
+
+                    tx.suspend();
+
+                    GridTestUtils.runAsync(() -> {
+                        tx.resume();
+
+                        cache.put(1, 1);
+                        cache.put(2, 2);
+
+                        tx.suspend();
+                    }).get(FUT_TIMEOUT);
+
+                    tx.resume();
+
+                    cache.put(1, 1);
+                    cache.put(2, 2);
+                    cache.put(3, 3);
+
+                    U.sleep(TX_TIMEOUT + 100L);
+
+                    GridTestUtils.assertThrowsWithCause(new Callable<Object>() {
 
 Review comment:
   Can be replaced with method reference

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message