IGNITE-10884: Fixed failure when performing non-MVCC SQL from transactions. This...
authorrkondakov <kondakov87@mail.ru>
Fri, 11 Jan 2019 15:33:01 +0000 (18:33 +0300)
committerIgor Seliverstov <gvvinblade@gmail.com>
Fri, 11 Jan 2019 15:33:18 +0000 (18:33 +0300)
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccUtils.java
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/mvcc/MvccUnsupportedTxModesTest.java
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/mvcc/CacheMvccSqlTxModesTest.java [new file with mode: 0644]
modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheMvccSqlTestSuite.java

index 0ceed09..c6848d3 100644 (file)
@@ -704,7 +704,7 @@ public class MvccUtils {
         GridNearTxLocal tx = tx0 != null && tx0.user() ? (GridNearTxLocal)tx0 : null;
 
         if (tx != null) {
-            if (!tx.pessimistic() || !tx.repeatableRead()) {
+            if (!tx.pessimistic()) {
                 tx.setRollbackOnly();
 
                 throw new UnsupportedTxModeException();
@@ -948,7 +948,7 @@ public class MvccUtils {
         private static final long serialVersionUID = 0L;
         /** */
         private UnsupportedTxModeException() {
-            super("Only pessimistic repeatable read transactions are supported when MVCC is enabled.");
+            super("Only pessimistic transactions are supported when MVCC is enabled.");
         }
     }
 
index dc1f2cb..e1b3e41 100644 (file)
@@ -348,8 +348,10 @@ public class MvccUnsupportedTxModesTest extends GridCommonAbstractTest {
         assertNotSupportedInTx(action, OPTIMISTIC, READ_COMMITTED);
         assertNotSupportedInTx(action, OPTIMISTIC, REPEATABLE_READ);
         assertNotSupportedInTx(action, OPTIMISTIC, SERIALIZABLE);
-        assertNotSupportedInTx(action, PESSIMISTIC, READ_COMMITTED);
-        assertNotSupportedInTx(action, PESSIMISTIC, SERIALIZABLE);
+
+        assertSupportedInTx(action, PESSIMISTIC, READ_COMMITTED);
+        assertSupportedInTx(action, PESSIMISTIC, REPEATABLE_READ);
+        assertSupportedInTx(action, PESSIMISTIC, SERIALIZABLE);
     }
 
     /** */
@@ -360,7 +362,16 @@ public class MvccUnsupportedTxModesTest extends GridCommonAbstractTest {
             fail("Action failure is expected.");
         }
         catch (TransactionException e) {
-            assertEquals("Only pessimistic repeatable read transactions are supported when MVCC is enabled.", e.getMessage());
+            assertEquals("Only pessimistic transactions are supported when MVCC is enabled.", e.getMessage());
+        }
+    }
+
+    /** */
+    private void assertSupportedInTx(Runnable action, TransactionConcurrency conc, TransactionIsolation iso) {
+        try (Transaction tx = grid(0).transactions().txStart(conc, iso)) {
+            action.run();
+
+            tx.commit();
         }
     }
 }
index 53530a3..4c26e35 100644 (file)
@@ -177,10 +177,10 @@ import static org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.txStart
 import static org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.TEXT;
 import static org.apache.ignite.internal.processors.query.h2.PreparedStatementEx.MVCC_CACHE_ID;
 import static org.apache.ignite.internal.processors.query.h2.PreparedStatementEx.MVCC_STATE;
-import static org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinMode.OFF;
-import static org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinMode.distributedJoinMode;
 import static org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryType.LOCAL;
 import static org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryType.PREPARE;
+import static org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinMode.OFF;
+import static org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinMode.distributedJoinMode;
 
 /**
  * Indexing implementation based on H2 database engine. In this implementation main query language is SQL,
@@ -1119,7 +1119,7 @@ public class IgniteH2Indexing implements GridQueryIndexing {
             final MvccQueryTracker tracker = mvccTracker == null && qry.mvccEnabled() ?
                 MvccUtils.mvccTracker(ctx.cache().context().cacheContext(qry.cacheIds().get(0)), startTx) : mvccTracker;
 
-            GridNearTxLocal tx = tx(ctx);
+            GridNearTxLocal tx = tracker != null ? tx(ctx) : null;
 
             if (qry.forUpdate())
                 qry.forUpdate(checkActive(tx) != null);
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/mvcc/CacheMvccSqlTxModesTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/mvcc/CacheMvccSqlTxModesTest.java
new file mode 100644 (file)
index 0000000..05f30b6
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+ * 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.mvcc;
+
+import java.util.concurrent.Callable;
+import javax.cache.CacheException;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.transactions.Transaction;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT;
+import static org.apache.ignite.cache.CacheMode.PARTITIONED;
+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;
+import static org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE;
+
+/**
+ *
+ */
+public class CacheMvccSqlTxModesTest extends CacheMvccAbstractTest {
+    /** {@inheritDoc} */
+    @Override protected CacheMode cacheMode() {
+        return PARTITIONED;
+    }
+
+    /**
+     * @throws Exception If failed
+     */
+    @Test
+    public void testSqlTransactionModesNoMvcc() throws Exception {
+        IgniteEx node = startGrid(0);
+
+        IgniteCache<Object, Object> nonMvccCache = node.createCache(new CacheConfiguration<>("no-mvcc-cache")
+            .setAtomicityMode(TRANSACTIONAL).setIndexedTypes(Integer.class, Integer.class));
+
+        nonMvccCache.put(1,1);
+
+        try (Transaction tx = node.transactions().txStart(OPTIMISTIC, READ_COMMITTED)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, SERIALIZABLE)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer").setLocal(true)).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, SERIALIZABLE)) {
+            nonMvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer").setLocal(true)).getAll();
+
+            tx.commit();
+        }
+    }
+
+    /**
+     * @throws Exception If failed
+     */
+    @Test
+    public void testSqlTransactionModesMvcc() throws Exception {
+        IgniteEx node = startGrid(0);
+
+        IgniteCache<Object, Object> mvccCache = node.createCache(new CacheConfiguration<>("mvcc-cache")
+            .setAtomicityMode(TRANSACTIONAL_SNAPSHOT).setIndexedTypes(Integer.class, Integer.class));
+
+        mvccCache.put(1,1);
+
+        GridTestUtils.assertThrows(log, new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                try (Transaction tx = node.transactions().txStart(OPTIMISTIC, READ_COMMITTED)) {
+                    mvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+                    tx.commit();
+                }
+
+                return null;
+            }
+        }, CacheException.class, "Only pessimistic transactions are supported when MVCC is enabled");
+
+        GridTestUtils.assertThrows(log, new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                try (Transaction tx = node.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
+                    mvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+                    tx.commit();
+                }
+
+                return null;
+            }
+        }, CacheException.class, "Only pessimistic transactions are supported when MVCC is enabled");
+
+        GridTestUtils.assertThrows(log, new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                try (Transaction tx = node.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
+                    mvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+                    tx.commit();
+                }
+
+                return null;
+            }
+        }, CacheException.class, "Only pessimistic transactions are supported when MVCC is enabled");
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
+            mvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            mvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, SERIALIZABLE)) {
+            mvccCache.query(new SqlFieldsQuery("SELECT * FROM Integer")).getAll();
+
+            tx.commit();
+        }
+    }
+
+    /**
+     * @throws Exception If failed
+     */
+    @Test
+    public void testConsequentMvccNonMvccOperations() throws Exception {
+        IgniteEx node = startGrid(0);
+
+        IgniteCache<Object, Object> mvccCache = node.createCache(new CacheConfiguration<>("mvcc-cache")
+            .setAtomicityMode(TRANSACTIONAL_SNAPSHOT).setIndexedTypes(Integer.class, Integer.class));
+
+        IgniteCache<Object, Object> nonMvccCache = node.createCache(new CacheConfiguration<>("no-mvcc-cache")
+            .setAtomicityMode(TRANSACTIONAL).setIndexedTypes(Integer.class, Integer.class));
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            nonMvccCache.put(1, 1);
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            mvccCache.query(new SqlFieldsQuery("INSERT INTO Integer (_key, _val) VALUES (3,3)")).getAll();
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            nonMvccCache.put(2, 2);
+
+            tx.commit();
+        }
+
+        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
+            mvccCache.query(new SqlFieldsQuery("INSERT INTO Integer (_key, _val) VALUES (5,5)")).getAll();
+
+            tx.commit();
+        }
+    }
+}
index bdc7664..0418914 100644 (file)
@@ -62,6 +62,7 @@ import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccSqlConfiguratio
 import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccSqlContinuousQueryPartitionedSelfTest;
 import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccSqlContinuousQueryReplicatedSelfTest;
 import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccSqlLockTimeoutTest;
+import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccSqlTxModesTest;
 import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccSqlUpdateCountersTest;
 import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccStreamingInsertTest;
 import org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxNodeMappingTest;
@@ -87,7 +88,7 @@ import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT;
     CacheMvccSizeTest.class,
     CacheMvccSqlUpdateCountersTest.class,
     CacheMvccSqlLockTimeoutTest.class,
-
+    CacheMvccSqlTxModesTest.class,
     GridIndexRebuildWithMvccEnabledSelfTest.class,
 
     CacheMvccTxNodeMappingTest.class,