Cassandra - writing does not break but no values ​​are inserted

I have a cluster of 3 Cassandra 2.0 nodes. In my application, I wrote a test that tries to write and read some data to / from Cassandra. Overall, this works great.

The curiosity is that after restarting my computer, this test will fail, because after writing, I read the same value that I wrote earlier and there I get null instead of the value, but that was no exception when writing. If I manually truncate the column-family I'm using, the test passes. Then I can run this test as often as I want, it runs over and over. Also, it doesn't matter if there are valuables in Kassandra or not. Alwalys result is the same.

If I look at the CLI and CQL wrapper, there are two different views:

cassandra-cli

enter image description here

Does anyone have any idea what is going wrong? The timestamp in the CLI is updated after re-execution, so it seems like it is a reading problem?

Part of my code: For inserts, I tried

Insert.Options insert =   QueryBuilder.insertInto(KEYSPACE_NAME,TABLENAME)
                .value(ID, id)
                .value(JAHR, zonedDateTime.getYear())
                .value(MONAT, zonedDateTime.getMonthValue())
                .value(ZEITPUNKT, date)
                .value(WERT, entry.getValue())
                .using(timestamp(System.nanoTime() / 1000));

      

and

Insert insert = QueryBuilder.insertInto(KEYSPACE_NAME,TABLENAME)
                .value(ID, id)
                .value(JAHR, zonedDateTime.getYear())
                .value(MONAT, zonedDateTime.getMonthValue())
                .value(ZEITPUNKT, date)
                .value(WERT, entry.getValue());

      

My choice looks like

Select.Where select = QueryBuilder.select(WERT)
            .from(KEYSPACE_NAME,TABLENAME)
            .where(eq(ID, id))
            .and(eq(JAHR, zonedDateTime.getYear()))
            .and(eq(MONAT, zonedDateTime.getMonthValue()))
            .and(eq(ZEITPUNKT, Date.from(instant)));

      

Consistency level - QUORUM (for both) and replicationfactor 3

+3


source to share


1 answer


I would say that this seems to be a timestamp issue as truncation solves the problem. In Cassandra, recent entries win and this could be an issue caused by using System.nanoTime () as

This method can only be used to measure elapsed time and is not related to any other concept of system time or wall time.

...

The values ​​returned by this method are only meaningful when the difference between two such values ​​obtained in the same instance of the Java Virtual Machine is calculated.

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime ()

This means that the recording that occurred before the reboot may have been performed "in the future" as compared to the recording after the restart. This will not fail the query, but the written meaning simply will not be visible due to the fact that there is a "new" meaning.



Do you have a requirement to use submillisecond precision for insertion timestamps? If possible, I recommend using System.currentTimeMillis () instead of nanoTime ().

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis ()

If you have a requirement to use submillisecond precision, you could use System.currentTimeMillis () with some kind of atomic counter that was between 0-999 and then use that as a timestamp. This, however, will break if multiple clients insert the same row at the same time.

+3


source







All Articles