【问题标题】:Strong consistency within transactions in Google Cloud DatastoreGoogle Cloud Datastore 中事务的高度一致性
【发布时间】:2017-07-08 06:07:27
【问题描述】:

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)

输出是:

[]

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

【问题讨论】:

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


    【解决方案1】:

    Google 的文档 here 确实解决了您的最后一个示例:

    与大多数数据库不同,在 Cloud Datastore 中进行查询和获取 事务中看不到先前写入的结果 交易。具体来说,如果实体在 事务、查询或获取返回原始版本的 交易开始时的实体,或者如果 那时实体不存在。

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

    【讨论】:

    • 像往常一样,我多次阅读 Google 的文档,但似乎错过了我正在寻找的关键信息。我确实认为关于祖先查询的文档提及事务隔离以防止我遇到的混乱/沮丧会很有帮助。感谢您为我指明正确的方向!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2021-10-31
    • 2023-03-10
    相关资源
    最近更新 更多