vldpyatkov commented on code in PR #13366:
URL: https://github.com/apache/ignite/pull/13366#discussion_r3713992222


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SelectForUpdateIntegrationTest.java:
##########
@@ -0,0 +1,611 @@
+/*
+ * 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.query.calcite.integration;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.SqlConfiguration;
+import org.apache.ignite.configuration.TransactionConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.TestRecordingCommunicationSpi;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import 
org.apache.ignite.internal.processors.query.calcite.message.QueryBatchMessage;
+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.TransactionState.ACTIVE;
+
+/**
+ * Integration tests for {@code SELECT ... FOR UPDATE} syntax.
+ */
+public class SelectForUpdateIntegrationTest extends GridCommonAbstractTest {
+    /** */
+    private static Ignite ignite0;
+
+    /** */
+    private static Ignite ignite1;
+
+    /** */
+    private static Ignite client;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        return super.getConfiguration(igniteInstanceName)
+            .setTransactionConfiguration(new TransactionConfiguration()
+                .setTxAwareQueriesEnabled(true))
+            .setSqlConfiguration(new SqlConfiguration()
+                .setQueryEnginesConfiguration(new 
CalciteQueryEngineConfiguration()
+                    .setDefault(true)))
+            .setCommunicationSpi(new TestRecordingCommunicationSpi());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        ignite0 = startGridsMultiThreaded(3);
+        ignite1 = grid(1);
+        client = startClientGrid();
+
+        awaitPartitionMapExchange();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        stopAllGrids();
+
+        ignite0 = null;
+        ignite1 = null;
+        client = null;
+
+        super.afterTestsStopped();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        sql("CREATE TABLE Person (id INT PRIMARY KEY, name VARCHAR, age INT, 
deptId INT, managerId INT) " +
+            "WITH atomicity=TRANSACTIONAL");
+        sql("INSERT INTO Person (id, name, age) VALUES " +
+            "(1, 'Alice', 20), (2, 'Bob', 21), (3, 'Ann', 22), (4, 'Bill', 
23)" +
+            ", (5, 'Alex', 24), (6, 'Ben', 25), (7, 'Cathy', 26), (8, 'Carl', 
27), (9, 'Diana', 28)" +
+            ", (10, 'David', 29), (11, 'Eva', 30), (12, 'Evan', 31), (13, 
'Fiona', 32), (14, 'Frank', 33)" +
+            ", (15, 'Grace', 34), (16, 'George', 35), (17, 'Hannah', 36), (18, 
'Harry', 37), (19, 'Ivy', 38)" +
+            ", (20, 'Ian', 39), (21, 'Jack', 40), (22, 'Jill', 41), (23, 
'Karen', 42), (24, 'Kyle', 43)" +
+            ", (25, 'Laura', 44), (26, 'Leo', 45), (27, 'Mia', 46), (28, 
'Mike', 47), (29, 'Nina', 48)" +
+            ", (30, 'Nick', 49)");
+        sql("UPDATE Person SET deptId = 1, managerId = 2 WHERE id = 1");
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        sql("DROP TABLE IF EXISTS Dept");
+        sql("DROP TABLE IF EXISTS Person");
+
+        super.afterTest();
+    }
+
+    /** SELECT FOR UPDATE without OF locks rows of every table participating 
in a JOIN. */
+    @Test
+    public void testSelectForUpdateJoinLocksAllTables() throws Exception {
+        createDeptTable();
+
+        CountDownLatch locked = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+
+        IgniteInternalFuture<?> lockFut = GridTestUtils.runAsync(() -> {
+            try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertRows(
+                    sql("SELECT p.id FROM Person p JOIN Dept d ON p.deptId = 
d.id WHERE p.id = 1 FOR UPDATE"),
+                    Arrays.asList(1)
+                );
+
+                locked.countDown();
+
+                assertTrue("Timed out waiting to release JOIN locks", 
release.await(30, TimeUnit.SECONDS));
+
+                tx.commit();
+            }
+        });
+
+        try {
+            assertTrue("JOIN transaction did not acquire locks in time", 
locked.await(10, TimeUnit.SECONDS));
+
+            assertTableRowLocked(ignite1, "Person", 1);
+            assertTableRowLocked(ignite1, "Dept", 1);
+        }
+        finally {
+            release.countDown();
+        }
+
+        lockFut.get(10_000);
+    }
+
+    /** FOR UPDATE OF locks only the table owning the specified JOIN column. */
+    @Test
+    public void testSelectForUpdateJoinOfLocksSelectedTable() throws Exception 
{
+        createDeptTable();
+
+        CountDownLatch locked = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+
+        IgniteInternalFuture<?> lockFut = GridTestUtils.runAsync(() -> {
+            try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertRows(
+                    sql("SELECT p.id FROM Person p JOIN Dept d ON p.deptId = 
d.id WHERE p.id = 1 FOR UPDATE OF p.id"),
+                    Arrays.asList(1)
+                );
+
+                locked.countDown();
+
+                assertTrue("Timed out waiting to release JOIN locks", 
release.await(30, TimeUnit.SECONDS));
+
+                tx.commit();
+            }
+        });
+
+        try {
+            assertTrue("JOIN transaction did not acquire locks in time", 
locked.await(10, TimeUnit.SECONDS));
+
+            assertTableRowLocked(ignite1, "Person", 1);
+
+            try (Transaction tx = ignite1.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertRows(sql(ignite1, "SELECT * FROM Dept WHERE id = 1 FOR 
UPDATE NOWAIT"),
+                    Arrays.asList(1, "Engineering"));
+
+                tx.commit();
+            }
+        }
+        finally {
+            release.countDown();
+        }
+
+        lockFut.get(10_000);
+    }
+
+    /** A qualified OF column selects a particular table occurrence in a 
self-join. */
+    @Test
+    public void testSelectForUpdateSelfJoinOfUsesAlias() throws Exception {
+        assertSelfJoinOfLocks("employee", 1, 2);
+        assertSelfJoinOfLocks("manager", 2, 1);
+    }
+
+    /** Creates a second transactional table used by JOIN tests. */
+    private void createDeptTable() {
+        sql("CREATE TABLE Dept (id INT PRIMARY KEY, name VARCHAR) WITH 
atomicity=TRANSACTIONAL");
+        sql("INSERT INTO Dept VALUES (1, 'Engineering'), (2, 'Sales'), (3, 
'HR')");
+    }
+
+    /** Verifies that OF resolves an alias to the correct side of a self-join. 
*/
+    private void assertSelfJoinOfLocks(String alias, int lockedId, int 
unlockedId) throws Exception {
+        CountDownLatch locked = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+
+        IgniteInternalFuture<?> lockFut = GridTestUtils.runAsync(() -> {
+            try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertRows(sql("SELECT employee.id, manager.id FROM Person 
employee " +
+                    "JOIN Person manager ON employee.managerId = manager.id " +
+                    "WHERE employee.id = 1 FOR UPDATE OF " + alias + ".id"), 
Arrays.asList(1, 2));
+
+                locked.countDown();
+
+                assertTrue("Timed out waiting to release self-join lock", 
release.await(30, TimeUnit.SECONDS));
+
+                tx.commit();
+            }
+        });
+
+        try {
+            assertTrue("Self-join transaction did not acquire lock in time", 
locked.await(10, TimeUnit.SECONDS));
+
+            assertTableRowLocked(ignite1, "Person", lockedId);
+            assertTableRowUnlocked(ignite1, "Person", unlockedId);
+        }
+        finally {
+            release.countDown();
+        }
+
+        lockFut.get(10_000);
+    }
+
+    /** FOR UPDATE without an active transaction produces "requires an active 
PESSIMISTIC transaction". */
+    @Test
+    public void testSelectForUpdateOutsideTransaction() {
+        GridTestUtils.assertThrowsAnyCause(log, () -> sql("SELECT id FROM 
Person FOR UPDATE"),
+            IgniteSQLException.class, "SELECT FOR UPDATE requires an active 
PESSIMISTIC transaction");

Review Comment:
   Ok



-- 
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]

Reply via email to