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/