|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.processors.tx; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import org.apache.ignite.IgniteCache; |
| 25 | +import org.apache.ignite.IgniteCheckedException; |
| 26 | +import org.apache.ignite.cache.CacheAtomicityMode; |
| 27 | +import org.apache.ignite.configuration.CacheConfiguration; |
| 28 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 29 | +import org.apache.ignite.internal.IgniteInternalFuture; |
| 30 | +import org.apache.ignite.internal.processors.query.IgniteSQLException; |
| 31 | +import org.apache.ignite.internal.processors.query.calcite.exec.rel.AbstractExecutionTest; |
| 32 | +import org.apache.ignite.internal.processors.query.calcite.integration.AbstractBasicIntegrationTest; |
| 33 | +import org.apache.ignite.internal.util.typedef.X; |
| 34 | +import org.apache.ignite.testframework.GridTestUtils; |
| 35 | +import org.apache.ignite.transactions.Transaction; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.junit.runners.Parameterized; |
| 39 | + |
| 40 | +import static org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.IGNITE_CALCITE_USE_QUERY_BLOCKING_TASK_EXECUTOR; |
| 41 | +import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; |
| 42 | +import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED; |
| 43 | + |
| 44 | +/** */ |
| 45 | +@RunWith(Parameterized.class) |
| 46 | +public class TxThreadLockingTest extends AbstractBasicIntegrationTest { |
| 47 | + /** */ |
| 48 | + private static final long TIMEOUT = 10_000L; |
| 49 | + |
| 50 | + /** */ |
| 51 | + private static final int POOL_SIZE = 10; |
| 52 | + |
| 53 | + /** */ |
| 54 | + private static final int IN_BUFFER_SIZE = AbstractExecutionTest.IN_BUFFER_SIZE; |
| 55 | + |
| 56 | + /** */ |
| 57 | + private static final int MODIFY_BATCH_SIZE = AbstractExecutionTest.MODIFY_BATCH_SIZE; |
| 58 | + |
| 59 | + /** Use query blocking executor. */ |
| 60 | + @Parameterized.Parameter(0) |
| 61 | + public boolean qryBlockingExecutor; |
| 62 | + |
| 63 | + /** */ |
| 64 | + @Parameterized.Parameters(name = "qryBlockingExecutor={0}") |
| 65 | + public static Collection<?> parameters() { |
| 66 | + return List.of(false, true); |
| 67 | + } |
| 68 | + |
| 69 | + /** {@inheritDoc} */ |
| 70 | + @Override protected void beforeTestsStarted() throws Exception { |
| 71 | + // No-op. |
| 72 | + } |
| 73 | + |
| 74 | + /** {@inheritDoc} */ |
| 75 | + @Override protected void beforeTest() throws Exception { |
| 76 | + System.setProperty(IGNITE_CALCITE_USE_QUERY_BLOCKING_TASK_EXECUTOR, String.valueOf(qryBlockingExecutor)); |
| 77 | + |
| 78 | + startGrid(0); |
| 79 | + |
| 80 | + client = startClientGrid(); |
| 81 | + } |
| 82 | + |
| 83 | + /** {@inheritDoc} */ |
| 84 | + @Override protected void afterTest() throws Exception { |
| 85 | + stopAllGrids(); |
| 86 | + |
| 87 | + System.clearProperty(IGNITE_CALCITE_USE_QUERY_BLOCKING_TASK_EXECUTOR); |
| 88 | + } |
| 89 | + |
| 90 | + /** {@inheritDoc} */ |
| 91 | + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| 92 | + return super.getConfiguration(igniteInstanceName).setQueryThreadPoolSize(POOL_SIZE); |
| 93 | + } |
| 94 | + |
| 95 | + /** {@inheritDoc} */ |
| 96 | + @Override protected <K, V> CacheConfiguration<K, V> cacheConfiguration() { |
| 97 | + return super.<K, V>cacheConfiguration().setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); |
| 98 | + } |
| 99 | + |
| 100 | + /** */ |
| 101 | + @Test |
| 102 | + public void testThreadsLockingByDml() throws Exception { |
| 103 | + createAndPopulateTable(); |
| 104 | + |
| 105 | + CountDownLatch unlockLatch = new CountDownLatch(1); |
| 106 | + |
| 107 | + IgniteInternalFuture<?> lockKeyFut = lockKeyAndWaitForLatch(0, unlockLatch); |
| 108 | + |
| 109 | + IgniteInternalFuture<?> updFut = GridTestUtils.runMultiThreadedAsync(() -> { |
| 110 | + sql("UPDATE person SET salary = 1 WHERE id = 0"); |
| 111 | + }, POOL_SIZE + 1, "update-thread"); |
| 112 | + |
| 113 | + // Ensure update queries are blocked. |
| 114 | + assertFalse(GridTestUtils.waitForCondition(updFut::isDone, 200)); |
| 115 | + |
| 116 | + IgniteInternalFuture<?> selectFut = GridTestUtils.runMultiThreadedAsync(() -> { |
| 117 | + for (int i = 0; i < 100; i++) |
| 118 | + sql("SELECT * FROM person WHERE id = 1"); |
| 119 | + }, POOL_SIZE + 1, "select-thread"); |
| 120 | + |
| 121 | + // Ensure other queries are not blocked by update queries. |
| 122 | + selectFut.get(TIMEOUT, TimeUnit.MILLISECONDS); |
| 123 | + |
| 124 | + unlockLatch.countDown(); |
| 125 | + |
| 126 | + lockKeyFut.get(); |
| 127 | + |
| 128 | + // Ensure update queries are released. |
| 129 | + updFut.get(TIMEOUT, TimeUnit.MILLISECONDS); |
| 130 | + } |
| 131 | + |
| 132 | + /** */ |
| 133 | + @Test |
| 134 | + public void testDifferentBlockedBatchSize() throws Exception { |
| 135 | + assertTrue("Unexpected constants [MODIFY_BATCH_SIZE=" + MODIFY_BATCH_SIZE + |
| 136 | + ", IN_BUFFER_SIZE=" + IN_BUFFER_SIZE + ']', MODIFY_BATCH_SIZE < IN_BUFFER_SIZE); |
| 137 | + |
| 138 | + createAndPopulateTable(); |
| 139 | + |
| 140 | + IgniteCache<Integer, Employer> cache = client.cache(TABLE_NAME); |
| 141 | + |
| 142 | + for (int i = 0; i < IN_BUFFER_SIZE + 1; i++) |
| 143 | + cache.put(i, new Employer("Test" + i, (double)i)); |
| 144 | + |
| 145 | + for (int size : new int[] {MODIFY_BATCH_SIZE, MODIFY_BATCH_SIZE + 1, IN_BUFFER_SIZE, IN_BUFFER_SIZE + 1}) { |
| 146 | + log.info("Blocked batch size: " + size); |
| 147 | + |
| 148 | + CountDownLatch unlockLatch = new CountDownLatch(1); |
| 149 | + |
| 150 | + IgniteInternalFuture<?> lockKeyFut = lockKeyAndWaitForLatch(0, unlockLatch); |
| 151 | + |
| 152 | + IgniteInternalFuture<List<List<?>>> updFut = GridTestUtils.runAsync(() -> |
| 153 | + sql("UPDATE person SET salary = 1 WHERE id < ?", size)); |
| 154 | + |
| 155 | + // Ensure update query is blocked. |
| 156 | + assertFalse(GridTestUtils.waitForCondition(updFut::isDone, 200)); |
| 157 | + |
| 158 | + unlockLatch.countDown(); |
| 159 | + |
| 160 | + lockKeyFut.get(); |
| 161 | + |
| 162 | + List<List<?>> res = updFut.get(TIMEOUT, TimeUnit.MILLISECONDS); |
| 163 | + |
| 164 | + assertEquals((long)size, res.get(0).get(0)); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** */ |
| 169 | + @Test |
| 170 | + public void testErrorAfterAsyncWait() throws Exception { |
| 171 | + createAndPopulateTable(); |
| 172 | + |
| 173 | + CountDownLatch unlockLatch = new CountDownLatch(1); |
| 174 | + |
| 175 | + IgniteInternalFuture<?> lockKeyFut = lockKeyAndWaitForLatch(0, unlockLatch); |
| 176 | + |
| 177 | + IgniteInternalFuture<List<List<?>>> insFut = GridTestUtils.runAsync(() -> |
| 178 | + sql("INSERT INTO person (id, name, salary) VALUES (0, 'Test', 0)")); |
| 179 | + |
| 180 | + // Ensure insert query is blocked. |
| 181 | + assertFalse(GridTestUtils.waitForCondition(insFut::isDone, 200)); |
| 182 | + |
| 183 | + unlockLatch.countDown(); |
| 184 | + |
| 185 | + lockKeyFut.get(); |
| 186 | + |
| 187 | + // Ensure exception is propagated to query initiator after async batch execution finished. |
| 188 | + try { |
| 189 | + insFut.get(TIMEOUT, TimeUnit.MILLISECONDS); |
| 190 | + |
| 191 | + fail("Exception wasn't thrown"); |
| 192 | + } |
| 193 | + catch (IgniteCheckedException expected) { |
| 194 | + assertTrue(X.hasCause(expected, "Failed to INSERT some keys because they are already in cache", |
| 195 | + IgniteSQLException.class)); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** */ |
| 200 | + private IgniteInternalFuture<?> lockKeyAndWaitForLatch(int key, CountDownLatch unlockLatch) throws Exception { |
| 201 | + CountDownLatch lockLatch = new CountDownLatch(1); |
| 202 | + |
| 203 | + IgniteInternalFuture<?> lockKeyFut = GridTestUtils.runAsync(() -> { |
| 204 | + try (Transaction tx = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) { |
| 205 | + client.cache(TABLE_NAME).put(key, new Employer("Test", 0d)); |
| 206 | + |
| 207 | + lockLatch.countDown(); |
| 208 | + |
| 209 | + assertTrue(unlockLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)); |
| 210 | + |
| 211 | + tx.commit(); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + assertTrue(lockLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)); |
| 216 | + |
| 217 | + return lockKeyFut; |
| 218 | + } |
| 219 | +} |
0 commit comments