21_上机动手实战演练基于_version进行乐观锁并发控制

news/2024/10/1 19:32:06

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"
}
}

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

同时带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改

PUT /test_index/test_type/7?version=1
{
"test_field": "test client 1"
}

{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}

(4)另外一个客户端,尝试基于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)在乐观锁成功阻止并发问题之后,尝试正确的完成更新

GET /test_index/test_type/7

{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_version": 2,
"found": true,
"_source": {
"test_field": "test client 1"
}
}

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

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
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/66879.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

19_深度图解剖析悲观锁与乐观锁两种并发控制方案

课程大纲 1、深度图解剖析悲观锁与乐观锁两种并发控制方案

js逆向实战之酷我音乐请求参数reqId加密逻辑

声明:本篇文章仅用于知识分享 实战网站:https://www.kuwo.cn/search/list?key=可以不是你 加密逻辑分析访问界面,根据数据包的回显内容判断哪个是我们需要的。找到相应的数据包,看下请求参数。发现reqId参数是一串随机字符串,所以就需要知道该参数的生成过程。全局搜索re…

13_图解Elasticsearch容错机制:master选举,replica容错,数据恢复

课程大纲 1、图解Elasticsearch容错机制:master选举,replica容错,数据恢复 (1)9 shard,3 node (2)master node宕机,自动master选举,red (3)replica容错:新master将replica提升为primary shard,yellow (4)重启宕机node,master copy replica到该node,使用原有的…

12_分布式原理_图解横向扩容过程,如何超出扩容极限,以及如何提升容错性

1、图解横向扩容过程,如何超出扩容极限,以及如何提升容错性 (1)primary&replica自动负载均衡,6个shard,3 primary,3 replica (2)每个node有更少的shard,IO/CPU/Memory资源给每个shard分配更多,每个shard性能更好 (3)扩容的极限,6个shard(3 primary,3 repli…

09_手工画图剖析Elasticsearch的基础分布式架构

课程大纲 1、Elasticsearch对复杂分布式机制的透明隐藏特性 2、Elasticsearch的垂直扩容与水平扩容 3、增减或减少节点时的数据rebalance 4、master节点 5、节点对等的分布式架构1、Elasticsearch对复杂分布式机制的透明隐藏特性 Elasticsearch是一套分布式的系统,分布式是为了…

10_shardreplica机制再次梳理以及单node环境中创建index图解

1、shard&replica机制再次梳理 2、图解单node环境下创建index是什么样子的1、shard&replica机制再次梳理 (1)index包含多个shard (2)每个shard都是一个最小工作单元,承载部分数据,lucene实例,完整的建立索引和处理请求的能力 (3)增减节点时,shard会自动在nod…

04_手工画图剖析Elasticsearch核心概念:NRT、索引、分片、副本等

课程大纲 1、lucene和elasticsearch的前世今生 2、elasticsearch的核心概念 3、elasticsearch核心概念 vs. 数据库核心概念1、lucene和elasticsearch的前世今生 lucene,最先进、功能最强大的搜索库,直接基于lucene开发,非常复杂,api复杂(实现一些简单的功能,写大量的java…

02_用大白话告诉你什么是Elasticsearch

大白话、什么是Elasticsearch Elasticsearch,分布式,高性能,高可用,可伸缩的搜索和分析系统 1、什么是搜索? 2、如果用数据库做搜索会怎么样? 3、什么是全文检索、倒排索引和Lucene? 4、什么是Elasticsearch?1、什么是搜索? 百度:我们比如说想找寻任何的信息的时候,…