17.分布式文档系统_动手实战演练Elasticsearch基于external version进行乐观锁并发控制

1、什么是external version

(1)external version

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

(2)区别

语法区别:
es内部version: ?version=1
external version:?version=1&version_type=external
version_type=external唯一的区别在于:
_version是只有当你提供的version与es中的_version一模一样的时候,才可以进行修改,只要不一样,就报错;
当version_type=external的时候,只有当你提供的version比es中的_version大的时候,才能完成修改
举例
es内部中的 _version = 1,使用内部_version更新的时候?version=1才能成功;使用external version 的话,需要version>1才能成功,
比如?version=2&version_type=external

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

(1)先构造一条数据

 

PUT /test_index/test_type/8
{
  "test_field":"test test8"
}
-----------------------------结果-----------------------------
{
  "_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 test8"
  }
}

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

 

客户端自己的版本号为2,es内部的版本号为1,2>1,所以更新成功
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": "8GnE3L60TnWxjSrPQ7Xa2A",
        "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": "8GnE3L60TnWxjSrPQ7Xa2A",
    "shard": "1",
    "index": "test_index"
  },
  "status": 409
}
内部版本号已经更新为2,external版本号与内部版本号冲突,更新失败

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值