Hector to get the resulting counter value after doing incrementCounter
Asked Answered
R

2

6

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);

Repossess answered 27/1, 2012 at 18:54 Comment(0)
G
7

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.

Gantrisin answered 28/1, 2012 at 17:36 Comment(5)
how will this solve the issue of counters not being atomic. For example, if I do one more (increment and read) operation, then it is possible I will still get the old value: call 1: counter val: 1 read (gets 1), increment by 1 (call issued), return 2 call 2: (the last increment call hasn't propagated through all nodes) read (gets 1), increment by 1 (call issued), return 2Embrocation
@AlastorMoody: As I said, this method allows to implement counters that look consistent. Since cassandra counters are eventually consistent by nature, it's only possible to make counters atomic at the price of significant performance impact (i.e. with distributed locks). If you really need atomic counters - there are plenty tools apart from Cassandra (like Redis)Gantrisin
@Gantrisin What I meant is that this approach might generally cause counters to appear consistent, however it will not be valid in all cases. For example if there are two subsequent calls to your method in quick succession, then it is possible the method will not return the expected value. Could you guide me as to where am I going wrong in this reasoning?Embrocation
@AlastorMoody: You are absolutely correct: The counters are ultimately inconsistent. So, of cause there are situations when it's obvious. :)Gantrisin
I suppose the only way to set them consistent is to set the Consistency Level to ALL.Embrocation
L
0

The only way to do it is query for it. There is no increment-then-read functionality available in Cassandra.

Lug answered 27/1, 2012 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.