Google Datastore 查询和最终一致性 [英] Google Datastore queries and eventual consistency

查看:33
本文介绍了Google Datastore 查询和最终一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确认我对 Google 数据存储区中的最终一致性的理解.假设我有一个如下定义的实体(使用 ndb):

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()

我想我了解场景 1,但我对场景 2 和 3 有疑问,因此非常感谢您提供一些建议.

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

场景 1: 我插入一个名为Luca"的新记录和一个给定的内容.然后,我查询数据存储:

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.

场景 2: 我读取了名为Luca"的现有记录,更新了内容,然后将其写回.例如,假设我有这个记录的键k":

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()

然后,我运行与场景 1 相同的查询.当我得到结果时,假设该记录是结果集的一部分(例如,因为索引已经包含名称为Luca"且键为 k 的记录).那么我是否可以保证字段内容将具有其新值新内容"?换句话说,如果我更新一条记录,不理会它的键和索引字段,我能保证读取最新的值吗?

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?

场景 3:我的做法与场景 2 类似,同样,其中 k 是名为Luca"的记录的键:

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.

场景 2. 不,相同的查询,所以最终还是一致的.

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

场景 3. 是的,您的理解是正确的.

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 Datastore 查询和最终一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆