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 进行修改等操作时,总是要先比对版本号,只有当版本号一致时才能修改,如果不一致就不能进行操作。

21.实验基于_version进行乐观锁并发控制的更多相关文章

  1. ElasticSearch(九)基于version进行乐观锁并发控制

    一.基于version进行乐观锁并发控制 1).查看一条document GET /test_version/test_version_type/ { "_index" : &qu ...

  2. 20.基于es内部_version进行乐观锁并发控制

  3. Elasticsearch学习笔记(八)Elasticsearch的乐观锁并发控制

    一.基于_version的乐观锁并发控制                 语法:PUT /test_index/test_type/id?version=xxx             更新时带上数据 ...

  4. 25.partial update内置乐观锁并发控制

    主要知识点     (1)partial update内置乐观锁并发控制 (2)retry_on_conflict post /index/type/id/_update?retry_on_confl ...

  5. 并发-AtomicInteger源码分析—基于CAS的乐观锁实现

    AtomicInteger源码分析—基于CAS的乐观锁实现 参考: http://www.importnew.com/22078.html https://www.cnblogs.com/mantu/ ...

  6. AtomicInteger源码分析——基于CAS的乐观锁实现

    AtomicInteger源码分析——基于CAS的乐观锁实现 1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时 ...

  7. 基于redis的乐观锁实践

    redis真是一个分布式应用场景下的好东西,对于我们的应用设计,功劳大大的! 今天要研究的是基于redis的事务机制以及watch指令(CAS)实现乐观锁的过程. 所谓乐观锁,就是利用版本号比较机制, ...

  8. AtomicInteger源码分析——基于CAS的乐观锁实

    1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时间片之间,需要进行cpu切换,也就是会发生进程的切换.切换涉及 ...

  9. 6:Partial Update 内部原理 和 乐观锁并发控制

    Partial Update 内部执行过程: 首先,ES文档是不可变的,它们只能被修改,不能被替换.Update Api 也不例外. Update API 简单使用与之前描述相同的 检索-修改-重建索 ...

随机推荐

  1. hibernate之关于一对一单向,双向关联映射

    [hibernate]之关于一对一单向,双向关联映射 首先我们来看,Hibernate官方对于一对一单向关联的解释: 基于外键关联的单向一对一关联和单向多对一关联差点儿是一样的. 唯一的不同就是单向一 ...

  2. ios 视频播放代码Demo

    方法一: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. / ...

  3. samba笔记

    ############ 1.安装网络yum ############ 2.安装createrepo [root@localhost ~]# yum install createrepo-0.9.8- ...

  4. 洛谷P3834 可持久化线段树(主席树)模板

    题目:https://www.luogu.org/problemnew/show/P3834 无法忍受了,我要写主席树! 解决区间第 k 大查询问题,可以用主席树,像前缀和一样建立 n 棵前缀区间的权 ...

  5. codeforces round #424 div2

    A 暴力查询,分三段查就可以了 #include<bits/stdc++.h> using namespace std; ; int n, pos; int a[N]; int main( ...

  6. 444D

    分类 首先我们要对询问分类,如果相差log级别就第一种询问,否则第二种. 第一种直接暴力lower_bound,复杂度玄学 第二种归并,复杂度玄学 但是就是过了.感觉很容易卡. #include< ...

  7. PCB Genesis加邮票孔(邮票孔增加方向判断--左右上下)实现算法

    之前没解决的问题,当时一下卡在用户界面选择邮票孔增加的方向(上下左右) 与邮票孔实际方位之前的逻辑与非判断上卡壳了,导致一下没进展下去. 回头看原来如此简单 ,将此点记录一下. 1.垂直线定义:80- ...

  8. E20170911-hm

    specification n.     规格; 说明书; 详述;

  9. bzoj2763: [JLOI2011]飞行路线(分层图spfa)

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3234  Solved: 1235[Submit][Stat ...

  10. 大数据攻城狮之Hadoop伪分布式篇

    对于初学大数据的萌新来说,初次接触Hadoop伪分布式搭建的同学可能是一脸萌笔的,那么这一次小编就手把手的教大家在centos7下搭建Hadoop伪分布式. 底层环境: VMware Workstat ...