Search in sources :

Example 1 with GridRunningQueryInfo

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

the class CacheSqlQueryValueCopySelfTest method testRunningSqlFieldsQuery.

/**
     * Test collecting info about running.
     *
     * @throws Exception If failed.
     */
public void testRunningSqlFieldsQuery() throws Exception {
    IgniteInternalFuture<?> fut = runQueryAsync(new SqlFieldsQuery("select _val, sleep(1000) from Value limit 3"));
    Thread.sleep(500);
    GridQueryProcessor qryProc = grid(0).context().query();
    Collection<GridRunningQueryInfo> queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    fut.get();
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
    SqlFieldsQuery qry = new SqlFieldsQuery("select _val, sleep(1000) from Value limit 3");
    qry.setLocal(true);
    fut = runQueryAsync(qry);
    Thread.sleep(500);
    queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    fut.get();
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
}
Also used : GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 2 with GridRunningQueryInfo

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

the class IgniteH2Indexing method queryLocalSql.

/**
     * Executes regular query.
     *
     * @param schemaName Schema name.
     * @param qry Query.
     * @param alias Table alias.
     * @param params Query parameters.
     * @param type Query return type.
     * @param filter Cache name and key filter.
     * @return Queried rows.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
public <K, V> GridCloseableIterator<IgniteBiTuple<K, V>> queryLocalSql(String schemaName, final String qry, String alias, @Nullable final Collection<Object> params, String type, final IndexingQueryFilter filter, GridQueryCancel cancel) throws IgniteCheckedException {
    final H2TableDescriptor tbl = tableDescriptor(schemaName, type);
    if (tbl == null)
        throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND);
    String sql = generateQuery(qry, alias, tbl);
    Connection conn = connectionForThread(tbl.schemaName());
    H2Utils.setupConnection(conn, false, false);
    GridH2QueryContext.set(new GridH2QueryContext(nodeId, nodeId, 0, LOCAL).filter(filter).distributedJoinMode(OFF));
    GridRunningQueryInfo run = new GridRunningQueryInfo(qryIdGen.incrementAndGet(), qry, SQL, schemaName, U.currentTimeMillis(), null, true);
    runs.put(run.id(), run);
    try {
        ResultSet rs = executeSqlQueryWithTimer(conn, sql, params, true, 0, cancel);
        return new H2KeyValueIterator(rs);
    } finally {
        GridH2QueryContext.clearThreadLocal();
        runs.remove(run.id());
    }
}
Also used : IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Connection(java.sql.Connection) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo) ResultSet(java.sql.ResultSet) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)

Example 3 with GridRunningQueryInfo

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

the class IgniteH2Indexing method queryLocalSqlFields.

/**
     * Queries individual fields (generally used by JDBC drivers).
     *
     * @param schemaName Schema name.
     * @param qry Query.
     * @param params Query parameters.
     * @param filter Cache name and key filter.
     * @param enforceJoinOrder Enforce join order of tables in the query.
     * @param timeout Query timeout in milliseconds.
     * @param cancel Query cancel.
     * @return Query result.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
public GridQueryFieldsResult queryLocalSqlFields(final String schemaName, final String qry, @Nullable final Collection<Object> params, final IndexingQueryFilter filter, boolean enforceJoinOrder, final int timeout, final GridQueryCancel cancel) throws IgniteCheckedException {
    final Connection conn = connectionForSchema(schemaName);
    H2Utils.setupConnection(conn, false, enforceJoinOrder);
    final PreparedStatement stmt = preparedStatementWithParams(conn, qry, params, true);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    if (DmlStatementsProcessor.isDmlStatement(p)) {
        SqlFieldsQuery fldsQry = new SqlFieldsQuery(qry);
        if (params != null)
            fldsQry.setArgs(params.toArray());
        fldsQry.setEnforceJoinOrder(enforceJoinOrder);
        fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS);
        return dmlProc.updateSqlFieldsLocal(schemaName, stmt, fldsQry, filter, cancel);
    } else if (DdlStatementsProcessor.isDdlStatement(p))
        throw new IgniteSQLException("DDL statements are supported for the whole cluster only", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    List<GridQueryFieldMetadata> meta;
    try {
        meta = H2Utils.meta(stmt.getMetaData());
    } catch (SQLException e) {
        throw new IgniteCheckedException("Cannot prepare query metadata", e);
    }
    final GridH2QueryContext ctx = new GridH2QueryContext(nodeId, nodeId, 0, LOCAL).filter(filter).distributedJoinMode(OFF);
    return new GridQueryFieldsResultAdapter(meta, null) {

        @Override
        public GridCloseableIterator<List<?>> iterator() throws IgniteCheckedException {
            assert GridH2QueryContext.get() == null;
            GridH2QueryContext.set(ctx);
            GridRunningQueryInfo run = new GridRunningQueryInfo(qryIdGen.incrementAndGet(), qry, SQL_FIELDS, schemaName, U.currentTimeMillis(), cancel, true);
            runs.putIfAbsent(run.id(), run);
            try {
                ResultSet rs = executeSqlQueryWithTimer(stmt, conn, qry, params, timeout, cancel);
                return new H2FieldsIterator(rs);
            } finally {
                GridH2QueryContext.clearThreadLocal();
                runs.remove(run.id());
            }
        }
    };
}
Also used : GridQueryFieldsResultAdapter(org.apache.ignite.internal.processors.query.GridQueryFieldsResultAdapter) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Connection(java.sql.Connection) Prepared(org.h2.command.Prepared) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) JdbcSqlFieldsQuery(org.apache.ignite.internal.jdbc2.JdbcSqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)

Example 4 with GridRunningQueryInfo

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

the class CacheSqlQueryValueCopySelfTest method testCancelingSqlFieldsQuery.

/**
     * Test collecting info about running.
     *
     * @throws Exception If failed.
     */
public void testCancelingSqlFieldsQuery() throws Exception {
    runQueryAsync(new SqlFieldsQuery("select * from (select _val, sleep(100) from Value limit 50)"));
    Thread.sleep(500);
    final GridQueryProcessor qryProc = grid(0).context().query();
    Collection<GridRunningQueryInfo> queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    final Collection<GridRunningQueryInfo> finalQueries = queries;
    for (GridRunningQueryInfo query : finalQueries) qryProc.cancelQueries(Collections.singleton(query.id()));
    int n = 100;
    // Give cluster some time to cancel query and cleanup resources.
    while (n > 0) {
        Thread.sleep(100);
        queries = qryProc.runningQueries(0);
        if (queries.isEmpty())
            break;
        log.info(">>>> Wait for cancel: " + n);
        n--;
    }
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
}
Also used : GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 5 with GridRunningQueryInfo

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

the class CacheSqlQueryValueCopySelfTest method testRunningSqlQuery.

/**
     * Test collecting info about running.
     *
     * @throws Exception If failed.
     */
public void testRunningSqlQuery() throws Exception {
    IgniteInternalFuture<?> fut = runQueryAsync(new SqlQuery<Integer, Value>(Value.class, "id > sleep(100)"));
    Thread.sleep(500);
    GridQueryProcessor qryProc = grid(0).context().query();
    Collection<GridRunningQueryInfo> queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    fut.get();
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
    SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class, "id > sleep(100)");
    qry.setLocal(true);
    fut = runQueryAsync(qry);
    Thread.sleep(500);
    queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    fut.get();
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo)

Aggregations

GridRunningQueryInfo (org.apache.ignite.internal.processors.query.GridRunningQueryInfo)5 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 GridQueryProcessor (org.apache.ignite.internal.processors.query.GridQueryProcessor)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 GridH2QueryContext (org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)1 SqlQuery (org.apache.ignite.cache.query.SqlQuery)1 JdbcSqlFieldsQuery (org.apache.ignite.internal.jdbc2.JdbcSqlFieldsQuery)1 GridQueryFieldMetadata (org.apache.ignite.internal.processors.query.GridQueryFieldMetadata)1 GridQueryFieldsResultAdapter (org.apache.ignite.internal.processors.query.GridQueryFieldsResultAdapter)1 Prepared (org.h2.command.Prepared)1 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)1