主要知识点

基于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"

}

结果如下,可以看到修改不成功,原因是客户端1进行了修改,此时版本号就是2,发起修改时的版本号不大于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、在并发控制成功后,重新基于最新的版本号发起更新

(1)先用GET得到最新的版本号

GET /test_index/test_type/8

结果如下:得到 "_version": 2,

{

"_index": "test_index",

"_type": "test_type",

"_id": "8",

"_version": 2,

"found": true,

"_source": {

"test_field": "test client 1"

}

}

(2)再在"_version": 2的基础上进行更新,这一次version=3

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

}

22.external version的更多相关文章

  1. Elasticsearch由浅入深(五)_version乐观锁、external version乐观锁、partial update、groovy脚本实现partial update

    基于_version进行乐观锁并发控制 先构造一条数据出来 PUT /test_index/test_type/ { "test_field": "test test&q ...

  2. 基于external version进行乐观锁并发控制

    ?version=1?version=1&version_type=external它们的唯一区别在于,_version,只有当你提供的version与es中的_version一模一样的时候, ...

  3. 在Spring(4.3.22)中集成Hibernate(5.4.0)

    (1)pom中添加相关依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibe ...

  4. 说一下集成 diagram-viewer 的心路历程 5.22.0

    1. 下载部署包文件地址:https://github.com/Activiti/Activiti/releases/download/activiti-5.22.0/activiti-5.22.0. ...

  5. Activiti 5.22 spring

    <!-- activiti依赖 --> <dependency> <groupId>org.activiti</groupId> <artifac ...

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

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

  7. 22 Maven高级应用

    1.Maven基础知识回顾 maven是一个项目管理工具.依赖管理:maven对项目中的jar包的管理过程.传统的工程我们直接将jar包放置到项目中. maven工程真正的jar包放置在仓库中,项目中 ...

  8. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project gulimall-common: There are test failures.

    对Maven打包时碰见的问题: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (d ...

  9. java自定义注解实现前后台参数校验

    2016.07.26 qq:992591601,欢迎交流 首先介绍些基本概念: Annotations(also known as metadata)provide a formalized way ...

随机推荐

  1. Hadop使用Partitioner后,结果还是一个文件,怎样解决??

    近期看了一下partitioner.于是照着写了一个列子.最后发现程序并没有将结果分开写入对应的文件,结果还是一个文件,于是乎感觉是不是没实用集群去执行程序,发现control中还是本地执行的代码: ...

  2. Zoj 3535 Gao the String II (AC自己主动机+dp)

    题目大意: 用集合A中的串构造出一个串,使之让很多其它的setB中的串成为他的子串. 思路分析: 和 Codeforces 86C 几乎相同. 只是这里是要用A中的构造. 先用A 和 B的串构造一个自 ...

  3. spark作业运行过程之--DAGScheduler

    DAGScheduler--stage划分和创建以及stage的提交 本篇,我会从一次spark作业的运行为切入点,将spark运行过程中涉及到的各个步骤,包括DAG图的划分,任务集的创建,资源分配, ...

  4. C99新增内容之变长数组(VLA)

    我们在使用多维数组是有一点,任何情况下只能省略第一维的长度.比如在函数中要传一个数组时,数组的行可以在函数调用时传递,当属数组的列却只能在能被预置在函数内部.看下面一个例子: #define COLS ...

  5. B - IQ test

    Problem description Bob is preparing to pass IQ test. The most frequent task in this test is to find ...

  6. 【java基础】(2)Java父类与子类的 内存引用讲解

    从对象的内存角度来理解试试.假设现在有一个父类Father,它里面的变量需要占用1M内存.有一个它的子类Son,它里面的变量需要占用0.5M内存.现在通过代码来看看内存的分配情况:Father f = ...

  7. Django中关于MySQL的bug总结

    bug one: You are trying to add a non-nullable field 'height' to person without a default; we can't d ...

  8. layui 时间前后节点验证

    var start = { istime: true, format: 'YYYY-MM-DD hh:mm:ss', max: '2099-06-16', istoday: true, choose: ...

  9. 联想笋尖S90(S90-t 、S90-u)解锁BootLoader

    工具下载链接: http://pan.baidu.com/s/1eSgZuka 备用下载链接: http://pan.baidu.com/s/1dFKqSId 本篇教程,仅限于联想笋尖S90(S90- ...

  10. sessionStorage和localStorage存储的转换不了json

    先说说localStorage与sessionStorage的差别 sessionStorage是存储浏览器的暂时性的数据,当关闭浏览器下次再打开的时候就不能拿到之前存储的缓存了 localStora ...