问题描述
我想确认我对Google数据存储最终一致性的理解。假设我有一个定义如下的实体(使用ndb): 场景1:新名称为Luca的记录和给定内容。然后,我查询数据存储区: 据我所知,由于最终的一致性,刚刚插入的记录可能不是结果集的一部分。 场景2:我读了一个名为Luca的现有记录, ,更新内容并写回。例如,假设我有这个记录的键k: 然后,我运行与方案1中相同的查询。当我得到结果时,假定记录是结果集的一部分(例如,因为索引已经包含名称为Luca和关键字k的记录)。然后,我保证该领域的内容将具有新的价值新内容? 场景3:我再次类似于场景2,其中k是名为Luca的记录的关键字: 但是我运行了查询的修改版本: 情景1.是的,您的理解是正确的。 场景2.不,同样的查询,所以仍然最终一致。 场景3.是的,您的理解是正确的。 您也可以通过在同一交易中完成所有事情来避免最终的一致性,但当然这可能不适用。 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): 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: 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: 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": but then I run a modified version of the query: 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. 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. 这篇关于Google数据存储查询和最终一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
$ p $ 类Record(ndb.Model):
name = ndb.StringProperty()
content =
了解场景1,但我对场景2和场景3有所怀疑,所以我们将非常感谢一些建议。
qry = Record.query(name ==Luca)
for r在qry.iter()中:
logger.info(我得到了这个内容:%r%r.content)
r = k.get()
r.content =new content
r.put()
换句话说,如果我更新了一条记录,只留下它的密钥和索引字段,我是否保证读取最近的值?
r = k.get()
r.content =new content
r.put()
qry = Record.query(name ==Luca)
for qry.iter(keys_only = True):
r = k.get ()
logger.info(我得到了这个内容:%r%r.content)
在这种情况下,逻辑告诉我我应该获得内容的最新价值,因为通过密钥读取保证了一致性。
class Record(ndb.Model):
name = ndb.StringProperty()
content = ndb.BlobProperty()
qry = Record.query(name=="Luca")
for r in qry.iter():
logger.info("I got this content: %r" % r.content)
r = k.get()
r.content = "new content"
r.put()
r = k.get()
r.content = "new content"
r.put()
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)