You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Tamar Fraenkel <ta...@tok-media.com> on 2012/03/28 11:00:37 UTC

another counter question

Hi!
I have a ring with 3 nodes and replication factor of 2.
I have counter cf with the following definition:

CREATE COLUMN FAMILY tk_counters
    with comparator = 'UTF8Type'
    and default_validation_class = 'CounterColumnType'
    and key_validation_class = 'CompositeType(UTF8Type,UUIDType)'
    and replicate_on_write = true;

In my code (Java, Hector), I increment a counter and then read it.

   1. Is it possible that the value read will be the value before increment?
   2. If yes, how can I ensure it does not happen. All my reads and writes
   are done with consistency level one.
   3. If this is consistency issue, can I do only the actions on
   tk_counters column family with a higher consistency level?
   4. What does replicate_on_write mean? I thought this should help, but
   maybe even if replicating after write, my read happen before replication
   finished and it returns value from a still not updated node.


My increment code is:
    Mutator<Composite> mutator =
            HFactory.createMutator(keyspace,
                    CompositeSerializer.get());
    mutator.incrementCounter(key,"tk_counters", columnName, inc);
    mutator.execute();

My read counter code is:
    CounterQuery<Composite,String> query =
            createCounterColumnQuery(keyspace,
                    CompositeSerializer.get(), StringSerializer.get());
    query.setColumnFamily("tk_counters");
    query.setKey(key);
    query.setName(columnName);
    QueryResult<HCounterColumn<String>> r = query.execute();
    return r.get().getValue();

Thanks,

*Tamar Fraenkel *
Senior Software Engineer, TOK Media

[image: Inline image 1]

tamar@tok-media.com
Tel:   +972 2 6409736
Mob:  +972 54 8356490
Fax:   +972 2 5612956

Re: another counter question

Posted by aaron morton <aa...@thelastpickle.com>.
> Is it possible that the value read will be the value before increment?
If you are writing and reading at QUOURM you will always see the update.

> If yes, how can I ensure it does not happen. All my reads and writes are done with consistency level one.
Use QUOURM if you want strongly consistent behaviour, otherwise you may be returned any value for a column including no value. 
Note that QUORUM with RF 2 is 2, RF 3 is a good starting point. 

> What does replicate_on_write mean? I thought this should help, but maybe even if replicating after write, my read happen before replication finished and it returns value from a still not updated node.
It has to do with when the increments are replicated. IIRC it was going to be deprecated and always enabled 
background: http://www.datastax.com/dev/blog/whats-new-in-cassandra-0-8-part-2-counters

Cheers


-----------------
Aaron Morton
Freelance Developer
@aaronmorton
http://www.thelastpickle.com

On 28/03/2012, at 10:00 PM, Tamar Fraenkel wrote:

> Hi!
> I have a ring with 3 nodes and replication factor of 2.
> I have counter cf with the following definition:
> 
> CREATE COLUMN FAMILY tk_counters
>     with comparator = 'UTF8Type'
>     and default_validation_class = 'CounterColumnType'
>     and key_validation_class = 'CompositeType(UTF8Type,UUIDType)'
>     and replicate_on_write = true;
> 
> In my code (Java, Hector), I increment a counter and then read it.
> Is it possible that the value read will be the value before increment?
> If yes, how can I ensure it does not happen. All my reads and writes are done with consistency level one. 
> If this is consistency issue, can I do only the actions on tk_counters column family with a higher consistency level?
> What does replicate_on_write mean? I thought this should help, but maybe even if replicating after write, my read happen before replication finished and it returns value from a still not updated node.
> 
> My increment code is:
>     Mutator<Composite> mutator =
>             HFactory.createMutator(keyspace,
>                     CompositeSerializer.get());
>     mutator.incrementCounter(key,"tk_counters", columnName, inc);
>     mutator.execute();
> 
> My read counter code is:
>     CounterQuery<Composite,String> query =
>             createCounterColumnQuery(keyspace,
>                     CompositeSerializer.get(), StringSerializer.get());
>     query.setColumnFamily("tk_counters");
>     query.setKey(key);
>     query.setName(columnName);
>     QueryResult<HCounterColumn<String>> r = query.execute();
>     return r.get().getValue();
> 
> Thanks,
> 
> Tamar Fraenkel 
> Senior Software Engineer, TOK Media 
> 
> <tokLogo.png>
> 
> tamar@tok-media.com
> Tel:   +972 2 6409736 
> Mob:  +972 54 8356490 
> Fax:   +972 2 5612956 
> 
> 
> 


Fwd: another counter question

Posted by Tamar Fraenkel <ta...@tok-media.com>.
Hi!
I have a ring with 3 nodes and replication factor of 2.
I have counter cf with the following definition:

CREATE COLUMN FAMILY tk_counters
    with comparator = 'UTF8Type'
    and default_validation_class = 'CounterColumnType'
    and key_validation_class = 'CompositeType(UTF8Type,UUIDType)'
    and replicate_on_write = true;

In my code (Java, Hector), I increment a counter and then read it.

   1. Is it possible that the value read will be the value before increment?
   2. If yes, how can I ensure it does not happen. All my reads and writes
   are done with consistency level one.
   3. If this is consistency issue, can I do only the actions on
   tk_counters column family with a higher consistency level?
   4. What does replicate_on_write mean? I thought this should help, but
   maybe even if replicating after write, my read happen before replication
   finished and it returns value from a still not updated node.


My increment code is:
    Mutator<Composite> mutator =
            HFactory.createMutator(keyspace,
                    CompositeSerializer.get());
    mutator.incrementCounter(key,"tk_counters", columnName, inc);
    mutator.execute();

My read counter code is:
    CounterQuery<Composite,String> query =
            createCounterColumnQuery(keyspace,
                    CompositeSerializer.get(), StringSerializer.get());
    query.setColumnFamily("tk_counters");
    query.setKey(key);
    query.setName(columnName);
    QueryResult<HCounterColumn<String>> r = query.execute();
    return r.get().getValue();

Thanks,

*Tamar Fraenkel *
Senior Software Engineer, TOK Media

[image: Inline image 1]

tamar@tok-media.com
Tel:   +972 2 6409736
Mob:  +972 54 8356490
Fax:   +972 2 5612956