python - Google Cloud Datastore 中事务的高度一致性

标签 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)

输出是:

[]

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

最佳答案

Google 文档 here解决你的最后一个例子:

Unlike with most databases, queries and gets inside a Cloud Datastore transaction do not see the results of previous writes inside that transaction. Specifically, if an entity is modified or deleted within a transaction, a query or get returns the original version of the entity as of the beginning of the transaction, or nothing if the entity did not exist then.

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

关于python - Google Cloud Datastore 中事务的高度一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319537/

相关文章:

javascript - 如何从 Google App Engine 中的延迟任务返回数据

python-2.7 - 如何在谷歌应用引擎中正确导入 requests_toolbelt?

google-app-engine - gcloud app deploy 触发奇怪的错误 : Conflicting SHA1 sum for file

python - 在 Ubuntu 14.04 可信服务器上使用 nginx 和 Gunicorn 的多个 Django 应用程序

python - 在 Qt 的槽中使用任何 'return' 语句会产生什么影响

python - 在保留换行符的同时进一步拆分文本

python - 如何定义 (x,y) 坐标的二维数组

适用于 App Engine 的 Python REST 框架?

node.js - 什么参数对于 Node js 的 Google Document AI 客户端库无效?

Node.js IPv6 问题