AppEngine allocateIdRange : clarification about CO

2019-07-04 02:45发布

I'm copying entities from one kind to another, and want to map their long ids in a predictable way. After the mapping is over, I want auto-generation of ids to kick in.

To protect the entities I copy, I want to use allocateIdRange and manually allocate each id as I copy it. My hope is that this will cause the datastore to protect these new ids, and only assign other ids to new entities created after the copy.

One return code has me worried: CONTENTION

Indicates the given KeyRange is empty but the datastore's automatic ID allocator may assign new entities keys in this range. However it is safe to manually assign Keys in this range if either of the following is true:

  1. No other request will insert entities with the same kind and parent as the given KeyRange until all entities with manually assigned keys from this range have been written.

  2. Overwriting entities written by other requests with the same kind and parent as the given KeyRange is acceptable.

Number 2 is out for me. It is not acceptable for these entities to be overwritten.

Number 1 I think is acceptable, but the wording is scary enough that I want to make sure. If I allocate 5 ids, from 100 to 104, and I get CONTENTION back, this seems to indicate that the entities I copy MAY be overwritten with new entities with automatic ids in the future. BUT, if I hurry up and write my own entities with ids manually set to 100, 101, 102, 103, and 104, I will be safe and new entities with automatic ids will NOT receive these ids.

I'm worried because I don't understand how this would work. I don't think of the id allocator as paying attention to what gets written.

TL;DR

Imagine the following scenario:

allocateIdRange(100, 104); // returns CONTENTION
putEntityWithManualId(100);
putEntityWithManualId(101);
putEntityWithManualId(102);
putEntityWithManualId(103);
putEntityWithManualId(104);
// all puts succeed

now, when, later, I call

putNewEntityWithAutomaticId();

is there any risk that the automatic id will be 100, 101, 102, 103, or 104?

1条回答
放荡不羁爱自由
2楼-- · 2019-07-04 03:08

The documentation follows as bellow:

The datastore's automatic ID allocator will not assign a key to a new entity that will overwrite an existing entity, so once the range is populated there will no longer be any contention.

Thus, you don't need to worry that your newly copied entities will be overwritten.

查看更多
登录 后发表回答