use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class PreparedStatementsTest method testPrepareWithLWT.
private void testPrepareWithLWT(ProtocolVersion version) throws Throwable {
Session session = sessionNet(version);
session.execute("USE " + keyspace());
createTable("CREATE TABLE %s (pk int, v1 int, v2 int, PRIMARY KEY (pk))");
PreparedStatement prepared1 = session.prepare(String.format("UPDATE %s SET v1 = ?, v2 = ? WHERE pk = 1 IF v1 = ?", currentTable()));
PreparedStatement prepared2 = session.prepare(String.format("INSERT INTO %s (pk, v1, v2) VALUES (?, 200, 300) IF NOT EXISTS", currentTable()));
execute("INSERT INTO %s (pk, v1, v2) VALUES (1,1,1)");
execute("INSERT INTO %s (pk, v1, v2) VALUES (2,2,2)");
ResultSet rs;
rs = session.execute(prepared1.bind(10, 20, 1));
assertRowsNet(rs, row(true));
assertEquals(rs.getColumnDefinitions().size(), 1);
rs = session.execute(prepared1.bind(100, 200, 1));
assertRowsNet(rs, row(false, 10));
assertEquals(rs.getColumnDefinitions().size(), 2);
rs = session.execute(prepared1.bind(30, 40, 10));
assertRowsNet(rs, row(true));
assertEquals(rs.getColumnDefinitions().size(), 1);
// Try executing the same message once again
rs = session.execute(prepared1.bind(100, 200, 1));
assertRowsNet(rs, row(false, 30));
assertEquals(rs.getColumnDefinitions().size(), 2);
rs = session.execute(prepared2.bind(1));
assertRowsNet(rs, row(false, 1, 30, 40));
assertEquals(rs.getColumnDefinitions().size(), 4);
alterTable("ALTER TABLE %s ADD v3 int;");
rs = session.execute(prepared2.bind(1));
assertRowsNet(rs, row(false, 1, 30, 40, null));
assertEquals(rs.getColumnDefinitions().size(), 5);
rs = session.execute(prepared2.bind(20));
assertRowsNet(rs, row(true));
assertEquals(rs.getColumnDefinitions().size(), 1);
rs = session.execute(prepared2.bind(20));
assertRowsNet(rs, row(false, 20, 200, 300, null));
assertEquals(rs.getColumnDefinitions().size(), 5);
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class PreparedStatementsTest method testPrepareWithBatchLWT.
private void testPrepareWithBatchLWT(ProtocolVersion version) throws Throwable {
Session session = sessionNet(version);
session.execute("USE " + keyspace());
createTable("CREATE TABLE %s (pk int, v1 int, v2 int, PRIMARY KEY (pk))");
PreparedStatement prepared1 = session.prepare("BEGIN BATCH " + "UPDATE " + currentTable() + " SET v1 = ? WHERE pk = 1 IF v1 = ?;" + "UPDATE " + currentTable() + " SET v2 = ? WHERE pk = 1 IF v2 = ?;" + "APPLY BATCH;");
PreparedStatement prepared2 = session.prepare("BEGIN BATCH " + "INSERT INTO " + currentTable() + " (pk, v1, v2) VALUES (1, 200, 300) IF NOT EXISTS;" + "APPLY BATCH");
execute("INSERT INTO %s (pk, v1, v2) VALUES (1,1,1)");
execute("INSERT INTO %s (pk, v1, v2) VALUES (2,2,2)");
com.datastax.driver.core.ResultSet rs;
rs = session.execute(prepared1.bind(10, 1, 20, 1));
assertRowsNet(rs, row(true));
assertEquals(rs.getColumnDefinitions().size(), 1);
rs = session.execute(prepared1.bind(100, 1, 200, 1));
assertRowsNet(rs, row(false, 1, 10, 20));
assertEquals(rs.getColumnDefinitions().size(), 4);
// Try executing the same message once again
rs = session.execute(prepared1.bind(100, 1, 200, 1));
assertRowsNet(rs, row(false, 1, 10, 20));
assertEquals(rs.getColumnDefinitions().size(), 4);
rs = session.execute(prepared2.bind());
assertRowsNet(rs, row(false, 1, 10, 20));
assertEquals(rs.getColumnDefinitions().size(), 4);
alterTable("ALTER TABLE %s ADD v3 int;");
rs = session.execute(prepared2.bind());
assertRowsNet(rs, row(false, 1, 10, 20, null));
assertEquals(rs.getColumnDefinitions().size(), 5);
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class PreparedStatementsTest method testStatementRePreparationOnReconnect.
@Test
public void testStatementRePreparationOnReconnect() {
Session session = sessionNet(ProtocolVersion.V5);
session.execute("USE " + keyspace());
session.execute(dropKsStatement);
session.execute(createKsStatement);
createTable("CREATE TABLE %s (id int PRIMARY KEY, cid int, val text);");
String insertCQL = "INSERT INTO " + currentTable() + " (id, cid, val) VALUES (?, ?, ?)";
String selectCQL = "Select * from " + currentTable() + " where id = ?";
PreparedStatement preparedInsert = session.prepare(insertCQL);
PreparedStatement preparedSelect = session.prepare(selectCQL);
session.execute(preparedInsert.bind(1, 1, "value"));
assertEquals(1, session.execute(preparedSelect.bind(1)).all().size());
try (Cluster newCluster = Cluster.builder().addContactPoints(nativeAddr).withClusterName("Test Cluster").withPort(nativePort).withoutJMXReporting().allowBetaProtocolVersion().build()) {
try (Session newSession = newCluster.connect()) {
newSession.execute("USE " + keyspace());
preparedInsert = newSession.prepare(insertCQL);
preparedSelect = newSession.prepare(selectCQL);
newSession.execute(preparedInsert.bind(1, 1, "value"));
assertEquals(1, newSession.execute(preparedSelect.bind(1)).all().size());
}
}
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class TraceCqlTest method testCqlStatementTracing.
@Test
public void testCqlStatementTracing() throws Throwable {
requireNetwork();
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");
try (Session session = sessionNet()) {
String cql = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + " WHERE id = ?";
PreparedStatement pstmt = session.prepare(cql).enableTracing();
QueryTrace trace = session.execute(pstmt.bind(1)).getExecutionInfo().getQueryTrace();
assertEquals(cql, trace.getParameters().get("query"));
assertEquals("1", trace.getParameters().get("bound_var_0_id"));
String cql2 = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + " WHERE id IN (?, ?, ?)";
pstmt = session.prepare(cql2).enableTracing();
trace = session.execute(pstmt.bind(19, 15, 16)).getExecutionInfo().getQueryTrace();
assertEquals(cql2, trace.getParameters().get("query"));
assertEquals("19", trace.getParameters().get("bound_var_0_id"));
assertEquals("15", trace.getParameters().get("bound_var_1_id"));
assertEquals("16", trace.getParameters().get("bound_var_2_id"));
// some more complex tests for tables with map and tuple data types and long bound values
createTable("CREATE TABLE %s (id int primary key, v1 text, v2 tuple<int, text, float>, v3 map<int, text>)");
execute("INSERT INTO %s (id, v1, v2, v3) values (?, ?, ?, ?)", 12, "mahdix", tuple(3, "bar", 2.1f), map(1290, "birthday", 39, "anniversary"));
execute("INSERT INTO %s (id, v1, v2, v3) values (?, ?, ?, ?)", 274, "CassandraRocks", tuple(9, "foo", 3.14f), map(9181, "statement", 716, "public speech"));
cql = "SELECT id, v1, v2, v3 FROM " + KEYSPACE + '.' + currentTable() + " WHERE v2 = ? ALLOW FILTERING";
pstmt = session.prepare(cql).enableTracing();
TupleType tt = TupleType.of(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE, DataType.cint(), DataType.text(), DataType.cfloat());
TupleValue value = tt.newValue();
value.setInt(0, 3);
value.setString(1, "bar");
value.setFloat(2, 2.1f);
trace = session.execute(pstmt.bind(value)).getExecutionInfo().getQueryTrace();
assertEquals(cql, trace.getParameters().get("query"));
assertEquals("(3, 'bar', 2.1)", trace.getParameters().get("bound_var_0_v2"));
cql2 = "SELECT id, v1, v2, v3 FROM " + KEYSPACE + '.' + currentTable() + " WHERE v3 CONTAINS KEY ? ALLOW FILTERING";
pstmt = session.prepare(cql2).enableTracing();
trace = session.execute(pstmt.bind(9181)).getExecutionInfo().getQueryTrace();
assertEquals(cql2, trace.getParameters().get("query"));
assertEquals("9181", trace.getParameters().get("bound_var_0_key(v3)"));
String boundValue = "Indulgence announcing uncommonly met she continuing two unpleasing terminated. Now " + "busy say down the shed eyes roof paid her. Of shameless collected suspicion existence " + "in. Share walls stuff think but the arise guest. Course suffer to do he sussex it " + "window advice. Yet matter enable misery end extent common men should. Her indulgence " + "but assistance favourable cultivated everything collecting." + "On projection apartments unsatiable so if he entreaties appearance. Rose you wife " + "how set lady half wish. Hard sing an in true felt. Welcomed stronger if steepest " + "ecstatic an suitable finished of oh. Entered at excited at forming between so " + "produce. Chicken unknown besides attacks gay compact out you. Continuing no " + "simplicity no favourable on reasonably melancholy estimating. Own hence views two " + "ask right whole ten seems. What near kept met call old west dine. Our announcing " + "sufficient why pianoforte. Full age foo set feel her told. Tastes giving in passed" + "direct me valley as supply. End great stood boy noisy often way taken short. Rent the " + "size our more door. Years no place abode in \uFEFFno child my. Man pianoforte too " + "solicitude friendship devonshire ten ask. Course sooner its silent but formal she " + "led. Extensive he assurance extremity at breakfast. Dear sure ye sold fine sell on. " + "Projection at up connection literature insensible motionless projecting." + "Nor hence hoped her after other known defer his. For county now sister engage had " + "season better had waited. Occasional mrs interested far expression acceptance. Day " + "either mrs talent pulled men rather regret admire but. Life ye sake it shed. Five " + "lady he cold in meet up. Service get met adapted matters offence for. Principles man " + "any insipidity age you simplicity understood. Do offering pleasure no ecstatic " + "whatever on mr directly. ";
String cql3 = "SELECT id, v1, v2, v3 FROM " + KEYSPACE + '.' + currentTable() + " WHERE v3 CONTAINS ? ALLOW FILTERING";
pstmt = session.prepare(cql3).enableTracing();
trace = session.execute(pstmt.bind(boundValue)).getExecutionInfo().getQueryTrace();
assertEquals(cql3, trace.getParameters().get("query"));
// when tracing is done, this boundValue will be surrounded by single quote, and first 1000 characters
// will be filtered. Here we take into account single quotes by adding them to the expected output
assertEquals("'" + boundValue.substring(0, 999) + "...'", trace.getParameters().get("bound_var_0_value(v3)"));
}
}
use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.
the class MixedModeFuzzTest method mixedModeFuzzTest.
@Test
public void mixedModeFuzzTest() throws Throwable {
try (ICluster<IInvokableInstance> c = builder().withNodes(2).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).withInstanceInitializer(PrepareBehaviour::oldNewBehaviour).start()) {
// Long string to make us invalidate caches occasionally
String veryLongString = "very";
for (int i = 0; i < 2; i++) veryLongString += veryLongString;
final String qualified = "SELECT pk as " + veryLongString + "%d, ck as " + veryLongString + "%d FROM ks%d.tbl";
final String unqualified = "SELECT pk as " + veryLongString + "%d, ck as " + veryLongString + "%d FROM tbl";
int KEYSPACES = 3;
final int STATEMENTS_PER_KS = 2;
for (int i = 0; i < KEYSPACES; i++) {
c.schemaChange(withKeyspace("CREATE KEYSPACE ks" + i + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};"));
c.schemaChange(withKeyspace("CREATE TABLE ks" + i + ".tbl (pk int, ck int, PRIMARY KEY (pk, ck));"));
for (int j = 0; j < i; j++) c.coordinator(1).execute("INSERT INTO ks" + i + ".tbl (pk, ck) VALUES (?, ?)", ConsistencyLevel.ALL, 1, j);
}
List<Thread> threads = new ArrayList<>();
AtomicBoolean interrupt = new AtomicBoolean(false);
AtomicReference<Throwable> thrown = new AtomicReference<>();
int INFREQUENT_ACTION_COEF = 100;
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(20);
for (int i = 0; i < 3; i++) {
int seed = i;
threads.add(new Thread(() -> {
com.datastax.driver.core.Cluster cluster = null;
Map<String, Session> sessions = new HashMap<>();
try {
AtomicBoolean nodeWithFix = new AtomicBoolean(false);
Supplier<Cluster> clusterSupplier = () -> {
return com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").addContactPoint("127.0.0.2").build();
};
AtomicBoolean allUpgraded = new AtomicBoolean(false);
Random rng = new Random(seed);
boolean reconnected = false;
Map<Pair<Integer, Integer>, PreparedStatement> qualifiedStatements = new HashMap<>();
Map<Pair<Integer, Integer>, PreparedStatement> unqualifiedStatements = new HashMap<>();
cluster = clusterSupplier.get();
for (int j = 0; j < KEYSPACES; j++) {
String ks = "ks" + j;
sessions.put(ks, cluster.connect(ks));
Assert.assertEquals(sessions.get(ks).getLoggedKeyspace(), ks);
}
long firstVersionBump = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
long reconnectAfter = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);
while (!interrupt.get() && (System.nanoTime() < deadline)) {
nodeWithFix.set(rng.nextBoolean());
final int ks = rng.nextInt(KEYSPACES);
final int statementIdx = rng.nextInt(STATEMENTS_PER_KS);
final Pair<Integer, Integer> statementId = Pair.create(ks, statementIdx);
int v = rng.nextInt(INFREQUENT_ACTION_COEF + 1);
Action[] pool;
if (v == INFREQUENT_ACTION_COEF)
pool = infrequent;
else
pool = frequent;
Action action = pool[rng.nextInt(pool.length)];
// logger.info(String.format("Executing %s on the node %s. ks %d", action, nodeWithFix.get() ? "1" : "2", ks));
switch(action) {
case BUMP_VERSION:
if (System.nanoTime() < firstVersionBump)
break;
c.stream().forEach(node -> node.runOnInstance(() -> {
if (version.get().equals(INITIAL_VERSION)) {
CassandraVersion upgradeTo = QueryProcessor.NEW_PREPARED_STATEMENT_BEHAVIOUR_SINCE_40;
while (!version.get().equals(upgradeTo)) {
if (version.compareAndSet(INITIAL_VERSION, upgradeTo)) {
logger.info("Bumped version to " + upgradeTo);
break;
}
}
}
}));
break;
case EXECUTE_QUALIFIED:
if (!qualifiedStatements.containsKey(statementId))
continue;
try {
int counter = 0;
BoundStatement boundStatement = qualifiedStatements.get(statementId).bind();
boundStatement.setHost(getHost(cluster, nodeWithFix.get()));
for (Iterator<Object[]> iter = RowUtil.toObjects(sessions.get("ks" + ks).execute(boundStatement)); iter.hasNext(); ) {
Object[] current = iter.next();
int v0 = (int) current[0];
int v1 = (int) current[1];
Assert.assertEquals(v0, 1);
Assert.assertEquals(v1, counter++);
}
if (nodeWithFix.get())
Assert.assertEquals(ks, counter);
} catch (Throwable t) {
if (t.getCause() != null && t.getCause().getMessage().contains("Statement was prepared on keyspace"))
continue;
throw t;
}
break;
case EXECUTE_UNQUALIFIED:
if (!unqualifiedStatements.containsKey(statementId))
continue;
try {
BoundStatement boundStatement = unqualifiedStatements.get(statementId).bind();
boundStatement.setHost(getHost(cluster, nodeWithFix.get()));
int counter = 0;
for (Iterator<Object[]> iter = RowUtil.toObjects(sessions.get("ks" + ks).execute(boundStatement)); iter.hasNext(); ) {
Object[] current = iter.next();
int v0 = (int) current[0];
int v1 = (int) current[1];
Assert.assertEquals(v0, 1);
Assert.assertEquals(v1, counter++);
}
if (nodeWithFix.get() && allUpgraded.get()) {
Assert.assertEquals(unqualifiedStatements.get(statementId).getQueryKeyspace() + " " + ks + " " + statementId, ks, counter);
}
} catch (Throwable t) {
if (t.getMessage().contains("ID mismatch while trying to reprepare") || (t.getCause() != null && t.getCause().getMessage().contains("Statement was prepared on keyspace"))) {
logger.info("Detected id mismatch, skipping as it is expected: ");
continue;
}
throw t;
}
break;
case FORGET_PREPARED:
Map<Pair<Integer, Integer>, PreparedStatement> toCleanup = rng.nextBoolean() ? qualifiedStatements : unqualifiedStatements;
Set<Pair<Integer, Integer>> toDrop = new HashSet<>();
for (Pair<Integer, Integer> e : toCleanup.keySet()) {
if (rng.nextBoolean())
toDrop.add(e);
}
for (Pair<Integer, Integer> e : toDrop) toCleanup.remove(e);
toDrop.clear();
break;
case CLEAR_CACHES:
if (!nodeWithFix.get() && !allUpgraded.get())
continue;
c.get(nodeWithFix.get() ? 1 : 2).runOnInstance(() -> {
SystemKeyspace.loadPreparedStatements((id, query, keyspace) -> {
if (rng.nextBoolean())
QueryProcessor.instance.evictPrepared(id);
return true;
});
});
break;
case PREPARE_QUALIFIED:
if (unqualifiedStatements.containsKey(statementId))
continue;
try {
String qs = String.format(qualified, statementIdx, statementIdx, ks);
String keyspace = "ks" + ks;
PreparedStatement preparedQualified = sessions.get("ks" + ks).prepare(qs);
// With prepared qualified, keyspace will be set to the keyspace of the statement when it was first executed
if (allUpgraded.get())
PreparedStatementHelper.assertHashWithoutKeyspace(preparedQualified, qs, keyspace);
qualifiedStatements.put(statementId, preparedQualified);
} catch (Throwable t) {
throw t;
}
break;
case PREPARE_UNQUALIFIED:
if (unqualifiedStatements.containsKey(statementId))
continue;
try {
String qs = String.format(unqualified, statementIdx, statementIdx);
// we don't know where it's going to be executed
PreparedStatement preparedUnqalified = sessions.get("ks" + ks).prepare(qs);
unqualifiedStatements.put(Pair.create(ks, statementIdx), preparedUnqalified);
} catch (InvalidQueryException iqe) {
if (!iqe.getMessage().contains("No keyspace has been"))
throw iqe;
} catch (Throwable t) {
throw t;
}
break;
case BOUNCE_CLIENT:
if (System.nanoTime() < reconnectAfter)
break;
if (!reconnected) {
for (Session s : sessions.values()) s.close();
cluster.close();
cluster = clusterSupplier.get();
for (int j = 0; j < KEYSPACES; j++) sessions.put("ks" + j, cluster.connect("ks" + j));
qualifiedStatements.clear();
unqualifiedStatements.clear();
reconnected = true;
}
break;
}
}
} catch (Throwable t) {
interrupt.set(true);
t.printStackTrace();
while (true) {
Throwable seen = thrown.get();
Throwable merged = Throwables.merge(seen, t);
if (thrown.compareAndSet(seen, merged))
break;
}
throw t;
} finally {
logger.info("Exiting...");
if (cluster != null)
cluster.close();
}
}));
}
for (Thread thread : threads) thread.start();
for (Thread thread : threads) thread.join();
if (thrown.get() != null)
throw thrown.get();
}
}
Aggregations