André Salvati André Salvati - 3 months ago 11
Java Question

Not getting Datastore strong consistency reads with getById()

I'm not getting strong consistency on Datastore reads like said here.

Code (with

Objectify
).

a = getById() // ofy().load().type(this.clazz).id(id).now()

Begin transaction

...
a.count++;
putSync(a) // ofy().save().entity(entity).now();
...

End transaction


Notice that I'm getting the counter with 2817 at 03:01:07, even after updates to 2818 at 03:00:27 and to 2819 at 03:00:47.

2015-04-06 03:01:07.582 /rest/x 200 127ms 3kb Java/1.7.0_75 module=x version=v3
2015-04-06 03:01:07.496 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: After getById() ... 2817
2015-04-06 03:01:07.547 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: Before putSync() ... 2818

2015-04-06 03:00:47.449 /rest/x 200 216ms 3kb Java/1.7.0_75 module=x version=v3
2015-04-06 03:00:47.339 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: After getById() ... 2818
2015-04-06 03:00:47.395 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: Before putSync() ... 2819

2015-04-06 03:00:27.227 /rest/x 200 189ms 3kb Java/1.7.0_75 module=x version=v3
2015-04-06 03:00:27.122 [s~primebus01/transmissoes:v3.383369173292091449].<stdout>: After getById() ... 2817
2015-04-06 03:00:27.174 [S~Primebus01/Transmissoes:v3.383369173292091449].<Stdout>: Before PutSync() ... 2818


This behavior occurs only at very specific times during a day.

Wasn't supposed reads (by id) return strong consistency results?

What is wrong? Or is it the expected behavior?

Answer

Your entity is being overwritten. Log publish time could be different from object update time.

You need to call getById() from inside the transaction. As explained here (from Objectify's wiki):

Thing th = ofy().transact(new Work<Thing>() {
    public Thing run() {
        Thing thing = ofy().load().key(thingKey).now();
        thing.modify();
        ofy().save().entity(thing);
        return thing;
    }
});