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

(1)先构造一条数据出来

  1. PUT /test_index/_doc/7
  2. {
  3. "test_field": "test test"
  4. }

(2)模拟两个客户端,都获取到了同一条数据

GET test_index/_doc/7

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

(3)其中一个客户端,先更新了一下这个数据
同时带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改

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

报错:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
  },
  "status" : 400
}

旧版本的elasticsearch是用version来解决并发问题,采用乐观锁的方式,比较更新时的version是否相同来决定能不能更新
在新版本es再使用version就会报上述问题,而新版本用的是_seq_no_primary_term两个字段来代替version处理并发问题,在查询文档时,这两个字段会返回。

GET /test_index/_doc/7


{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "7",
  "_version" : 4,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_field" : "test test 1232"
  }
}

需要使用 _seq_no_primary_term 两个字段来代替version处理并发问题

PUT /test_index/_doc/7?if_seq_no=4&if_primary_term=1
{
  "test_field": "test test dddddddd"
}

使用如下方式,并且 version 值要大于当前 document 的 version 值:

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

(4)另外一个客户端,尝试基于version=1的数据去进行修改,同样带上version版本号,进行乐观锁的并发控制

PUT /test_index/_doc/7?version=2&version_type=external
{
  "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)在乐观锁成功阻止并发问题之后,尝试正确的完成更新, 重新获取 document 数据和版本号

GET /test_index/_doc/7

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

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

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

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