Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hector to get the resulting counter value after doing incrementCounter

We are doing the following to update the value of a counter, now we wonder if there is a straightforward way to get back the updated counter value immediately.

mutator.incrementCounter(rowid1, "cf1", "counter1", value);

like image 306
tom Avatar asked Jan 27 '12 18:01

tom


1 Answers

There's no single 'incrementAndGet' operation in Cassandra thrift API.

Counters in Cassandra are eventually consistent and non-atomic. Fragile ConsistencyLevel.ALL operation is required to get "guaranteed to be updated" counter value, i.e. perform consistent read. ConsistencyLevel.QUORUM is not sufficient (as specified in counters design document: https://issues.apache.org/jira/secure/attachment/12459754/Partitionedcountersdesigndoc.pdf).

To implement incrementAndGet method that looks consistent, you might want at first read counter value, then issue increment mutation, and return (read value + inc).

For example, if previous counter value is 10 to 20 (on different replicas), and one add 50 to it, read-before-increment will return either 60 or 70. And read-after-increment might still return 10 or 20.

like image 139
Wildfire Avatar answered Oct 11 '22 19:10

Wildfire