Optimistic Cassandra Lock

I have a table cassandra1:

CREATE TABLE Policy.table1 ( name VARCHAR , date TIMESTAMP , version_num INT, PRIMARY KEY ( name )) WITH caching = 'all' -- and memtable_flush_period_in_ms = 7200 ; ; 

I need to implement optimistic locking on a tis table. When we read a row from table1, we remember its version_num. And when we want to update this line, we compare the current value and the version_num value that we remembered. We also need to increment version_num for each update.

Problems:

  • We cannot put version_num in where, this will create an error: Bad Request: Non PRIMARY KEY version_num found in where where:

      update table where name = 'abc' and version = 3 
  • We cannot set version_num as part of the primary key because we need to update its value

  • If we index version_num, this will not help update operators, the same exception will be thrown
  • The only way I can see is to get the current value of version_num Java, and if the expected and actual values โ€‹โ€‹of version_num are the same, than update. The problem is that in this case we do not have the atomic operation of checking the value of version_num and updating the string.

Do you see any solution to this problem?

+6
source share
1 answer

The solution was found there: Cassandra 2.0, Light Transactions http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html

If I execute the request:

 update table1 set version_num = 5 where name = 'abc' if version_num = 4; 

I will get a row with a [applied] column. This line contains a logical value: true = update was successful, false = in another case.

+6
source

Source: https://habr.com/ru/post/971336/


All Articles