Repository: ignite Updated Branches: refs/heads/master a549a742f -> 445d375ac
IGNITE-8915: Check moved to GridQueryProcessor. Check for SqlFieldsQuery added. - Fixes #4414. Signed-off-by: Nikolay Izhikov <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/445d375a Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/445d375a Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/445d375a Branch: refs/heads/master Commit: 445d375ac31c64e9f7896f578b96fca43095c6be Parents: a549a74 Author: Nikolay Izhikov <[email protected]> Authored: Thu Sep 6 13:05:39 2018 +0300 Committer: Nikolay Izhikov <[email protected]> Committed: Thu Sep 6 13:05:39 2018 +0300 ---------------------------------------------------------------------- .../processors/cache/IgniteCacheProxyImpl.java | 3 - .../processors/query/GridQueryProcessor.java | 24 +++++++- .../cache/IgniteCacheAbstractQuerySelfTest.java | 54 ++++++++++++++++- .../IgniteCacheReplicatedQuerySelfTest.java | 27 --------- .../index/H2DynamicIndexAbstractSelfTest.java | 6 ++ .../local/IgniteCacheLocalQuerySelfTest.java | 64 +++++++++++++++++++- 6 files changed, 144 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/445d375a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java index 69ea562..225fa81 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java @@ -760,9 +760,6 @@ public class IgniteCacheProxyImpl<K, V> extends AsyncSupportAdapter<IgniteCache< (qry instanceof SqlQuery || qry instanceof SqlFieldsQuery || qry instanceof TextQuery)) throw new CacheException("Failed to execute query. Add module 'ignite-indexing' to the classpath " + "of all Ignite nodes."); - - if (qry.isLocal() && (qry instanceof SqlQuery) && ctx.kernalContext().clientNode()) - throw new CacheException("Execution of local sql query on client node disallowed."); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/ignite/blob/445d375a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java index bd5d91c..eb3f2a7 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java @@ -2111,7 +2111,7 @@ public class GridQueryProcessor extends GridProcessorAdapter { final boolean failOnMultipleStmts) { checkxEnabled(); - validateSqlFieldsQuery(qry); + validateSqlFieldsQuery(qry, ctx, cctx); if (!ctx.state().publicApiActiveState(true)) { throw new IgniteException("Can not perform the operation because the cluster is inactive. Note, that " + @@ -2161,13 +2161,31 @@ public class GridQueryProcessor extends GridProcessorAdapter { * Validate SQL fields query. * * @param qry Query. + * @param ctx Kernal context. + * @param cctx Cache context. */ - private static void validateSqlFieldsQuery(SqlFieldsQuery qry) { + private static void validateSqlFieldsQuery(SqlFieldsQuery qry, GridKernalContext ctx, + @Nullable GridCacheContext<?, ?> cctx) { if (qry.isReplicatedOnly() && qry.getPartitions() != null) throw new CacheException("Partitions are not supported in replicated only mode."); if (qry.isDistributedJoins() && qry.getPartitions() != null) throw new CacheException("Using both partitions and distributed JOINs is not supported for the same query"); + + if (qry.isLocal() && ctx.clientNode() && (cctx == null || cctx.config().getCacheMode() != CacheMode.LOCAL)) + throw new CacheException("Execution of local SqlFieldsQuery on client node disallowed."); + } + + /** + * Validate SQL query. + * + * @param qry Query. + * @param ctx Kernal context. + * @param cctx Cache context. + */ + private static void validateSqlQuery(SqlQuery qry, GridKernalContext ctx, GridCacheContext<?, ?> cctx) { + if (qry.isLocal() && ctx.clientNode() && cctx.config().getCacheMode() != CacheMode.LOCAL) + throw new CacheException("Execution of local SqlQuery on client node disallowed."); } /** @@ -2238,6 +2256,8 @@ public class GridQueryProcessor extends GridProcessorAdapter { */ public <K, V> QueryCursor<Cache.Entry<K,V>> querySql(final GridCacheContext<?,?> cctx, final SqlQuery qry, boolean keepBinary) { + validateSqlQuery(qry, ctx, cctx); + if (qry.isReplicatedOnly() && qry.getPartitions() != null) throw new CacheException("Partitions are not supported in replicated only mode."); http://git-wip-us.apache.org/repos/asf/ignite/blob/445d375a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java index a845aaa..ac9de6f 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java @@ -64,6 +64,7 @@ import org.apache.ignite.cache.query.annotations.QueryTextField; import org.apache.ignite.cache.store.CacheStore; import org.apache.ignite.cache.store.CacheStoreAdapter; import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.configuration.NearCacheConfiguration; import org.apache.ignite.events.CacheQueryExecutedEvent; @@ -96,6 +97,7 @@ import static org.apache.ignite.events.EventType.EVT_CACHE_QUERY_EXECUTED; import static org.apache.ignite.events.EventType.EVT_CACHE_QUERY_OBJECT_READ; import static org.apache.ignite.internal.processors.cache.query.CacheQueryType.FULL_TEXT; import static org.apache.ignite.internal.processors.cache.query.CacheQueryType.SCAN; +import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause; import static org.junit.Assert.assertArrayEquals; /** @@ -142,9 +144,12 @@ public abstract class IgniteCacheAbstractQuerySelfTest extends GridCommonAbstrac c.setDiscoverySpi(new TcpDiscoverySpi().setForceServerMode(true).setIpFinder(ipFinder)); - if (igniteInstanceName.startsWith("client")) + if (igniteInstanceName.startsWith("client")) { c.setClientMode(true); + c.setDataStorageConfiguration(new DataStorageConfiguration()); + } + return c; } @@ -1774,6 +1779,53 @@ public abstract class IgniteCacheAbstractQuerySelfTest extends GridCommonAbstrac } /** + * @throws Exception If failed. + */ + public void testLocalSqlQueryFromClient() throws Exception { + try { + Ignite g = startGrid("client"); + + IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class); + + for (int i = 0; i < 10; i++) + c.put(i, i); + + SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key"); + + qry.setLocal(true); + + assertThrowsWithCause(() -> c.query(qry), CacheException.class); + } + finally { + stopGrid("client"); + } + } + + /** + * @throws Exception If failed. + */ + public void testLocalSqlFieldsQueryFromClient() throws Exception { + try { + Ignite g = startGrid("client"); + + IgniteCache<UUID, Person> c = jcache(g, UUID.class, Person.class); + + Person p = new Person("Jon", 1500); + + c.put(p.id(), p); + + SqlFieldsQuery qry = new SqlFieldsQuery("select count(*) from Person"); + + qry.setLocal(true); + + assertThrowsWithCause(() -> c.query(qry), CacheException.class); + } + finally { + stopGrid("client"); + } + } + + /** * */ private static class ArrayObject implements Serializable { http://git-wip-us.apache.org/repos/asf/ignite/blob/445d375a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/distributed/replicated/IgniteCacheReplicatedQuerySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/distributed/replicated/IgniteCacheReplicatedQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/distributed/replicated/IgniteCacheReplicatedQuerySelfTest.java index bd3dffd..13942c2 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/distributed/replicated/IgniteCacheReplicatedQuerySelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/distributed/replicated/IgniteCacheReplicatedQuerySelfTest.java @@ -28,7 +28,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CountDownLatch; import javax.cache.Cache; -import javax.cache.CacheException; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.IgniteException; @@ -53,7 +52,6 @@ import org.apache.ignite.transactions.Transaction; import static org.apache.ignite.cache.CacheMode.REPLICATED; import static org.apache.ignite.cache.CachePeekMode.ALL; import static org.apache.ignite.events.EventType.EVT_NODE_LEFT; -import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause; /** * Tests replicated query. @@ -163,31 +161,6 @@ public class IgniteCacheReplicatedQuerySelfTest extends IgniteCacheAbstractQuery } /** - * @throws Exception If failed. - */ - public void testClientsLocalQuery() throws Exception { - try { - Ignite g = startGrid("client"); - - IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class); - - for (int i = 0; i < 10; i++) - c.put(i, i); - - assertEquals(0, c.localSize()); - - SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key"); - - qry.setLocal(true); - - assertThrowsWithCause(() -> c.query(qry), CacheException.class); - } - finally { - stopGrid("client"); - } - } - - /** * JUnit. * * @throws Exception If failed. http://git-wip-us.apache.org/repos/asf/ignite/blob/445d375a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicIndexAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicIndexAbstractSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicIndexAbstractSelfTest.java index 9579b79..10ef56f 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicIndexAbstractSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/H2DynamicIndexAbstractSelfTest.java @@ -85,6 +85,9 @@ public abstract class H2DynamicIndexAbstractSelfTest extends AbstractSchemaSelfT // Test that local queries on all nodes use new index. for (int i = 0 ; i < 4; i++) { + if (ignite(i).configuration().isClientMode()) + continue; + List<List<?>> locRes = ignite(i).cache("cache").query(new SqlFieldsQuery("explain select \"id\" from " + "\"cache\".\"ValueClass\" where \"field1\" = 'A'").setLocal(true)).getAll(); @@ -155,6 +158,9 @@ public abstract class H2DynamicIndexAbstractSelfTest extends AbstractSchemaSelfT // Test that no local queries on all nodes use new index. for (int i = 0 ; i < 4; i++) { + if (ignite(i).configuration().isClientMode()) + continue; + List<List<?>> locRes = ignite(i).cache("cache").query(new SqlFieldsQuery("explain select \"id\" from " + "\"cache\".\"ValueClass\" where \"field1\" = 'A'").setLocal(true)).getAll(); http://git-wip-us.apache.org/repos/asf/ignite/blob/445d375a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/local/IgniteCacheLocalQuerySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/local/IgniteCacheLocalQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/local/IgniteCacheLocalQuerySelfTest.java index 2570bc8..2272f27 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/local/IgniteCacheLocalQuerySelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/local/IgniteCacheLocalQuerySelfTest.java @@ -19,9 +19,12 @@ package org.apache.ignite.internal.processors.cache.local; import java.util.Iterator; import java.util.List; +import java.util.UUID; import javax.cache.Cache; +import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.query.FieldsQueryCursor; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.SqlQuery; @@ -93,4 +96,63 @@ public class IgniteCacheLocalQuerySelfTest extends IgniteCacheAbstractQuerySelfT cache.destroy(); } } -} \ No newline at end of file + + /** {@inheritDoc} */ + @Override public void testLocalSqlQueryFromClient() throws Exception { + try { + Ignite g = startGrid("client"); + + IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class); + + for (int i = 0; i < 10; i++) + c.put(i, i); + + SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key"); + + qry.setLocal(true); + + try(QueryCursor<Cache.Entry<Integer, Integer>> qryCursor = c.query(qry)) { + assertNotNull(qryCursor); + + List<Cache.Entry<Integer, Integer>> res = qryCursor.getAll(); + + assertNotNull(res); + + assertEquals(5, res.size()); + } + } + finally { + stopGrid("client"); + } + } + + /** {@inheritDoc} */ + @Override public void testLocalSqlFieldsQueryFromClient() throws Exception { + try { + Ignite g = startGrid("client"); + + IgniteCache<UUID, Person> c = jcache(g, UUID.class, Person.class); + + Person p = new Person("Jon", 1500); + + c.put(p.id(), p); + + SqlFieldsQuery qry = new SqlFieldsQuery("select * from Person"); + + qry.setLocal(true); + + try(FieldsQueryCursor<List<?>> qryCursor = c.query(qry)) { + assertNotNull(qryCursor); + + List<List<?>> res = qryCursor.getAll(); + + assertNotNull(res); + + assertEquals(1, res.size()); + } + } + finally { + stopGrid("client"); + } + } +}
