Google Datastore 应用架构问题

Google Datastore app architecture questions

我正在开发一个 Google AppEngine 应用程序,通过其 JSON API 连接到 Google 云数据存储(我正在使用 PHP ).

我正在阅读 Google 提供的所有文档,但我仍有疑问:

  1. 在关于 Transactions 的文档中,有以下提及:"Transactions must operate on entities that belong to a limited number (5) of entity groups"(顺便说一句,几行之后我们可以找到:"All Datastore operations in a transaction can operate on a maximum of twenty-five entity groups")。 我不确定什么是实体组。假设我有一个对象 Country,它仅由其种类 (COUNTRY) 和数据存储区的自动影响键 ID 标识。所以没有祖先路径、等级关系等……所有国家/地区实体是否仅计入 1 个实体组?还是每个国家算一个?

  2. 对于 Country 实体类型,我需要一个增量唯一 ID(如 SQL AUTOINCREMENT)。它必须绝对独特且没有差距。此外,此类对象的创建次数不会超过几分钟/分钟,因此无需处理争用和分片。我正在考虑有一个独特的计数器来反映自动增量并在事务中使用它。以下代码模式可以吗?: Starting transaction, getting the counter, commit the creation of the Country along with the update of the counter. Rollback the transaction if the commit fails. 这种模式是否可以防止 2 个相同 ID 的影响?您能否确认一下,如果 2 个进程同时获得计数器(因此值相同),第一个提交的进程将使另一个进程失败(因此它将能够重新启动并获得新的计数器值)?

  3. documentation 还提到: "If your application receives an exception when attempting to commit a transaction, it does not necessarily mean that the transaction has failed. It is possible to receive exceptions or error messages even when a transaction has been committed and will eventually be applied successfully" !?我们应该如何处理这种情况?如果在创建我的国家时出现此行为(问题 #2),我的自动递增 ID 会出现问题,不!?

  4. 因为数据存储需要一个事务的所有写操作只在一次调用中完成。而且既然事务保证了事务的全部或者none的动作都会被执行,为什么还要回滚呢?

  5. 1 次写入/秒的限制是否仅针对实体(因此由其种类和关键路径定义)而不是整个实体组(只有当我会时我才会放心确定什么是实体组 ;-) 问题 #1)

我在这里停下来不做一个巨大的post。在得到这些问题的答案后,我可能会回来回答其他(或改进的)问题 ;-)

感谢您的帮助。

[更新] 国家/地区仅用作示例 class 对象。

  1. 不是,('Country', 123123)('Country', 679621)不在同一个实体组。但是 ('Country', 123123, 'City', '1')('Country', 123123, 'City', '2') 在同一个实体组中。具有相同祖先的实体属于同一组。

  2. 对国家等事物使用自动递增听起来真是个坏主意。只需根据国家名称生成一个 ID。

  3. 来自同一段:

Whenever possible, structure your Datastore transactions so that the end result will be unaffected if the same transaction is applied more than once.

  1. 在 db 或 ndb 等内部数据存储 API 中,您不必担心回滚,它会自动发生。

  2. 每个整个实体组 大约 每秒写入 1 次,这就是为什么您需要使组尽可能小的原因。