crystal-cassandra icon indicating copy to clipboard operation
crystal-cassandra copied to clipboard

Setting consistency does not work

Open munjalpatel opened this issue 4 years ago • 6 comments

Hello,

I am trying to execute the following statements:

stmt = <<-CQL
  begin batch
    insert into #{@tbl_customer} (team_id) values (?) if not exists
    update #{@tbl_customer} set customer_id = ?, created_at = ? where team_id = ?
  apply batch
CQL

@db.exec stmt, team_id, cust_id, now, team_id

However, I am getting the following error when @db.exec executes:

ErrorServerProtocolError: Consistency level for LWT is missing for a request with conditions

To fix this, I am trying to set consistency level like this:

Cassandra::LibCass.execution_profile_set_consistency(profile, Cassandra::LibCass::CassConsistency::ConsistencyAny)
Cassandra::LibCass.execution_profile_set_serial_consistency(profile, Cassandra::LibCass::CassConsistency::ConsistencySerial)

stmt = <<-CQL
  begin batch
    insert into #{@tbl_customer} (team_id) values (?) if not exists
    update #{@tbl_customer} set customer_id = ?, created_at = ? where team_id = ?
  apply batch
CQL

@db.exec stmt, team_id, cust_id, now, team_id

However, seeing consistency in the profile does not seem to make any difference.

cc: @kaukas

munjalpatel avatar Feb 18 '21 20:02 munjalpatel

How did you create the profile? How did you set it to be used for the cluster or statement?

Currently this lib does not support execution profiles. But you could extend it by writing a wrapper object and allowing it to be set on clusters or statements.

kaukas avatar Feb 19 '21 07:02 kaukas

You should also be able to set consistency via CQL.

kaukas avatar Feb 19 '21 11:02 kaukas

Thanks @kaukas . Setting consistency keeps erroring with message of syntax error.

munjalpatel avatar Feb 24 '21 17:02 munjalpatel

I do not see a straightforward way for crystal-cassandra to expose statement objects through crystal-db. And apparently you do need to expose them because I did not find a way to change the default execution profile (unlike Python which has EXEC_PROFILE_DEFAULT).

In other words, the simplest might be to extend crystal-cassandra to take consistency as an URI param, assign a profile to the cluster and then use that profile for every statement. Still no access through db but 🤷‍♂️.

Alternatively, perhaps it's easier to skip the crystal-db layer altogether. Could crystal-cassandra be used directly?

db_context = DB::ConnectionContext.new…
cluster = Cassandra::DBApi::Cluster.acquire(db_context)
# set the exec profile on cluster
session = Cassandra::DBApi::Session.new(db_context)
statement = session.build_unprepared_statement("SELECT ...")
# set the exec profile on statement

kaukas avatar Feb 24 '21 19:02 kaukas

@bcardiff, do you think it would be reasonable to support random extra parameters for exec?

kaukas avatar Oct 14 '21 08:10 kaukas

Apparently random extra parameters are exactly what exec takes. Here is my attempt at supporting statement options.

kaukas avatar Oct 27 '21 12:10 kaukas