Google Datastore queries and eventual consistency

只愿长相守 提交于 2019-12-18 13:27:57

问题


I would like to confirm my understanding of eventual consistency in the Google datastore. Suppose that I have an entity defined as follows (using ndb):

class Record(ndb.Model):
    name = ndb.StringProperty()
    content = ndb.BlobProperty()

I think I understand Scenarios 1, but I have doubts about Scenarios 2 and 3, so some advice would be highly appreciated.

Scenario 1: I insert a new Record with name "Luca" and a given content. Then, I query the datastore:

qry = Record.query(name=="Luca")
for r in qry.iter():
    logger.info("I got this content: %r" % r.content)

I understand that, due to eventual consistency, the just-inserted record might not be part of the result set. I know about using ancestor queries in order to over come this if needed.

Scenario 2: I read an existing Record with name "Luca", update the content, and write it back. For instance, assuming I have the key "k" of this record:

r = k.get()
r.content = "new content"
r.put()

Then, I run the same query as in Scenario 1. When I get the results, assume that the record is part of the result set (for instance, because the index already contained the record with name "Luca" and key k). Am I then guaranteed that the field content will have its new value "new content"? In other words, if I update a record, leaving its key and indexed fields alone, am I guaranteed to read the most recent value?

Scenario 3: I do similarly to Scenario 2, again where k is the key of a record with name "Luca":

r = k.get()
r.content = "new content"
r.put()

but then I run a modified version of the query:

qry = Record.query(name=="Luca")
for k in qry.iter(keys_only=True):
    r = k.get()
    logger.info("I got this content: %r" % r.content)

In this case, logic tells me I should be getting the latest value of the content, because reading by key guarantees strong consistency. I would appreciate confirmation.


回答1:


Scenario 1. Yes, your understanding is correct.

Scenario 2. No, same query, so still eventually consistent.

Scenario 3. Yes, your understanding is correct.

Also you can avoid eventual consistency by doing everything in the same transaction, but of course this may not be applicable.



来源:https://stackoverflow.com/questions/19884076/google-datastore-queries-and-eventual-consistency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!