Search in sources :

Example 1 with QueryHistory

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

the class JdbcThinPartitionAwarenessTransactionsSelfTest method checkNodesUsage.

/**
 * Utility method that:
 *   1. warms up an affinity cache;
 *   2. resets query history;
 *   3. executes given query multiple times;
 *   4. checks query history metrics in order to verify that not more than expected nodes were used.
 *
 * @param sql Sql query, either prepared statement or sql query should be used.
 * @param expRowsCnt Expected rows count within result.
 * @param dml Flag that signals whether we execute dml or not.
 * @throws Exception If failed.
 */
private void checkNodesUsage(String sql, int expRowsCnt, boolean dml) throws Exception {
    // Warm up an affinity cache.
    if (dml)
        stmt.executeUpdate(sql);
    else
        stmt.executeQuery(sql);
    // Reset query history.
    for (int i = 0; i < NODES_CNT; i++) {
        ((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().resetQueryHistoryMetrics();
    }
    // Execute query multiple times
    for (int i = 0; i < NODES_CNT * QUERY_EXECUTION_MULTIPLIER; i++) {
        ResultSet rs = null;
        int updatedRowsCnt = 0;
        if (dml)
            updatedRowsCnt = stmt.executeUpdate(sql);
        else
            rs = stmt.executeQuery(sql);
        if (dml) {
            assertEquals("Unexpected updated rows count: expected [" + expRowsCnt + "]," + " got [" + updatedRowsCnt + "]", expRowsCnt, updatedRowsCnt);
        } else {
            assert rs != null;
            int gotRowsCnt = 0;
            while (rs.next()) gotRowsCnt++;
            assertEquals("Unexpected rows count: expected [" + expRowsCnt + "], got [" + gotRowsCnt + "]", expRowsCnt, gotRowsCnt);
        }
    }
    // Check query history metrics in order to verify that not more than expected nodes were used.
    int nonEmptyMetricsCntr = 0;
    int qryExecutionsCntr = 0;
    for (int i = 0; i < NODES_CNT; i++) {
        Collection<QueryHistory> metrics = ((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().queryHistoryMetrics().values();
        if (!metrics.isEmpty()) {
            nonEmptyMetricsCntr++;
            qryExecutionsCntr += new ArrayList<>(metrics).get(0).executions();
        }
    }
    assertTrue("Unexpected amount of used nodes: expected [0 < nodesCnt <= 1" + "], got [" + nonEmptyMetricsCntr + "]", nonEmptyMetricsCntr == 1);
    assertEquals("Executions count doesn't match expeted value: expected [" + NODES_CNT * QUERY_EXECUTION_MULTIPLIER + "], got [" + qryExecutionsCntr + "]", NODES_CNT * QUERY_EXECUTION_MULTIPLIER, qryExecutionsCntr);
}
Also used : ResultSet(java.sql.ResultSet) QueryHistory(org.apache.ignite.internal.processors.query.QueryHistory) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)

Example 2 with QueryHistory

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

the class JdbcThinPartitionAwarenessSelfTest method checkNodesUsage.

/**
 * Utility method that:
 *  1. warms up an affinity cache;
 *  2. resets query history;
 *  3. executes given query multiple times;
 *  4. checks query history metrics in order to verify that not more than expected nodes were used.
 *
 * @param ps Prepared statement, either prepared statement or sql query should be used.
 * @param sql Sql query, either prepared statement or sql query should be used.
 * @param maxNodesUsedCnt Expected maximum number of used nodes.
 * @param expRowsCnt Expected rows count within result.
 * @param dml Flag that signals whether we execute dml or not.
 * @throws Exception If failed.
 */
private void checkNodesUsage(PreparedStatement ps, Statement stmt, String sql, int maxNodesUsedCnt, int expRowsCnt, boolean dml) throws Exception {
    // Warm up an affinity cache.
    if (ps != null)
        if (dml)
            ps.executeUpdate();
        else
            ps.executeQuery();
    else {
        if (dml)
            stmt.executeUpdate(sql);
        else
            stmt.executeQuery(sql);
    }
    // Reset query history.
    for (int i = 0; i < NODES_CNT; i++) {
        ((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().resetQueryHistoryMetrics();
    }
    // Execute query multiple times
    for (int i = 0; i < NODES_CNT * QUERY_EXECUTION_MULTIPLIER; i++) {
        ResultSet rs = null;
        int updatedRowsCnt = 0;
        if (ps != null)
            if (dml)
                updatedRowsCnt = ps.executeUpdate();
            else
                rs = ps.executeQuery();
        else {
            if (dml)
                updatedRowsCnt = stmt.executeUpdate(sql);
            else
                rs = stmt.executeQuery(sql);
        }
        if (dml) {
            assertEquals("Unexpected updated rows count: expected [" + expRowsCnt + "]," + " got [" + updatedRowsCnt + "]", expRowsCnt, updatedRowsCnt);
        } else {
            assert rs != null;
            int gotRowsCnt = 0;
            while (rs.next()) gotRowsCnt++;
            assertEquals("Unexpected rows count: expected [" + expRowsCnt + "], got [" + gotRowsCnt + "]", expRowsCnt, gotRowsCnt);
        }
    }
    // Check query history metrics in order to verify that not more than expected nodes were used.
    int nonEmptyMetricsCntr = 0;
    int qryExecutionsCntr = 0;
    for (int i = 0; i < NODES_CNT; i++) {
        Collection<QueryHistory> metrics = ((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().queryHistoryMetrics().values();
        if (!metrics.isEmpty()) {
            nonEmptyMetricsCntr++;
            qryExecutionsCntr += new ArrayList<>(metrics).get(0).executions();
        }
    }
    assertTrue("Unexpected amount of used nodes: expected [0 < nodesCnt <= " + maxNodesUsedCnt + "], got [" + nonEmptyMetricsCntr + "]", nonEmptyMetricsCntr > 0 && nonEmptyMetricsCntr <= maxNodesUsedCnt);
    assertEquals("Executions count doesn't match expected value: expected [" + NODES_CNT * QUERY_EXECUTION_MULTIPLIER + "], got [" + qryExecutionsCntr + "]", NODES_CNT * QUERY_EXECUTION_MULTIPLIER, qryExecutionsCntr);
}
Also used : ResultSet(java.sql.ResultSet) QueryHistory(org.apache.ignite.internal.processors.query.QueryHistory) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)

Aggregations

ResultSet (java.sql.ResultSet)2 QueryHistory (org.apache.ignite.internal.processors.query.QueryHistory)2 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)2