Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Being able to toggle constancy level during execute statement #67

Merged
merged 5 commits into from

4 participants

@astronomy88

Would like to have flexibility to configure the consistency level during CQL execute.

For instance, in our use case we have the consistency level set at LOCAL_QUORUM for writes, and ONE for reads.If a user doesn't bother with consistency levels, the default option is QUORUM (which would be the setting that would occur before this change)

lib/cassandra-cql/database.rb
@@ -100,8 +100,12 @@ def prepare(statement, options={}, &block)
end
end
- def execute(statement, *bind_vars)
- result = statement_class.new(self, statement).execute(bind_vars)
+ def execute(statement, consistency=CassandraCQL::Thrift::ConsistencyLevel::QUORUM, *bind_vars)
@kreynolds Owner

This won't work .. this is a breaking change for anybody who has already been using this library. I think our only option to have this be backwards compatible is to instead have a separate method, something like execute_with_consistency() that takes consistency, statement, *bind_vars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/cassandra-cql/database.rb
@@ -111,9 +115,9 @@ def execute(statement, *bind_vars)
raise Error::InvalidRequestException.new($!.why)
end
- def execute_cql_query(cql, compression=CassandraCQL::Thrift::Compression::NONE)
+ def execute_cql_query(cql, compression=CassandraCQL::Thrift::Compression::NONE, consistency)
@kreynolds Owner

consistency should have a default option of QUORUM to be backwards compatible. I know its specified in one of the callers, but it should be here as well in case somebody was relying on that behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/cassandra-cql/statement.rb
@@ -36,7 +36,7 @@ def prepare(statement)
@statement = statement
end
- def execute(bind_vars=[], options={})
+ def execute(bind_vars=[], consistency, options={})
@kreynolds Owner

consistency should be an option here, not another argument to the method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kreynolds
Owner

And its a pain I know, but I'll need tests for the changes as well. I appreciate the effort though, almost there!

@astronomy88

Hey Kelley - tried to follow along with the suggestions! Thanks for taking the time to go through it!

@codekitchen

@astronomy88 do you think you'll be writing the tests for this change soon? if not I may take a stab at it, having this in the gem is the last thing we need before we can upgrade our cassandra clusters.

@astronomy88

@codekitchen Sorry, looks like my schedule will be a little hectic that next few days. If you don't mind taking a stab at it, that would be great! Otherwise, I might be able to try in a couple weeks!

@codekitchen

No worries, thanks for doing the bulk of the work. I can't amend this pull request so I started a new one at #71

@kreynolds kreynolds merged commit 9326076 into kreynolds:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 3 deletions.
  1. +17 −2 lib/cassandra-cql/database.rb
  2. +6 −1 lib/cassandra-cql/statement.rb
View
19 lib/cassandra-cql/database.rb
@@ -100,6 +100,21 @@ def prepare(statement, options={}, &block)
end
end
+ def execute_with_consistency(statement, consistency=CassandraCQL::Thrift::ConsistencyLevel::QUORUM, *bind_vars)
+ # consistency can include either of the following options:
+ # - CassandraCQL::Thrift::ConsistencyLevel::QUORUM
+ # - CassandraCQL::Thrift::ConsistencyLevel::LOCAL_QUORUM
+ # - CassandraCQL::Thrift::ConsistencyLevel::ONE
+ result = statement_class.new(self, statement).execute(bind_vars, {:consistency => consistency})
+ if block_given?
+ yield result
+ else
+ result
+ end
+ rescue CassandraCQL::Thrift::InvalidRequestException
+ raise Error::InvalidRequestException.new($!.why)
+ end
+
def execute(statement, *bind_vars)
result = statement_class.new(self, statement).execute(bind_vars)
if block_given?
@@ -111,9 +126,9 @@ def execute(statement, *bind_vars)
raise Error::InvalidRequestException.new($!.why)
end
- def execute_cql_query(cql, compression=CassandraCQL::Thrift::Compression::NONE)
+ def execute_cql_query(cql, compression=CassandraCQL::Thrift::Compression::NONE, consistency=CassandraCQL::Thrift::ConsistencyLevel::QUORUM)
if use_cql3?
- @connection.execute_cql3_query(cql, compression, CassandraCQL::Thrift::ConsistencyLevel::QUORUM) #TODO consistency level
+ @connection.execute_cql3_query(cql, compression, consistency)
else
@connection.execute_cql_query(cql, compression)
end
View
7 lib/cassandra-cql/statement.rb
@@ -44,7 +44,12 @@ def execute(bind_vars=[], options={})
sanitized_query = Utility.compress(sanitized_query)
end
- res = Result.new(@handle.execute_cql_query(sanitized_query, compression_type))
+ consistency = CassandraCQL::Thrift::ConsistencyLevel::QUORUM
+ if options[:consistency]
+ consistency = options[:consistency]
+ end
+
+ res = Result.new(@handle.execute_cql_query(sanitized_query, compression_type, consistency))
# Change our keyspace if required
if @statement =~ KS_CHANGE_RE
Something went wrong with that request. Please try again.