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

时间:2017-02-18 19:13:09

标签: python google-app-engine google-cloud-platform google-cloud-datastore app-engine-ndb

Google Cloud Datastore是一个非关系型数据库,基于eventual consistency的概念。它还提供了一种通过ancestor queries and entity groups获得强一致性的方法。但是,在transaction中使用祖先查询时,我没有获得强大的一致性。

考虑一下:

class Child(ndb.Model):

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

Child.create()

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

[]

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

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)

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

[Child(key=Key('Parent', <id>, 'Child', <id>))]

然而,当我们将一个交易投入混合时:

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)

输出结果为:

[]

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

1 个答案:

答案 0 :(得分:4)

Google的文档here确实解决了您的上一个例子:

  

与大多数数据库不同,查询并进入云数据存储区   事务中没有看到之前写入的结果   交易。具体而言,如果在其中修改或删除实体   一个事务,一个查询或获取返回的原始版本   交易开始时的实体,如果是,则不执行任何操作   实体当时不存在。

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