简体   繁体   English

同时创建 Cosmos 文档

[英]Simultaneous Cosmos document creation

I am getting a race condition when trying to update (here creating the document for the first time) to Cosmos with the same primary key and part key simultaneously, and as a result, I am losing one of the updates.当我尝试同时使用相同的主键和部分键更新(这里是第一次创建文档)到 Cosmos 时,我遇到了竞争条件,结果,我丢失了一个更新。 The business logic is to update the document if it already exists and create it if it does not exist, but losing some data if two creations occur at the same timestamp.业务逻辑是如果文档已经存在则更新文档,如果不存在则创建文档,但如果两次创建发生在同一时间戳,则会丢失一些数据。 I know we can achieve concurrency using eTag, but here the issue happens only for the first update or creation of a document.我知道我们可以使用 eTag 实现并发,但这里的问题只发生在第一次更新或创建文档时。

For example, consider the following models: { "id": 1, "pkey": 2, "value": ["first text"]} and例如,考虑以下模型: { "id": 1, "pkey": 2, "value": ["first text"]}

{ "id": 1, "pkey": 2, "value": ["second text"]} I have some internal business logic to update and append the value array. { "id": 1, "pkey": 2, "value": ["second text"]}我有一些内部业务逻辑要更新和 append 值数组。 { "id": 1, "pkey": 2, "value": ["first text," "second text,"] } But I lose one value when cosmos creation occurs simultaneously. { "id": 1, "pkey": 2, "value": ["first text," "second text,"] }但是当 cosmos creation 同时发生时,我失去了一个值。 Please feel free to correct the question if any errors are found.如果发现任何错误,请随时更正问题。

Please help if anyone has faced a similar issue.如果有人遇到类似问题,请提供帮助。 tried keeping etag in the request option, but the issue still persists.尝试将 etag 保留在请求选项中,但问题仍然存在。

Based on your description it looks like you are executing concurrent Replaces over existing documents.根据您的描述,您似乎正在对现有文档执行并发替换。

The best way to avoid the scenario where a second concurrent Replace removed the data added by the first is to use Optimistic Concurrency .避免第二个并发替换删除第一个添加的数据的情况的最佳方法是使用乐观并发

In a nutshell:简而言之:

  1. Read the document you want to update.阅读您要更新的文档。
  2. From the response, you obtain the ETag.从响应中,您可以获得 ETag。
  3. Apply modification locally and send the Replace operation with the IfMatchETag option.在本地应用修改并发送带有 IfMatchETag 选项的替换操作。
  4. If there was a concurrent Replace operation, you will get an HTTP 412 response.如果存在并发替换操作,您将收到 HTTP 412 响应。
  5. Repeat 1-4 until you get a success response.重复 1-4,直到获得成功响应。

Full example from the Java SDK (assuing Java because your question is tagged so) repo: https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/0ead4ca33dac72c223285e1db866c9dc06f5fb47/src/main/java/com/azure/cosmos/examples/documentcrud/async/DocumentCRUDQuickstartAsync.java#L405 Full example from the Java SDK (assuing Java because your question is tagged so) repo: https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/0ead4ca33dac72c223285e1db866c9dc06f5fb47/src/main/ java/com/azure/cosmos/examples/documentcrud/async/DocumentCRUDQuickstartAsync.java#L405

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM