21.实验基于_version进行乐观锁并发控制

主要知识点:

  实验基于_version进行乐观锁并发控制

1、实验实战演练基于_version进行乐观锁并发控制

(1)先构造一条数据出来

PUT /test_index/test_type/7

{

"test_field": "test test"

}

(2)模拟两个客户端,都获取到了同一条数据

GET test_index/test_type/7

{

"_index": "test_index",

"_type": "test_type",

"_id": "7",

"_version": 1,

"found": true,

"_source": {

"test_field": "test test"

}

}

此时可以看出两个客户端所取得的数据的 _version都是等于1,

(3)其中一个客户端,先更新这个数据

  更新数据的同时带上数据的版本号,确保此次更新的客户端中的数据的版本号和es中的数据的版本号相同,才能修改

PUT /test_index/test_type/7?version=1

{

"test_field": "test client 1"

}

结果如下,可以看出此时的版本号变成2了。

{

"_index": "test_index",

"_type": "test_type",

"_id": "7",

"_version": 2,

"result": "updated",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"created": false

}

(4)另外一个客户端基于原版本号进行修改

  因为他取数据时原数据的版本号是1,此次修改就尝试基于version=1的数据去进行修改,同样带上version版本号,进行乐观锁的并发控制

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": "6m0G7yx7R1KECWWGnfH1sw",

"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": "6m0G7yx7R1KECWWGnfH1sw",

"shard": "3",

"index": "test_index"

},

"status": 409

}

(5)在乐观锁成功阻止并发问题之后,尝试正确的完成更新

1、先去取现在这份数据在es中的版本号

GET /test_index/test_type/7

{

"_index": "test_index",

"_type": "test_type",

"_id": "7",

"_version": 2,

"found": true,

"_source": {

"test_field": "test client 1"

}

}

2、然后基于最新的数据和版本号去进行修改

  修改时,带上最新的版本号,可能这个步骤会需要反复执行好几次,才能成功,特别是在多线程并发更新同一条数据很频繁的情况下

PUT /test_index/test_type/7?version=2

{

"test_field": "test client 2"

}

{

"_index": "test_index",

"_type": "test_type",

"_id": "7",

"_version": 3,

"result": "updated",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"created": false

}

总结:

es 进行修改等操作时,总是要先比对版本号,只有当版本号一致时才能修改,如果不一致就不能进行操作。

最新文章

  1. Kotlin开发语言文档(官方文档)-- 目录
  2. JAVA中static关键字
  3. 时间js转换方法Date("149...") 转成 2016-7-12 21:23:34 009
  4. WebApiTestClient自定义返回值说明
  5. jquery获取和设置表单数据
  6. knockout 第一个实例visible
  7. 导出数据库数据制成Excel和txt
  8. ios开发--企业帐号发布
  9. Mysql数据表的操作
  10. FZU 11月月赛D题:双向搜索+二分
  11. Eclipse设置保存时自动给变量加final
  12. Intuit Quicken Home & Business 2016(Manage your business and personal finances)
  13. locate命令
  14. Java socket详解(转)
  15. 魔力Python——一些基本概念
  16. windows10安装MongoDB服务启动不了 😀✅已解决!
  17. linux第四章笔记
  18. Python知识(5)--绘图
  19. ios app 实现热更新(无需发新版本号实现app加入新功能)
  20. Java Thread系列(七)死锁

热门文章

  1. SQL Server 2012内部原理及故障排除(专栏)
  2. cocos2dx编译安卓版本号查看C++错误
  3. Git版本号控制:Git分支处理
  4. WCF学习笔记——配置服务引用
  5. ⭐linux主次设备号介绍
  6. 90.商城登录页面Extjs
  7. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
  8. 「LOJ10150」括号配对
  9. B - Is your horseshoe on the other hoof?
  10. 自己写的_top、_parent以及对iframe和frameset的理解