1
votes

I am creating a cassandra session object for my application and creating few prepared statements for it. Setting different consistency levels on each prepared statement.

statement1 = session.prepare("SELECT key FROM foo WHERE key = ?");
statement1.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);

statement2 = session.prepare("SELECT key FROM foo WHERE key = ?");
statement2.setConsistencyLevel(ConsistencyLevel.QUORUM);

we only had statement1 before, cassandra read latency was less than 10 ms. when we added statement2 and started using it from one part of the code, latency increased to 250 ms for every cassandra calls.

Is this a bug in datastax? Is it possible that

statement2.setConsistencyLevel(ConsistencyLevel.QUORUM);

is setting consistency level to ConsistencyLevel.QUORUM for entire session?

Am I missing something silly?

I am using cassandra-driver-core-3.1.3.jar

1
Understand your concern. Can you quickly test it out with latest cassadra driver? If problem still exists, you can raise one jira in cassandra driver project.sayboras
I just check API docs, setConsistencyLevel also returns statement object, can you change to statement1 = session.prepare("SELECT key FROM foo WHERE key = ?").setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)sayboras
QUORUM requires a QUORUM of the entire cluster, where LOCAL_QUORUM only requires a quorum of the local datacenter. If you have a Multi-DataCenter cluster, that's going to involve more replicas, and will require communication cross datacenters. If your DCs are located in different places (i.e. US and EU) your latency will be rather high...Andy Tolbert
@AndyTolbert Understand your point, but the question is that if any case, setting consistency level for one query might update consistency level for all queries of the same session.sayboras
Are you sure you aren't using statement2 for all your queries? Setting CL on one statement should not affect another and that's not a bug in the driver that i'm aware of.Andy Tolbert

1 Answers

0
votes

Solution:

statement1 = session.prepare("SELECT key FROM foo WHERE key = ?");
statement2 = session.prepare("SELECT key FROM foo WHERE key = ?");

These two prepared statements were exactly same, meaning query string is same: "SELECT key FROM foo WHERE key = ?"

cassandra datastax drive has optimization in place. So, when you create 2nd statement, datastax driver returns you the reference of first statement. Hence,

statement2.setConsistencyLevel(ConsistencyLevel.QUORUM);

ends up setting consistency level on the first statement.

Solution to this problem is to set consistency level on bound statement instead of a prepared statement.

e.g.:

statement1 = session.prepare("SELECT key FROM foo WHERE key = ?");
BoundStatement boundStatement = new BoundStatement(statement1); 
boundStatement.setConsistencyLevel(ConsistencyLevel.QUORUM);