use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class ReprepareTestBase method testReprepare.
public void testReprepare(BiConsumer<ClassLoader, Integer> instanceInitializer, ReprepareTestConfiguration... configs) throws Throwable {
try (ICluster<IInvokableInstance> c = init(builder().withNodes(2).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).withInstanceInitializer(instanceInitializer).start())) {
ForceHostLoadBalancingPolicy lbp = new ForceHostLoadBalancingPolicy();
c.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
for (ReprepareTestConfiguration config : configs) {
// 1 has old behaviour
for (int firstContact : new int[] { 1, 2 }) {
try (com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").addContactPoint("127.0.0.2").withLoadBalancingPolicy(lbp).build();
Session session = cluster.connect()) {
lbp.setPrimary(firstContact);
final PreparedStatement select = session.prepare(withKeyspace("SELECT * FROM %s.tbl"));
session.execute(select.bind());
c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
lbp.setPrimary(firstContact == 1 ? 2 : 1);
if (config.withUse)
session.execute(withKeyspace("USE %s"));
// Re-preparing on the node
if (!config.skipBrokenBehaviours && firstContact == 1)
session.execute(select.bind());
c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
lbp.setPrimary(firstContact);
// Re-preparing on the node with old behaviour will break no matter where the statement was initially prepared
if (!config.skipBrokenBehaviours)
session.execute(select.bind());
c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
}
}
}
}
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class BinAuditLoggerTest method testSelectRoundTripQuery.
@Test
public void testSelectRoundTripQuery() throws Throwable {
createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 1, "Apache", "Cassandra");
execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 2, "trace", "test");
String cql = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + " WHERE id = ?";
Session session = sessionNet();
PreparedStatement pstmt = session.prepare(cql);
ResultSet rs = session.execute(pstmt.bind(1));
assertEquals(1, rs.all().size());
try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
ExcerptTailer tailer = queue.createTailer();
assertTrue(tailer.readDocument(wire -> {
assertEquals(0L, wire.read("version").int16());
assertEquals("audit", wire.read("type").text());
assertThat(wire.read("message").text(), containsString(AuditLogEntryType.PREPARE_STATEMENT.toString()));
}));
assertTrue(tailer.readDocument(wire -> {
assertEquals(0L, wire.read("version").int16());
assertEquals("audit", wire.read("type").text());
assertThat(wire.read("message").text(), containsString(AuditLogEntryType.SELECT.toString()));
}));
assertFalse(tailer.readDocument(wire -> {
}));
}
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class AuditLoggerTest method testCqlPrepareQueryError.
@Test
public void testCqlPrepareQueryError() {
createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
String cql = "INSERT INTO " + KEYSPACE + '.' + currentTable() + " (id, v1, v2) VALUES (?,?,?)";
try {
Session session = sessionNet();
PreparedStatement pstmt = session.prepare(cql);
AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cql, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
dropTable("DROP TABLE %s");
ResultSet rs = session.execute(pstmt.bind(1, "insert_audit", "test"));
Assert.fail("should not succeed");
} catch (NoHostAvailableException e) {
// nop
}
AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(logEntry, null);
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(logEntry, cql);
assertEquals(0, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class AuditLoggerTest method executeAndAssertWithPrepare.
private ResultSet executeAndAssertWithPrepare(String cql, AuditLogEntryType executeType, boolean isTableNull, Object... bindValues) throws Throwable {
Session session = sessionNet();
PreparedStatement pstmt = session.prepare(cql);
ResultSet rs = session.execute(pstmt.bind(bindValues));
AuditLogEntry logEntry1 = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cql, AuditLogEntryType.PREPARE_STATEMENT, logEntry1, isTableNull);
AuditLogEntry logEntry2 = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cql, executeType, logEntry2, isTableNull);
assertEquals(0, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
return rs;
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class AuditLoggerTest method testCqlBatch_MultipleTablesAuditing.
@Test
public void testCqlBatch_MultipleTablesAuditing() {
createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
String table1 = currentTable();
Session session = sessionNet();
BatchStatement batchStatement = new BatchStatement();
String cqlInsert1 = "INSERT INTO " + KEYSPACE + "." + table1 + " (id, v1, v2) VALUES (?, ?, ?)";
PreparedStatement prep = session.prepare(cqlInsert1);
AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cqlInsert1, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
String table2 = currentTable();
String cqlInsert2 = "INSERT INTO " + KEYSPACE + "." + table2 + " (id, v1, v2) VALUES (?, ?, ?)";
prep = session.prepare(cqlInsert2);
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cqlInsert2, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
String ks2 = currentKeyspace();
createTable(ks2, "CREATE TABLE %s (id int primary key, v1 text, v2 text)");
String table3 = currentTable();
String cqlInsert3 = "INSERT INTO " + ks2 + "." + table3 + " (id, v1, v2) VALUES (?, ?, ?)";
prep = session.prepare(cqlInsert3);
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cqlInsert3, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false, ks2);
batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
ResultSet rs = session.execute(batchStatement);
assertEquals(4, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cqlInsert1, table1, AuditLogEntryType.UPDATE, logEntry, false, KEYSPACE);
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cqlInsert2, table2, AuditLogEntryType.UPDATE, logEntry, false, KEYSPACE);
logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
assertLogEntry(cqlInsert3, table3, AuditLogEntryType.UPDATE, logEntry, false, ks2);
int size = rs.all().size();
assertEquals(0, size);
}
Aggregations