Google Cloud Datastore中的交易具有很强的一致性

red*_*nce 5 python google-app-engine app-engine-ndb google-cloud-datastore google-cloud-platform

Google Cloud Datastore是一个非关系数据库,建立在最终一致性的概念之上.它还提供了一种通过祖先查询和实体组获得强一致性的方法.但是,在事务中使用祖先查询时,我没有获得强一致性.

考虑一下:

class Child(ndb.Model):

    @classmethod
    def create(cls):
        child = cls()
        child.put()
        print Child.query().fetch()

Child.create()
Run Code Online (Sandbox Code Playgroud)

由于这不使用实体组,因此它最终具有一致性.正如所料,我们得到:

[]
Run Code Online (Sandbox Code Playgroud)

让我们使用实体组和祖先查询来尝试它:

class Parent(ndb.Model):

    pass


class Child(ndb.Model):

    @classmethod
    def create(cls, parent):
        child = cls(parent=parent)
        child.put()
        print Child.query(ancestor=parent).fetch()


parent = Parent().put()
Child.create(parent)
Run Code Online (Sandbox Code Playgroud)

这里我们得到了很强的一致性,所以输出是:

[Child(key=Key('Parent', <id>, 'Child', <id>))]
Run Code Online (Sandbox Code Playgroud)

但是,当我们将一个交易投入到混合中时:

class Parent(ndb.Model):

    pass


class Child(ndb.Model):

    @classmethod
    @ndb.transactional
    def create(cls, parent):
        child = cls(parent=parent)
        child.put()
        print Child.query(ancestor=parent).fetch()


parent = Parent().put()
Child.create(parent)
Run Code Online (Sandbox Code Playgroud)

输出是:

[]
Run Code Online (Sandbox Code Playgroud)

鉴于翻译主要用于祖先查询(跨组标志甚至存在只是为了绕过该要求),为什么在事务中丢失了强一致性?

gae*_*fan 4

谷歌的文档确实解决了你的最后一个例子:

与大多数数据库不同,Cloud Datastore 事务中的查询和获取不会看到该事务中之前写入的结果。具体来说,如果在事务中修改或删除实体,则查询或获取将返回该实体在事务开始时的原始版本,或者如果该实体当时不存在,则不返回任何内容。

我无法比 Google 文档更好地解释这一点,但这是基于 Google 如何实现事务隔离的事务的预期行为。