Bootstrap

使用 external version 进行 Elasticsearch 并发控制

文字内容整理自 B 站中华石杉的 Elasticsearch 顶尖高手系列课程核心知识篇

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

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

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

  • Elasticsearch,_version=1,?version=1,才能更新成功

  • Elasticsearch,_version=1,?version>1&version_type=external,才能成功,比如说?version=2&version_type=external

先构造一条数据

PUT /test_index/test_type/8
{
  "test_field": "test 8"
}

模拟两个客户端同时查询到这条数据

```
GET /test_index/test_type/8

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "8",
  "_version" : 1,
  "_seq_no" : 13,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_field" : "test 8"
  }
}
```

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

PUT test_index/test_type/8?version=1&version_type=external
{
  "test_field": "update from client 1"
}

{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[8]: version conflict, current version [1] is higher or equal to the one provided [1]",
        "index_uuid" : "zJ1EzrvMTIeZgBYwUX5u2A",
        "shard" : "0",
        "index" : "test_index"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[8]: version conflict, current version [1] is higher or equal to the one provided [1]",
    "index_uuid" : "zJ1EzrvMTIeZgBYwUX5u2A",
    "shard" : "0",
    "index" : "test_index"
  },
  "status" : 409
}

PUT test_index/test_type/8?version=2&version_type=external
{
  "test_field": "update from client 1"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "8",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 14,
  "_primary_term" : 1
}

  • 修改 PUT 的时候,输入的 version 值是修改之后的,而不是现在的 version

  • 另外 version 字段可以指定更大的值,而不是递增加一,这样在并发的时候会遇到问题么?比如一个客户端直接把 version 设为 10000

还有一个提醒,给和我一样的小白,在 Kibana 的 Dev Tools 的 Web Console 界面,当光标处在选中的代码片段时,可以直接 Ctrl+Enter,就可以执行,这样就不需要用鼠标了。

模拟第二个客户端,同时拿到了自己数据库中维护的那个版本号,也是2,同时基于version=2发起了修改

PUT test_index/test_type/8?version=5
{
  "test_field": "update from client 2 version 5"
}

{
  "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
}

如果没有 versiontype=external 就会得到如上报错信息,当然也可以使用 ifseq_no 和 if_primary_term 来更新。

即使加上了 version_type,同样会得到 error。

PUT test_index/test_type/8?version=5&version_type=external
{
  "test_field": "update from client 2 version 5"
}

{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[8]: version conflict, current version [5] is higher or equal to the one provided [5]",
        "index_uuid" : "zJ1EzrvMTIeZgBYwUX5u2A",
        "shard" : "0",
        "index" : "test_index"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[8]: version conflict, current version [5] is higher or equal to the one provided [5]",
    "index_uuid" : "zJ1EzrvMTIeZgBYwUX5u2A",
    "shard" : "0",
    "index" : "test_index"
  },
  "status" : 409
}

在并发控制成功后,重新基于最新的版本号发起更新

PUT test_index/test_type/8?version=6&version_type=external
{
  "test_field": "update version 6"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "8",
  "_version" : 6,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 16,
  "_primary_term" : 1
}

version 很关键,那么我一上来就给一个很大的 version 呢?