当两个进程同时写入时如何解决同一密钥上的ElasticSearch冲突 [英] How to fix ElasticSearch conflicts on the same key when two process writing at the same time

查看:88
本文介绍了当两个进程同时写入时如何解决同一密钥上的ElasticSearch冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个进程同时向ES写入数据,而且两个进程可能同时写入具有不同值的相同密钥,这引起了如下异常:

I have multiple processes to write data to ES at the same time, also two processes may write the same key with different values at the same time, it caused the exception as following:

"error" : "VersionConflictEngineException[[website][2] [blog][1]:
             version conflict, current [2], provided [1]]",
"status" : 409

请问如何解决以上问题,因为必须保留多个进程。

How could I fix the above problem please, since I have to keep multiple processes.

推荐答案

VersionConflictEngineException 防止数据丢失。 elasticsearch中的每个文档都有一个 _version 编号,只要更改文档,该编号就会递增。

VersionConflictEngineException is thrown to prevent data loss. Every document in elasticsearch has a _version number that is incremented whenever a document is changed.

当您从ES查询文档时,响应中还会包含该文档的版本。当您更新相同的文档并提供版本时,索引中应该已经存在具有相同版本的文档。

When you query a doc from ES, the response also includes the version of that doc. When you update the same doc and provide a version, then a document with the same version is expected to be already existing in the index.

如果当前版本大于更新请求中的版本,那么我们现在得到的是一个冲突,HTTP错误代码为409, VersionConflictEngineException

If the current version is greater than the one in the update request, What we would get now is a conflict, with the HTTP error code of 409 and VersionConflictEngineException

在当前情况下,


版本冲突,当前 2 ,提供 1

ES中的当前版本为2,而您的请求中的当前版本为1,这意味着其他线程已经修改了文档,而您的更改正尝试覆盖文档。

The current version in ES is 2 whereas in your request is 1 which means some other thread has already modified the doc and your change is trying overwrite the doc.

如果出现VersionConflictEngineException,则应重新获取文档,然后尝试使用最新的更新版本再次进行更新。

In case of VersionConflictEngineException, you should re-fetch the doc and try to update again with the latest updated version.

是否使用版本 / 乐观并发控制,取决于应用。如果您可以忍受数据丢失,则可以避免在更新请求中传递版本。

Whether or not to use the versioning / Optimistic Concurrency Control, depends on the application. If you can live with data-loss, you may avoid passing version in the update request.

这篇关于当两个进程同时写入时如何解决同一密钥上的ElasticSearch冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆