ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

上机动手实战基于_version进行乐观并发控制

2022-02-17 21:35:13  阅读:164  来源: 互联网

标签:index 上机 field 版本号 并发 version test type



1.先构造一条数据

PUT /test_index/test_type/7
{
"test_field":"test test"
}
2.模拟两个客户端,都获取到了同一条数据

GET /test_index/test_type/7
1
结果:

{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_version": 1,
"found": true,
"_source": {
"test_field": "test test"
}
}
3.其中一个客户端,先更新了一下这个数据
同时带上数据的版本号,确保说,es中数据的版本号,跟客户端中的数据的版本号相同才能修改

PUT /test_index/test_type/7?version=1
{
"test_field":"test client1"
}
结果version变为2

PUT /test_index/test_type/7?version=1
{
"test_field":"test client1"
}
4.另外一个客户端尝试基于version=1的数据去进行修改,同样带上version的版本号,进行乐观锁的并发控制

PUT /test_index/test_type/7?version=1
{
"test_field":"test client2"
}
结果:

{
"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": "Zt6VyFvpQw60VmopRZ56UA",
"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": "Zt6VyFvpQw60VmopRZ56UA",
"shard": "3",
"index": "test_index"
},
"status": 409
}

5.在乐观锁成功阻止并发问题之后,尝试正确的完成更新
首先查看当前的版本号

GET /test_index/test_type/7

根据版本号把对应的version去put(可能这个步骤需要反复执行好几次,才能成功,特别是在多线程并发更新同一条数据很频繁的情况下)

PUT /test_index/test_type/7?version=2
{
"test_field":"test client2"
}

结果:

{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 2
}

基于内部的version

1.先构造一条数据

PUT /test_index/test_type/9
{
"test_field":"test1"
}

2.模拟两个客户端同时查询到这条数据

GET /test_index/test_type/9

结果:

{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_version": 1,
"found": true,
"_source": {
"test_field": "test1"
}
}

3.第一个客户端先进行修改,此时客户端程序是在自己的数据库中获取到了这条数据的最新版本号,比如说2

PUT /test_index/test_type/9?version=2&version_type=external
{
"test_field":"test client1"
}

成功:

{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 2
}

4.模拟第二个客户端,同时拿到了自己数据库中的哪个版本号,也是2,同时基于version=2发起了修改

PUT /test_index/test_type/9?version=2&version_type=external
{
"test_field":"test client2"
}

错误了:

{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][9]: version conflict, current version [2] is higher or equal to the one provided [2]",
"index_uuid": "Zt6VyFvpQw60VmopRZ56UA",
"shard": "1",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_type][9]: version conflict, current version [2] is higher or equal to the one provided [2]",
"index_uuid": "Zt6VyFvpQw60VmopRZ56UA",
"shard": "1",
"index": "test_index"
},
"status": 409
}


5.在并发控制成功后,重新基于最新的版本号发起更新
获取最新的数据

GET /test_index/test_type/9?
1
{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_version": 2,
"found": true,
"_source": {
"test_field": "test client1"
}
}

指定version为3比原来的大

PUT /test_index/test_type/9?version=3&version_type=external
{
"test_field":"test client2"
}

成功了:

{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 2
}
————————————————
版权声明:本文为CSDN博主「知道的越多 那么不知道的就越多」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42061487/article/details/102013826

标签:index,上机,field,版本号,并发,version,test,type
来源: https://www.cnblogs.com/xiaozhumaopao/p/15906464.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有