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

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

课程大纲

1、上机动手实战演练基于external version进行乐观锁并发控制

external version

es提供了一个feature,就是说,你可以不用它提供的内部_version版本号来进行并发控制,可以基于你自己维护的一个版本号来进行并发控制。举个列子,加入你的数据在mysql里也有一份,然后你的应用系统本身就维护了一个版本号,无论是什么自己生成的,程序控制的。这个时候,你进行乐观锁并发控制的时候,可能并不是想要用es内部的_version来进行控制,而是用你自己维护的那个version来进行控制。

?version=1
?version=1&version_type=external

version_type=external,唯一的区别在于,_version,只有当你提供的version与es中的_version一模一样的时候,才可以进行修改,只要不一样,就报错;当version_type=external的时候,只有当你提供的version比es中的_version大的时候,才能完成修改

es,_version=1,?version=1,才能更新成功
es,_version=1,?version>1&version_type=external,才能成功,比如说?version=2&version_type=external

(1)先构造一条数据

PUT /test_index/test_type/8
{
"test_field": "test"
}

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

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

GET /test_index/test_type/8

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

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

PUT /test_index/test_type/8?version=2&version_type=external
{
"test_field": "test client 1"
}

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

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

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

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

(5)在并发控制成功后,重新基于最新的版本号发起更新

GET /test_index/test_type/8

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

PUT /test_index/test_type/8?version=3&version_type=external
{
"test_field": "test client 2"
}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/66878.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、什么是搜索? 百度:我们比如说想找寻任何的信息的时候,…