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

Posted ql211lin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
}

 

 

 

 


以上是关于ElasticSearch基于version进行乐观锁并发控制的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch系统学习-ES并发控制

Elasticsearch安装教程

ES是啥

Elasticsearch并发冲突问题

elasticsearch 安装配置

熹乐科技范维肖CC:基于开源 YoMo 框架构建“全球同服”的 Realtime Metaverse Application