use of org.apache.ignite.events.SqlQueryExecutionEvent 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);
}
}
use of org.apache.ignite.events.SqlQueryExecutionEvent in project ignite by apache.
the class IgniteH2Indexing method registerRunningQuery.
/**
* Register running query.
*
* @param qryDesc Query descriptor.
* @param qryParams Query parameters.
* @param cancel Query cancel state holder.
* @param stmnt Parsed statement.
* @return Id of registered query or {@code null} if query wasn't registered.
*/
private long registerRunningQuery(QueryDescriptor qryDesc, QueryParameters qryParams, GridQueryCancel cancel, @Nullable GridSqlStatement stmnt) {
String qry = QueryUtils.INCLUDE_SENSITIVE || stmnt == null ? qryDesc.sql() : sqlWithoutConst(stmnt);
long res = runningQryMgr.register(qry, GridCacheQueryType.SQL_FIELDS, qryDesc.schemaName(), qryDesc.local(), cancel, qryDesc.queryInitiatorId());
if (ctx.event().isRecordable(EVT_SQL_QUERY_EXECUTION)) {
ctx.event().record(new SqlQueryExecutionEvent(ctx.discovery().localNode(), GridCacheQueryType.SQL_FIELDS.name() + " query execution.", qry, qryParams.arguments(), ctx.security().enabled() ? ctx.security().securityContext().subject().id() : null));
}
return res;
}
use of org.apache.ignite.events.SqlQueryExecutionEvent in project ignite by apache.
the class UserQueriesTestBase method getConfiguration.
/**
* {@inheritDoc}
*/
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
IgnitePredicate<SqlQueryExecutionEvent> lsnr = evt -> {
assertNotNull(evt.text());
SQL_QRY_EXEC_EVT_CNTR.incrementAndGet();
return true;
};
int[] evts = new int[] { EVT_SQL_QUERY_EXECUTION };
cfg.setIncludeEventTypes(evts);
cfg.setLocalEventListeners(Collections.singletonMap(lsnr, evts));
return cfg;
}
use of org.apache.ignite.events.SqlQueryExecutionEvent in project ignite by apache.
the class IgniteCacheAbstractQuerySelfTest method doTestClientQueryExecutedEvents.
/**
*/
public void doTestClientQueryExecutedEvents(boolean inclSens) throws Exception {
CountDownLatch execLatch = new CountDownLatch(9);
IgnitePredicate<SqlQueryExecutionEvent> lsnr = evt -> {
assertNotNull(evt.text());
if (inclSens)
assertTrue(evt.toString().contains("args="));
else
assertFalse(evt.toString().contains("args="));
execLatch.countDown();
return true;
};
ignite().events().localListen(lsnr, EVT_SQL_QUERY_EXECUTION);
ClientConfiguration cc = new ClientConfiguration().setAddresses(Config.SERVER);
try (IgniteClient client = Ignition.startClient(cc)) {
client.query(new SqlFieldsQuery("create table TEST_TABLE(key int primary key, val int)")).getAll();
client.query(new SqlFieldsQuery("insert into TEST_TABLE values (?, ?)").setArgs(1, 1)).getAll();
client.query(new SqlFieldsQuery("update TEST_TABLE set val = ?2 where key = ?1").setArgs(1, 2)).getAll();
client.query(new SqlFieldsQuery("select * from TEST_TABLE")).getAll();
client.query(new SqlFieldsQuery("create index idx_1 on TEST_TABLE(key)")).getAll();
client.query(new SqlFieldsQuery("drop index idx_1")).getAll();
client.query(new SqlFieldsQuery("alter table TEST_TABLE add column val2 int")).getAll();
client.query(new SqlFieldsQuery("alter table TEST_TABLE drop val2")).getAll();
client.query(new SqlFieldsQuery("drop table TEST_TABLE")).getAll();
assert execLatch.await(3_000, MILLISECONDS);
} finally {
ignite().events().stopLocalListen(lsnr, EVT_SQL_QUERY_EXECUTION);
}
}
Aggregations