|
23 | 23 | import java.util.List; |
24 | 24 | import java.util.Set; |
25 | 25 | import java.util.UUID; |
| 26 | +import java.util.concurrent.Callable; |
26 | 27 | import org.apache.calcite.util.ImmutableBitSet; |
| 28 | +import org.apache.ignite.IgniteCheckedException; |
| 29 | +import org.apache.ignite.cache.CacheEntry; |
27 | 30 | import org.apache.ignite.cache.query.SqlFieldsQuery; |
28 | 31 | import org.apache.ignite.calcite.CalciteQueryEngineConfiguration; |
29 | 32 | import org.apache.ignite.configuration.IgniteConfiguration; |
30 | 33 | import org.apache.ignite.configuration.SqlConfiguration; |
31 | 34 | import org.apache.ignite.configuration.TransactionConfiguration; |
32 | 35 | import org.apache.ignite.internal.IgniteEx; |
| 36 | +import org.apache.ignite.internal.IgniteInternalFuture; |
33 | 37 | import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; |
| 38 | +import org.apache.ignite.internal.processors.cache.CacheEntryImplEx; |
| 39 | +import org.apache.ignite.internal.processors.cache.IgniteCacheProxy; |
34 | 40 | import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; |
35 | 41 | import org.apache.ignite.internal.processors.query.IgniteSQLException; |
36 | 42 | import org.apache.ignite.internal.processors.query.QueryUtils; |
|
49 | 55 | import org.apache.ignite.internal.processors.query.calcite.schema.IgniteIndex; |
50 | 56 | import org.apache.ignite.internal.processors.query.calcite.schema.TechnicalColumns; |
51 | 57 | import org.apache.ignite.internal.processors.query.calcite.util.Commons; |
| 58 | +import org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException; |
52 | 59 | import org.apache.ignite.testframework.GridTestUtils; |
53 | 60 | import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
54 | 61 | import org.apache.ignite.transactions.Transaction; |
@@ -95,6 +102,72 @@ public void testTableScanReturnsTechnicalColumns() throws Exception { |
95 | 102 | assertTechnicalColumns(tbl.scan(scanCtx.ectx, scanCtx.grp, requiredColumns(tbl)), tbl); |
96 | 103 | } |
97 | 104 |
|
| 105 | + /** */ |
| 106 | + @Test |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + public void testScannedTechnicalColumnsCanLockTxEntries() throws Exception { |
| 109 | + createAndPopulatePersonTable(); |
| 110 | + |
| 111 | + IgniteCacheTable tbl = personTable(); |
| 112 | + ScanContext scanCtx = scanContext(tbl); |
| 113 | + List<Object[]> rows = materialize(tbl.scan(scanCtx.ectx, scanCtx.grp, lockRequiredColumns(tbl))); |
| 114 | + List<CacheEntry<Object, Object>> entries = new ArrayList<>(); |
| 115 | + Integer expSrc = tbl.descriptor().cacheInfo().cacheId(); |
| 116 | + |
| 117 | + assertEquals(30, rows.size()); |
| 118 | + |
| 119 | + for (Object[] row : rows) { |
| 120 | + assertEquals(4, row.length); |
| 121 | + assertTrue("Unexpected _VER value [val=" + row[2] + ", cls=" + |
| 122 | + (row[2] == null ? null : row[2].getClass()) + ']', row[2] instanceof GridCacheVersion); |
| 123 | + assertEquals(expSrc, row[3]); |
| 124 | + |
| 125 | + entries.add(new CacheEntryImplEx<>(row[0], row[1], (GridCacheVersion)row[2])); |
| 126 | + } |
| 127 | + |
| 128 | + try (Transaction tx = node.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) { |
| 129 | + assertTrue(node.cache(tbl.descriptor().cacheInfo().name()).unwrap(IgniteCacheProxy.class) |
| 130 | + .internalProxy().lockTxEntries(entries, 5_000)); |
| 131 | + |
| 132 | + checkInaccessInOtherTx(); |
| 133 | + |
| 134 | + sql("UPDATE Person SET age = 42 WHERE id = 2"); |
| 135 | + |
| 136 | + tx.commit(); |
| 137 | + } |
| 138 | + |
| 139 | + List<List<?>> rowsAfterUpdate = sql("SELECT id, name, age FROM Person WHERE id = 2"); |
| 140 | + |
| 141 | + assertEquals(1, rowsAfterUpdate.size()); |
| 142 | + assertEquals(personName(2), rowsAfterUpdate.get(0).get(1)); |
| 143 | + assertEquals(42, rowsAfterUpdate.get(0).get(2)); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Checks that another transaction cannot access the cache. |
| 148 | + * |
| 149 | + * @throws IgniteCheckedException If failed. |
| 150 | + */ |
| 151 | + private void checkInaccessInOtherTx() throws IgniteCheckedException { |
| 152 | + IgniteInternalFuture<Void> accessFut = GridTestUtils.runAsync(new Callable<Void>() { |
| 153 | + @Override public Void call() { |
| 154 | + try (Transaction tx = node.transactions().txStart(PESSIMISTIC, READ_COMMITTED, 500, 1)) { |
| 155 | + sql("UPDATE Person SET name = 'Charley' WHERE id = 2"); |
| 156 | + |
| 157 | + tx.commit(); |
| 158 | + } |
| 159 | + |
| 160 | + return null; |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + GridTestUtils.assertThrowsWithCause(new Callable<Object>() { |
| 165 | + @Override public Object call() throws Exception { |
| 166 | + return accessFut.get(10_000); |
| 167 | + } |
| 168 | + }, IgniteTxTimeoutCheckedException.class); |
| 169 | + } |
| 170 | + |
98 | 171 | /** */ |
99 | 172 | @Test |
100 | 173 | public void testIndexScanReturnsTechnicalColumns() throws Exception { |
@@ -132,6 +205,22 @@ public void testTechnicalColumnsAreHiddenFromSql() throws Exception { |
132 | 205 | assertTechnicalColumnAccessForbidden("SELECT CASE WHEN _ver IS NOT NULL THEN 1 ELSE 0 END FROM Person"); |
133 | 206 | assertTechnicalColumnAccessForbidden("SELECT CAST(_ver AS VARCHAR) FROM Person"); |
134 | 207 | assertTechnicalColumnAccessForbidden("SELECT id FROM Person WHERE (SELECT _ver FROM Person WHERE id = 1) IS NOT NULL"); |
| 208 | + |
| 209 | + // MERGE: technical columns must be forbidden in all clause positions. |
| 210 | + assertTechnicalColumnAccessForbidden( |
| 211 | + "MERGE INTO Person " + |
| 212 | + "USING (SELECT id, _ver FROM Person) AS src ON (Person.id = src.id) " + |
| 213 | + "WHEN NOT MATCHED THEN INSERT (id, name, age) VALUES (src.id, 'x', 1)"); |
| 214 | + |
| 215 | + assertTechnicalColumnAccessForbidden( |
| 216 | + "MERGE INTO Person " + |
| 217 | + "USING (SELECT 100 AS id) AS src ON (Person._ver IS NOT NULL AND Person.id = src.id) " + |
| 218 | + "WHEN NOT MATCHED THEN INSERT (id, name, age) VALUES (src.id, 'x', 1)"); |
| 219 | + |
| 220 | + assertTechnicalColumnAccessForbidden( |
| 221 | + "MERGE INTO Person " + |
| 222 | + "USING (SELECT 1 AS id) AS src ON (Person.id = src.id) " + |
| 223 | + "WHEN MATCHED THEN UPDATE SET name = CAST(Person._ver AS VARCHAR)"); |
135 | 224 | } |
136 | 225 |
|
137 | 226 | /** */ |
@@ -202,6 +291,16 @@ private ImmutableBitSet requiredColumns(IgniteCacheTable tbl) { |
202 | 291 | ); |
203 | 292 | } |
204 | 293 |
|
| 294 | + /** */ |
| 295 | + private ImmutableBitSet lockRequiredColumns(IgniteCacheTable tbl) { |
| 296 | + return ImmutableBitSet.of( |
| 297 | + columnIndex(tbl, QueryUtils.KEY_FIELD_NAME), |
| 298 | + columnIndex(tbl, QueryUtils.VAL_FIELD_NAME), |
| 299 | + columnIndex(tbl, TechnicalColumns.VER_FIELD_NAME), |
| 300 | + columnIndex(tbl, TechnicalColumns.SRC_FIELD_NAME) |
| 301 | + ); |
| 302 | + } |
| 303 | + |
205 | 304 | /** */ |
206 | 305 | private int columnIndex(IgniteCacheTable tbl, String name) { |
207 | 306 | ColumnDescriptor desc = tbl.descriptor().columnDescriptor(name); |
|
0 commit comments