1.基于version的控制,只有当version的版本号一致时,才允许更新。
例如:
1.先建立一条数据:
PUT /test_index/test_type/7
{
"test_field":"test"
}
2.开启两个kibana客户端:
第一个客户端先执行更新操作:
PUT /test_index/test_type/7**?version=1**
{
"test_field":"test_client 1"
}
第二个客户端也执行相应的操作:
put test_index/test_type/7**?version=1**
{
"test_field":"test_client 2"
}
发现报错了:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][7]: version conflict, current version [2] is different than the one provided [1]",
"index_uuid": "ZZuYYEO3QPShE9Se_sEpfw",
"shard": "3",
"index": "test_index"
}
],
"type": "**version_conflict_engine_exception**",
"reason": "**[test_type][7]: version conflict, current version [2] is different than the one provided [1]**",
"index_uuid": "ZZuYYEO3QPShE9Se_sEpfw",
"shard": "3",
"index": "test_index"
},
"status": 409
}
第二个客户端更新时,开始不知道别人修改了数据,也用version=1修改
但是出现版本冲突的错误,所以这时需要重新查询版本号,在更新
put test_index/test_type/7**?version=2**
{
"test_field":"test_client 2"
}
这时候就更新成功了。
2.使用version控制并发,也可以加上 version_type=external
例如这样:
当前的version=3
put test_index/test_type/7**?version=4&version_type=external**
{
“test_field”:”test_client 3”
}
与纯version控制并发操作不同的是,加上version_type=external之后
version的值要大于当前的版本号,才能执行成功。
以上就是ElstaticSearch对于并发的控制策略,上述就是我所有实践的例子,有什么问题,欢迎指正。