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

一、基于version进行乐观锁并发控制

1)、查看一条document

GET /test_version/test_version_type/1
{
  "_index" : "test_version",
  "_type" : "test_version_type",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "test_field" : "test test"
  }
}

2)、模拟多并发下,利用version进行更新

同时带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改

PUT /test_version/test_version_type/1?version=1
{
  "test_field": "test client 1"
}

{
  "_index" : "test_version",
  "_type" : "test_version_type",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
PUT /test_version/test_version_type/1?version=1
{
  "test_field": "test client 2"
}

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_version_type][1]: version conflict, current version [2] is different than the one provided [1]",
        "index_uuid": "VT8uFhvTS_qawAksysahtQ",
        "shard": "3",
        "index": "test_version"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_version_type][1]: version conflict, current version [2] is different than the one provided [1]",
    "index_uuid": "VT8uFhvTS_qawAksysahtQ",
    "shard": "3",
    "index": "test_version"
  },
  "status": 409
}

 

二、基于external version进行乐观锁并发控制

es提供了一个feature,就是说,你可以不用它提供的内部_version版本号来进行并发控制,可以基于你自己维护的一个版本号来进行并发控制。

1)、查看一条document

GET /test_version/test_version_type/2
{
  "_index" : "test_version",
  "_type" : "test_version_type",
  "_id" : "2",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "test_field" : "test"
  }
}

2)、语法与区别

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

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

3)、模拟多并发下,利用version进行更新

PUT /test_version/test_version_type/3?version=2&version_type=external
{
  "test_field": "test client 1"
}

{
  "_index" : "test_version",
  "_type" : "test_version_type",
  "_id" : "3",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
PUT /test_version/test_version_type/3?version=2&version_type=external
{
  "test_field": "test client 2"
}

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_version_type][3]: version conflict, current version [2] is higher or equal to the one provided [2]",
        "index_uuid": "VT8uFhvTS_qawAksysahtQ",
        "shard": "4",
        "index": "test_version"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_version_type][3]: version conflict, current version [2] is higher or equal to the one provided [2]",
    "index_uuid": "VT8uFhvTS_qawAksysahtQ",
    "shard": "4",
    "index": "test_version"
  },
  "status": 409
}

 

 

 

 

posted @ 2019-01-15 11:59  加肥猫咪  阅读(2346)  评论(0编辑  收藏  举报