尝试同时运行两个弹性请求时,一个请求因版本控制错误而被拒绝
{"error":{"reason":"[operation][wtWubh3DHE2TFpNsL]: version conflict, current version [5] is different than the one provided [4]","index_uuid":"jsbbwKwJSeSCJcgUBX-FeQ","index":"operations","shard":"3","type":"version_conflict_engine_exception","root_cause":[{"reason":"[operation][wtWubh3DHE2TFpNsL]: version conflict, current version [5] is different than the one provided [4]","index_uuid":"jsbbwKwJSeSCJcgUBX-FeQ","index":"operations","shard":"3","type":"version_conflict_engine_exception"}]},"status":409}
第二个进展顺利:
{"result":"updated","_shards":{"total":2,"failed":0,"successful":1},"_index":"operations","_type":"operation","_id":"wtWubh3DHE2TFpNsL","_version":5}
尝试在请求末尾添加 ?refresh,但这也无济于事。编码:
thread {
val json_a = JSONObject().put("script", "ctx._source.thread_in_work+=1")
val a = Unirest.post("http://localhost:9200/operations/operation/wtWubh3DHE2TFpNsL/_update")
.body(json_a.toString())
println(a.asJson().body)
}
thread {
val json_a = JSONObject().put("script", "ctx._source.thread_in_work+=1")
val a = Unirest.post("http://localhost:9200/operations/operation/wtWubh3DHE2TFpNsL/_update")
.body(json_a.toString())
println(a.asJson().body)
}
我知道你可以在这里做+2,但是当一个请求到达服务器时你需要加1,同时有几个请求。因此,此代码允许您重新创建错误。
elasticsearch 版本控制系统的存在是为了实现乐观锁定。这意味着如果当前客户端以外的其他人设法在读写之间更新文档,引擎将拒绝此类更新,这就是发生的情况 - 假设在这种情况下客户端将简单地重复请求,并且更快或稍后客户端将“拖动”您的更新。因此,elastic 只是要求您重复该操作(因为否则它不能保证正确执行,即使是这样一个简单的增量),您需要将操作放在 while 循环中,或者使用参数
retry_on_conflict,这将ES侧也一样。同时,我想指出的是,ES 有一个官方的 java 客户端,当然可以在 kotlin 中使用它,并且使用它可以更容易地处理这些事情(有必要捕获不是 HTTP 409 代码,但一个非常具体的例外),并且 ES 不值得用作数据存储 - 它还没有提供足够的保证,并且因为它首先是一个搜索引擎,所以像这样的问题不是团队的首要任务(那并不意味着他们没有在他们身上工作——这根本不是微不足道的)需要花费大量资源的事情,链接到文档)。换句话说,在某些情况下,您有可能从正在更新的计数器中得到不正确的值,以及丢失对该计数器的更新。