Search in sources :

Example 71 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class KillQueryTest method testCancelQueryIfPartitionsCantBeReservedOnMapNodes.

/**
 * Check if query hangs (due to reducer spins retrying to reserve partitions but they can't be reserved), we still
 * able to cancel it. Used mocked indexing simulates 100% unability.
 */
@Test
public void testCancelQueryIfPartitionsCantBeReservedOnMapNodes() throws Exception {
    // Releases thread that kills query when map nodes receive query request. At least one map node received is ok.
    GridMessageListener qryStarted = (node, msg, plc) -> {
        if (msg instanceof GridH2QueryRequest)
            TestSQLFunctions.cancelLatch.countDown();
    };
    for (int i = 0; i < NODES_COUNT; i++) grid(i).context().io().addMessageListener(GridTopic.TOPIC_QUERY, qryStarted);
    // Suspends distributed queries on the map nodes.
    MockedIndexing.failReservations = true;
    try {
        IgniteInternalFuture cancelFut = cancel(1, asyncCancel);
        GridTestUtils.assertThrows(log, () -> {
            ignite.cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer where _val <> 42")).getAll();
            return null;
        }, CacheException.class, "The query was cancelled while executing.");
        cancelFut.get(CHECK_RESULT_TIMEOUT);
    } finally {
        for (int i = 0; i < NODES_COUNT; i++) grid(i).context().io().removeMessageListener(GridTopic.TOPIC_QUERY, qryStarted);
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Arrays(java.util.Arrays) Connection(java.sql.Connection) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) TimeoutException(java.util.concurrent.TimeoutException) IgniteEx(org.apache.ignite.internal.IgniteEx) PartitionReservation(org.apache.ignite.internal.processors.query.h2.twostep.PartitionReservation) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) After(org.junit.After) QueryEntity(org.apache.ignite.cache.QueryEntity) GridIoPolicy(org.apache.ignite.internal.managers.communication.GridIoPolicy) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) Parameterized(org.junit.runners.Parameterized) ReducePartitionMapper(org.apache.ignite.internal.processors.query.h2.twostep.ReducePartitionMapper) CyclicBarrier(java.util.concurrent.CyclicBarrier) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) Serializable(java.io.Serializable) GridSpinBusyLock(org.apache.ignite.internal.util.GridSpinBusyLock) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Arrays.stream(java.util.Arrays.stream) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) TcpDiscoveryIpFinder(org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder) IgniteUtils.resolveIgnitePath(org.apache.ignite.internal.util.IgniteUtils.resolveIgnitePath) IntStream(java.util.stream.IntStream) GridH2QueryRequest(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2QueryRequest) CustomMessageWrapper(org.apache.ignite.internal.managers.discovery.CustomMessageWrapper) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Affinity(org.apache.ignite.cache.affinity.Affinity) RunWith(org.junit.runner.RunWith) QuerySqlFunction(org.apache.ignite.cache.query.annotations.QuerySqlFunction) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) SQLException(java.sql.SQLException) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) CacheException(javax.cache.CacheException) ReducePartitionMapResult(org.apache.ignite.internal.processors.query.h2.twostep.ReducePartitionMapResult) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Before(org.junit.Before) F(org.apache.ignite.internal.util.typedef.F) SchemaProposeDiscoveryMessage(org.apache.ignite.internal.processors.query.schema.message.SchemaProposeDiscoveryMessage) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) Iterator(java.util.Iterator) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) TcpDiscoveryVmIpFinder(org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder) DataStreamerRequest(org.apache.ignite.internal.processors.datastreamer.DataStreamerRequest) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test) Field(java.lang.reflect.Field) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) GridReduceQueryExecutor(org.apache.ignite.internal.processors.query.h2.twostep.GridReduceQueryExecutor) TimeUnit(java.util.concurrent.TimeUnit) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty) GridTopic(org.apache.ignite.internal.GridTopic) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) PartitionReservationManager(org.apache.ignite.internal.processors.query.h2.twostep.PartitionReservationManager) Statement(java.sql.Statement) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) Assert(org.junit.Assert) Collections(java.util.Collections) QueryIndex(org.apache.ignite.cache.QueryIndex) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridH2QueryRequest(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2QueryRequest) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) Test(org.junit.Test)

Example 72 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class KillQueryTest method checkPartitions.

/**
 * Test if user specified partitions for query explicitly, such query is cancealble.
 *
 * @param partitions user specified partitions, could contain partitions that are mapped on one or both nodes.
 */
public void checkPartitions(int[] partitions) throws Exception {
    TestSQLFunctions.reset();
    IgniteInternalFuture cancelRes = cancel(1, asyncCancel);
    GridTestUtils.assertThrows(log, () -> {
        ignite.cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer where _key in " + "(select abs(_key) from Integer where awaitLatchCancelled() = 0) and shouldNotBeCalledInCaseOfCancellation()").setPartitions(partitions)).getAll();
        return null;
    }, CacheException.class, "The query was cancelled while executing.");
    // Ensures that there were no exceptions within async cancellation process.
    cancelRes.get(CHECK_RESULT_TIMEOUT);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 73 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class KillQueryTest method testCancelLocalLazyQueryNative.

/**
 * Check that local query can be canceled either using async or non-async method. Local query is performed using
 * cache.query() API with "local" property "true".
 */
@Test
public void testCancelLocalLazyQueryNative() throws Exception {
    IgniteInternalFuture cancelRes = cancel(1, asyncCancel);
    GridTestUtils.assertThrowsAnyCause(log, () -> {
        ignite.cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer where _key in " + "(select _key from Integer where awaitLatchCancelled() = 0) and shouldNotBeCalledMoreThan(128)").setLocal(true).setLazy(true)).getAll();
        return null;
    }, QueryCancelledException.class, "The query was cancelled while executing.");
    // Ensures that there were no exceptions within async cancellation process.
    cancelRes.get(CHECK_RESULT_TIMEOUT);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) Test(org.junit.Test)

Example 74 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class KillQueryTest method testKillAlreadyKilledQuery.

/**
 * Trying to kill already killed query. No exceptions expected.
 */
@Test
public void testKillAlreadyKilledQuery() throws Exception {
    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
    FieldsQueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery("select * from Integer where awaitLatchCancelled() = 0"));
    List<GridRunningQueryInfo> runningQueries = (List<GridRunningQueryInfo>) ignite.context().query().runningQueries(-1);
    GridRunningQueryInfo runQryInfo = runningQueries.get(0);
    SqlFieldsQuery killQry = createKillQuery(runQryInfo.globalQueryId(), asyncCancel);
    IgniteCache<Object, Object> reqCache = igniteForKillRequest.cache(DEFAULT_CACHE_NAME);
    IgniteInternalFuture killFut = cancel(1, asyncCancel);
    GridTestUtils.assertThrows(log, () -> cur.iterator().next(), QueryCancelledException.class, "The query was cancelled while executing");
    killFut.get(CHECK_RESULT_TIMEOUT);
    GridTestUtils.assertThrows(log, () -> reqCache.query(killQry), CacheException.class, "Query with provided ID doesn't exist");
    cur.close();
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) Test(org.junit.Test)

Example 75 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class KillQueryOnClientDisconnectTest method cancelAndCheckClientDisconnect.

/**
 * Cancels current query, actual cancel will wait <code>cancelLatch</code> to be releaseds.
 * Checked that Cancellation wasn't complete due to clint was disconnected.
 *
 * @return <code>IgniteInternalFuture</code> to check whether exception was thrown.
 */
protected IgniteInternalFuture cancelAndCheckClientDisconnect() {
    return GridTestUtils.runAsync(() -> {
        try {
            TestSQLFunctions.cancelLatch.await();
            List<GridRunningQueryInfo> runningQueries = (List<GridRunningQueryInfo>) serverNode().context().query().runningQueries(-1);
            assertEquals(1, runningQueries.size());
            IgniteInternalFuture fut = GridTestUtils.runAsync(() -> {
                clientNode().cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("KILL QUERY '" + runningQueries.get(0).globalQueryId() + "'"));
            });
            doSleep(500);
            TestSQLFunctions.reqLatch.countDown();
            GridTestUtils.assertThrows(log, () -> fut.get(TIMEOUT), IgniteCheckedException.class, "Failed to cancel query because local client node has been disconnected from the cluster");
        } catch (Exception e) {
            log.error("Unexpected exception.", e);
            Assert.fail("Unexpected exception");
        }
    });
}
Also used : List(java.util.List) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SQLException(java.sql.SQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Aggregations

SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)679 Test (org.junit.Test)388 List (java.util.List)373 Ignite (org.apache.ignite.Ignite)170 ArrayList (java.util.ArrayList)136 IgniteCache (org.apache.ignite.IgniteCache)123 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)115 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)94 IgniteEx (org.apache.ignite.internal.IgniteEx)90 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)83 Transaction (org.apache.ignite.transactions.Transaction)80 CacheException (javax.cache.CacheException)67 Random (java.util.Random)55 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)53 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)52 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)45 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)41 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)32 HashMap (java.util.HashMap)31 Cache (javax.cache.Cache)31