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());
}
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());
}
}
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());
}
}
};
}
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());
}
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());
}
Aggregations