use of io.crate.action.sql.Session in project crate by crate.
the class NodeSettingsTest method testClusterName.
/**
* The default cluster name is "crate" if not set differently in crate settings
*/
@Test
public void testClusterName() throws Exception {
try (Session session = sqlOperations.newSystemSession()) {
SQLResponse response = SQLTransportExecutor.execute("select name from sys.cluster", new Object[0], session).get(SQLTransportExecutor.REQUEST_TIMEOUT.millis(), TimeUnit.MILLISECONDS);
assertThat(response.rows()[0][0], is("crate"));
}
}
use of io.crate.action.sql.Session in project crate by crate.
the class JobLogIntegrationTest method testJobLogWithEnabledAndDisabledStats.
@Test
// SET extra_float_digits = 3 gets added to the jobs_log
@UseJdbc(0)
public void testJobLogWithEnabledAndDisabledStats() throws Exception {
String setStmt = "set global transient stats.jobs_log_size=1";
execute(setStmt);
// We record the statements in the log **after** we notify the result receivers (see {@link JobsLogsUpdateListener usage).
// So it might happen that the "set global ..." statement execution is returned to this test but the recording
// in the log is done AFTER the execution of the below "select name from sys.cluster" statement (because async
// programming is evil like that). And then this test will fail and people will spend days and days to figure
// out what's going on.
// So let's just wait for the "set global ... " statement to be recorded here and then move on with our test.
assertBusy(() -> {
boolean setStmtFound = false;
for (JobsLogService jobsLogService : internalCluster().getDataNodeInstances(JobsLogService.class)) {
// each node must have received the new jobs_log_size setting change instruction
assertThat(jobsLogService.jobsLogSize(), is(1));
JobsLogs jobsLogs = jobsLogService.get();
Iterator<JobContextLog> iterator = jobsLogs.jobsLog().iterator();
if (iterator.hasNext()) {
if (iterator.next().statement().equalsIgnoreCase(setStmt)) {
setStmtFound = true;
}
}
}
// at least one node must have the set statement logged
assertThat(setStmtFound, is(true));
});
// only the latest queries are found in the log.
for (SQLOperations sqlOperations : internalCluster().getDataNodeInstances(SQLOperations.class)) {
Session session = sqlOperations.newSystemSession();
execute("select name from sys.cluster", null, session);
}
assertJobLogOnNodesHaveOnlyStatement("select name from sys.cluster");
for (SQLOperations sqlOperations : internalCluster().getDataNodeInstances(SQLOperations.class)) {
Session session = sqlOperations.newSystemSession();
execute("select id from sys.cluster", null, session);
}
assertJobLogOnNodesHaveOnlyStatement("select id from sys.cluster");
execute("set global transient stats.enabled = false");
for (JobsLogService jobsLogService : internalCluster().getDataNodeInstances(JobsLogService.class)) {
assertBusy(() -> assertThat(jobsLogService.isEnabled(), is(false)));
}
execute("select * from sys.jobs_log");
assertThat(response.rowCount(), is(0L));
}
use of io.crate.action.sql.Session in project crate by crate.
the class SQLTransportExecutor method execute.
public CompletableFuture<SQLResponse> execute(String stmt, @Nullable Object[] args) {
Session session = newSession();
CompletableFuture<SQLResponse> result = execute(stmt, args, session);
return result.whenComplete((res, err) -> session.close());
}
use of io.crate.action.sql.Session in project crate by crate.
the class SQLIntegrationTestCase method systemExecute.
/**
* Execute a SQL statement as system query on a specific node in the cluster
*
* @param stmt the SQL statement
* @param schema the schema that should be used for this statement
* schema is nullable, which means the default schema ("doc") is used
* @param node the name of the node on which the stmt is executed
* @return the SQL Response
*/
public SQLResponse systemExecute(String stmt, @Nullable String schema, String node) {
SQLOperations sqlOperations = internalCluster().getInstance(SQLOperations.class, node);
UserLookup userLookup;
try {
userLookup = internalCluster().getInstance(UserLookup.class, node);
} catch (ConfigurationException ignored) {
// If enterprise is not enabled there is no UserLookup instance bound in guice
userLookup = userName -> User.CRATE_USER;
}
try (Session session = sqlOperations.createSession(schema, userLookup.findUser("crate"))) {
response = sqlExecutor.exec(stmt, session);
}
return response;
}
Aggregations