Search in sources :

Example 1 with GridCacheQueryType

use of org.apache.ignite.internal.processors.cache.query.GridCacheQueryType in project ignite by apache.

the class RemoveConstantsFromQueryTest method testConstantRemoved.

/**
 */
@Test
public void testConstantRemoved() throws Exception {
    IgniteEx ignite = startGrid(0);
    ignite.cluster().state(ACTIVE);
    cleanPerformanceStatisticsDir();
    startCollectStatistics();
    String john = "John Connor";
    String sarah = "Sarah Connor";
    // SQL query, Regexp to find, Constant that should be removed.
    List<GridTuple3<String, String, String>> qries = Arrays.asList(F.t("CREATE TABLE TST(id INTEGER PRIMARY KEY, name VARCHAR, age integer)", null, null), F.t("CREATE TABLE TST2(id INTEGER PRIMARY KEY, name VARCHAR, age integer)", null, null), F.t("INSERT INTO TST(id, name, age) VALUES(1, '" + john + "', 16)", "INSERT INTO .*TST.*VALUES.*", john), F.t("INSERT INTO TST SELECT id, name, age FROM TST2 WHERE name = 'John Connor'", "INSERT INTO .*TST.*SELECT.*FROM .*TST2 WHERE.*", john), F.t("UPDATE TST SET name = '" + sarah + "' WHERE id = 1", "UPDATE .*TST SET NAME.*WHERE ID.*", sarah), F.t("DELETE FROM TST WHERE name = '" + sarah + "'", "DELETE FROM .*TST WHERE NAME = ?", sarah), F.t("SELECT * FROM TST WHERE name = '" + sarah + "'", "SELECT .* FROM .*TST.*WHERE .*NAME = ?", sarah), F.t("SELECT * FROM TST WHERE name = SUBSTR('" + sarah + "', 0, 2)", "SELECT .* FROM .*TST.*WHERE .*NAME = ?", sarah.substring(0, 2)), F.t("SELECT * FROM TST GROUP BY id HAVING name = '" + john + "'", "SELECT .* FROM .*TST.*GROUP BY .*ID HAVING .*NAME = ?", john), F.t("SELECT * FROM TST GROUP BY id HAVING name = '" + sarah + "' UNION " + "SELECT * FROM TST GROUP BY id HAVING name = '" + john + "'", ".*SELECT .* FROM .*TST .* GROUP BY .*ID HAVING .*NAME = ?.* UNION " + ".*SELECT .* FROM .*TST .* GROUP BY .*ID HAVING .*NAME = ?.*", sarah), F.t("SELECT CONCAT(name, '" + sarah + "') FROM TST", "SELECT CONCAT(.*) FROM .*TST", sarah), F.t("ALTER TABLE TST ADD COLUMN department VARCHAR(200)", null, null), F.t("DROP TABLE TST", null, null), F.t("KILL SERVICE 'my_service'", null, null));
    AtomicReference<String> lastQryFromEvt = new AtomicReference<>();
    ignite.events().localListen(evt -> {
        assertTrue(evt instanceof SqlQueryExecutionEvent);
        lastQryFromEvt.set(((SqlQueryExecutionEvent) evt).text());
        return true;
    }, EventType.EVT_SQL_QUERY_EXECUTION);
    for (GridTuple3<String, String, String> qry : qries) {
        execSql(ignite, qry.get1());
        String expHist = qry.get2() == null ? qry.get1() : qry.get2();
        String qryFromEvt = lastQryFromEvt.get();
        List<List<?>> hist = execSql(ignite, "SELECT SQL FROM SYS.SQL_QUERIES_HISTORY WHERE SQL = ?", qryFromEvt);
        assertNotNull(hist);
        assertEquals(1, hist.size());
        String qryFromHist = hist.get(0).get(0).toString();
        if (qry.get2() != null) {
            Pattern ptrn = Pattern.compile(qry.get2());
            assertTrue(qry.get2() + " should match " + qryFromHist, ptrn.matcher(qryFromHist).find());
            assertTrue(qry.get2() + " should match " + qryFromEvt, ptrn.matcher(qryFromEvt).find());
        } else {
            assertEquals(qryFromHist, expHist);
            assertEquals(qryFromEvt, expHist);
        }
        if (qry.get3() != null) {
            assertFalse(qryFromHist.contains(qry.get3()));
            assertFalse(qryFromEvt.contains(qry.get3()));
        }
    }
    Set<String> qriesFromStats = new HashSet<>();
    stopCollectStatisticsAndRead(new AbstractPerformanceStatisticsTest.TestHandler() {

        @Override
        public void query(UUID nodeId, GridCacheQueryType type, String text, long id, long startTime, long duration, boolean success) {
            qriesFromStats.add(text);
        }
    });
    // `SELECT ... WHERE name  = 'X'` and `SELECT ... WHERE name = SUBSTR(...)` produces the same query text
    // and we have one extra query `SELECT sql FROM SYS.SQL_QUERIES_HISTORY WHERE sql = ?`
    // so the sizes of two collection should be equal.
    assertEquals(qries.size(), qriesFromStats.size());
    assertTrue(qriesFromStats.contains("SELECT SQL FROM SYS.SQL_QUERIES_HISTORY WHERE SQL = ?1"));
    for (GridTuple3<String, String, String> qry : qries) {
        boolean found = false;
        for (String qryFromStat : qriesFromStats) {
            if (qry.get2() != null) {
                if (!Pattern.compile(qry.get2()).matcher(qryFromStat).find())
                    continue;
            } else if (!qryFromStat.equals(qry.get1()))
                continue;
            found = qry.get3() == null || !qryFromStat.contains(qry.get3());
            if (found)
                break;
        }
        assertTrue(qry.get1() + " should be in statistics", found);
    }
}
Also used : Pattern(java.util.regex.Pattern) AbstractPerformanceStatisticsTest(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest) SqlQueryExecutionEvent(org.apache.ignite.events.SqlQueryExecutionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) GridTuple3(org.apache.ignite.internal.util.lang.GridTuple3) IgniteEx(org.apache.ignite.internal.IgniteEx) List(java.util.List) UUID(java.util.UUID) HashSet(java.util.HashSet) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test) AbstractPerformanceStatisticsTest(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest)

Example 2 with GridCacheQueryType

use of org.apache.ignite.internal.processors.cache.query.GridCacheQueryType in project ignite by apache.

the class PerformanceStatisticsQueryTest method runQueryAndCheck.

/**
 * Runs query and checks statistics.
 */
private void runQueryAndCheck(GridCacheQueryType expType, Query<?> qry, String expText, boolean hasLogicalReads, boolean hasPhysicalReads) throws Exception {
    long startTime = U.currentTimeMillis();
    cleanPerformanceStatisticsDir();
    startCollectStatistics();
    Collection<UUID> expNodeIds = new ArrayList<>();
    if (clientType == SERVER) {
        srv.cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        expNodeIds.add(srv.localNode().id());
    } else if (clientType == CLIENT) {
        client.cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        expNodeIds.add(client.localNode().id());
    } else if (clientType == THIN_CLIENT) {
        thinClient.cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        expNodeIds.addAll(F.nodeIds(client.cluster().forServers().nodes()));
    }
    Set<UUID> readsNodes = new HashSet<>();
    if (hasLogicalReads)
        srv.cluster().forServers().nodes().forEach(node -> readsNodes.add(node.id()));
    AtomicInteger queryCnt = new AtomicInteger();
    AtomicInteger readsCnt = new AtomicInteger();
    HashSet<Long> qryIds = new HashSet<>();
    stopCollectStatisticsAndRead(new TestHandler() {

        @Override
        public void query(UUID nodeId, GridCacheQueryType type, String text, long id, long queryStartTime, long duration, boolean success) {
            queryCnt.incrementAndGet();
            qryIds.add(id);
            assertTrue(expNodeIds.contains(nodeId));
            assertEquals(expType, type);
            assertEquals(expText, text);
            assertTrue(queryStartTime >= startTime);
            assertTrue(duration >= 0);
            assertTrue(success);
        }

        @Override
        public void queryReads(UUID nodeId, GridCacheQueryType type, UUID queryNodeId, long id, long logicalReads, long physicalReads) {
            readsCnt.incrementAndGet();
            qryIds.add(id);
            readsNodes.remove(nodeId);
            assertTrue(expNodeIds.contains(queryNodeId));
            assertEquals(expType, type);
            assertTrue(logicalReads > 0);
            assertTrue(hasPhysicalReads ? physicalReads > 0 : physicalReads == 0);
        }
    });
    assertEquals(1, queryCnt.get());
    assertTrue("Query reads expected on nodes: " + readsNodes, readsNodes.isEmpty());
    assertEquals(1, qryIds.size());
}
Also used : CLIENT(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest.ClientType.CLIENT) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) U(org.apache.ignite.internal.util.typedef.internal.U) SERVER(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest.ClientType.SERVER) IgniteEx(org.apache.ignite.internal.IgniteEx) THIN_CLIENT(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest.ClientType.THIN_CLIENT) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Config(org.apache.ignite.client.Config) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedList(java.util.LinkedList) Parameterized(org.junit.runners.Parameterized) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) F(org.apache.ignite.internal.util.typedef.F) Query(org.apache.ignite.cache.query.Query) INACTIVE(org.apache.ignite.cluster.ClusterState.INACTIVE) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) UUID(java.util.UUID) SCAN(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SCAN) IgniteCache(org.apache.ignite.IgniteCache) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) DFLT_SCHEMA(org.apache.ignite.internal.processors.query.QueryUtils.DFLT_SCHEMA) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SQL_FIELDS(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SQL_FIELDS) Collections(java.util.Collections) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) ArrayList(java.util.ArrayList) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 3 with GridCacheQueryType

use of org.apache.ignite.internal.processors.cache.query.GridCacheQueryType in project ignite by apache.

the class FilePerformanceStatisticsReader method deserialize.

/**
 * @param buf Buffer.
 * @param nodeId Node id.
 * @return {@code True} if operation deserialized. {@code False} if not enough bytes.
 */
private boolean deserialize(ByteBuffer buf, UUID nodeId) throws IOException {
    if (buf.remaining() < 1)
        return false;
    byte opTypeByte = buf.get();
    OperationType opType = OperationType.of(opTypeByte);
    if (cacheOperation(opType)) {
        if (buf.remaining() < cacheRecordSize())
            return false;
        int cacheId = buf.getInt();
        long startTime = buf.getLong();
        long duration = buf.getLong();
        for (PerformanceStatisticsHandler handler : curHnd) handler.cacheOperation(nodeId, opType, cacheId, startTime, duration);
        return true;
    } else if (transactionOperation(opType)) {
        if (buf.remaining() < 4)
            return false;
        int cacheIdsCnt = buf.getInt();
        if (buf.remaining() < transactionRecordSize(cacheIdsCnt) - 4)
            return false;
        GridIntList cacheIds = new GridIntList(cacheIdsCnt);
        for (int i = 0; i < cacheIdsCnt; i++) cacheIds.add(buf.getInt());
        long startTime = buf.getLong();
        long duration = buf.getLong();
        for (PerformanceStatisticsHandler handler : curHnd) handler.transaction(nodeId, cacheIds, startTime, duration, opType == TX_COMMIT);
        return true;
    } else if (opType == QUERY) {
        if (buf.remaining() < 1)
            return false;
        boolean cached = buf.get() != 0;
        String text;
        int hash = 0;
        if (cached) {
            if (buf.remaining() < 4)
                return false;
            hash = buf.getInt();
            text = knownStrs.get(hash);
            if (buf.remaining() < queryRecordSize(0, true) - 1 - 4)
                return false;
        } else {
            if (buf.remaining() < 4)
                return false;
            int textLen = buf.getInt();
            if (buf.remaining() < queryRecordSize(textLen, false) - 1 - 4)
                return false;
            text = readString(buf, textLen);
        }
        GridCacheQueryType queryType = GridCacheQueryType.fromOrdinal(buf.get());
        long id = buf.getLong();
        long startTime = buf.getLong();
        long duration = buf.getLong();
        boolean success = buf.get() != 0;
        if (text == null)
            forwardRead(hash);
        for (PerformanceStatisticsHandler handler : curHnd) handler.query(nodeId, queryType, text, id, startTime, duration, success);
        return true;
    } else if (opType == QUERY_READS) {
        if (buf.remaining() < queryReadsRecordSize())
            return false;
        GridCacheQueryType queryType = GridCacheQueryType.fromOrdinal(buf.get());
        UUID uuid = readUuid(buf);
        long id = buf.getLong();
        long logicalReads = buf.getLong();
        long physicalReads = buf.getLong();
        for (PerformanceStatisticsHandler handler : curHnd) handler.queryReads(nodeId, queryType, uuid, id, logicalReads, physicalReads);
        return true;
    } else if (opType == TASK) {
        if (buf.remaining() < 1)
            return false;
        boolean cached = buf.get() != 0;
        String taskName;
        int hash = 0;
        if (cached) {
            if (buf.remaining() < 4)
                return false;
            hash = buf.getInt();
            taskName = knownStrs.get(hash);
            if (buf.remaining() < taskRecordSize(0, true) - 1 - 4)
                return false;
        } else {
            if (buf.remaining() < 4)
                return false;
            int nameLen = buf.getInt();
            if (buf.remaining() < taskRecordSize(nameLen, false) - 1 - 4)
                return false;
            taskName = readString(buf, nameLen);
        }
        IgniteUuid sesId = readIgniteUuid(buf);
        long startTime = buf.getLong();
        long duration = buf.getLong();
        int affPartId = buf.getInt();
        if (taskName == null)
            forwardRead(hash);
        for (PerformanceStatisticsHandler handler : curHnd) handler.task(nodeId, sesId, taskName, startTime, duration, affPartId);
        return true;
    } else if (opType == JOB) {
        if (buf.remaining() < jobRecordSize())
            return false;
        IgniteUuid sesId = readIgniteUuid(buf);
        long queuedTime = buf.getLong();
        long startTime = buf.getLong();
        long duration = buf.getLong();
        boolean timedOut = buf.get() != 0;
        for (PerformanceStatisticsHandler handler : curHnd) handler.job(nodeId, sesId, queuedTime, startTime, duration, timedOut);
        return true;
    } else if (opType == CACHE_START) {
        if (buf.remaining() < 1)
            return false;
        boolean cached = buf.get() != 0;
        String cacheName;
        int hash = 0;
        if (cached) {
            if (buf.remaining() < 4)
                return false;
            hash = buf.getInt();
            cacheName = knownStrs.get(hash);
            if (buf.remaining() < cacheStartRecordSize(0, true) - 1 - 4)
                return false;
        } else {
            if (buf.remaining() < 4)
                return false;
            int nameLen = buf.getInt();
            if (buf.remaining() < cacheStartRecordSize(nameLen, false) - 1 - 4)
                return false;
            cacheName = readString(buf, nameLen);
        }
        int cacheId = buf.getInt();
        for (PerformanceStatisticsHandler handler : curHnd) handler.cacheStart(nodeId, cacheId, cacheName);
        return true;
    } else if (opType == CHECKPOINT) {
        if (buf.remaining() < checkpointRecordSize())
            return false;
        long beforeLockDuration = buf.getLong();
        long lockWaitDuration = buf.getLong();
        long listenersExecDuration = buf.getLong();
        long markDuration = buf.getLong();
        long lockHoldDuration = buf.getLong();
        long pagesWriteDuration = buf.getLong();
        long fsyncDuration = buf.getLong();
        long walCpRecordFsyncDuration = buf.getLong();
        long writeCheckpointEntryDuration = buf.getLong();
        long splitAndSortCpPagesDuration = buf.getLong();
        long totalDuration = buf.getLong();
        long cpStartTime = buf.getLong();
        int pagesSize = buf.getInt();
        int dataPagesWritten = buf.getInt();
        int cowPagesWritten = buf.getInt();
        for (PerformanceStatisticsHandler handler : curHnd) {
            handler.checkpoint(nodeId, beforeLockDuration, lockWaitDuration, listenersExecDuration, markDuration, lockHoldDuration, pagesWriteDuration, fsyncDuration, walCpRecordFsyncDuration, writeCheckpointEntryDuration, splitAndSortCpPagesDuration, totalDuration, cpStartTime, pagesSize, dataPagesWritten, cowPagesWritten);
        }
        return true;
    } else if (opType == PAGES_WRITE_THROTTLE) {
        if (buf.remaining() < pagesWriteThrottleRecordSize())
            return false;
        long endTime = buf.getLong();
        long duration = buf.getLong();
        for (PerformanceStatisticsHandler handler : curHnd) handler.pagesWriteThrottle(nodeId, endTime, duration);
        return true;
    } else
        throw new IgniteException("Unknown operation type id [typeId=" + opTypeByte + ']');
}
Also used : IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) GridIntList(org.apache.ignite.internal.util.GridIntList) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) UUID(java.util.UUID)

Example 4 with GridCacheQueryType

use of org.apache.ignite.internal.processors.cache.query.GridCacheQueryType in project ignite by apache.

the class PerformanceStatisticsQueryTest method testMultipleStatementsSql.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultipleStatementsSql() throws Exception {
    assumeFalse("Multiple statements queries are not supported by thin client.", clientType == THIN_CLIENT);
    long startTime = U.currentTimeMillis();
    LinkedList<String> expQrs = new LinkedList<>();
    expQrs.add("create table " + SQL_TABLE + " (id int primary key, val varchar)");
    expQrs.add("insert into " + SQL_TABLE + " (id, val) values (1, 'a')");
    expQrs.add("insert into " + SQL_TABLE + " (id, val) values (2, 'b'), (3, 'c')");
    LinkedList<String> qrsWithReads = new LinkedList<>();
    qrsWithReads.add("update " + SQL_TABLE + " set val = 'd' where id = 1");
    qrsWithReads.add("select * from " + SQL_TABLE);
    expQrs.addAll(qrsWithReads);
    startCollectStatistics();
    SqlFieldsQuery qry = new SqlFieldsQuery(F.concat(expQrs, ";"));
    IgniteEx loadNode = this.clientType == SERVER ? srv : client;
    List<FieldsQueryCursor<List<?>>> res = loadNode.context().query().querySqlFields(qry, true, false);
    assertEquals("Unexpected cursors count: " + res.size(), expQrs.size(), res.size());
    res.get(4).getAll();
    HashSet<Long> qryIds = new HashSet<>();
    stopCollectStatisticsAndRead(new TestHandler() {

        @Override
        public void query(UUID nodeId, GridCacheQueryType type, String text, long id, long queryStartTime, long duration, boolean success) {
            if (qrsWithReads.contains(text))
                qryIds.add(id);
            assertEquals(loadNode.localNode().id(), nodeId);
            assertEquals(SQL_FIELDS, type);
            assertTrue("Unexpected query: " + text, expQrs.remove(text));
            assertTrue(queryStartTime >= startTime);
            assertTrue(duration >= 0);
            assertTrue(success);
        }

        @Override
        public void queryReads(UUID nodeId, GridCacheQueryType type, UUID queryNodeId, long id, long logicalReads, long physicalReads) {
            qryIds.add(id);
            assertEquals(SQL_FIELDS, type);
            assertEquals(loadNode.localNode().id(), queryNodeId);
            assertTrue(logicalReads > 0);
            assertEquals(0, physicalReads);
        }
    });
    assertTrue("Queries was not handled: " + expQrs, expQrs.isEmpty());
    assertEquals("Unexpected IDs: " + qryIds, qrsWithReads.size(), qryIds.size());
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) LinkedList(java.util.LinkedList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

UUID (java.util.UUID)4 GridCacheQueryType (org.apache.ignite.internal.processors.cache.query.GridCacheQueryType)4 HashSet (java.util.HashSet)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Pattern (java.util.regex.Pattern)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteException (org.apache.ignite.IgniteException)1 Ignition (org.apache.ignite.Ignition)1 QueryEntity (org.apache.ignite.cache.QueryEntity)1