zoukankan      html  css  js  c++  java
  • Elasticsearch系统学习(七)-ES并发控制

    一、ES并发控制原理

    1.1、ES并发冲突问题

    image

    1.2、悲观锁与乐观锁并发控制图解

    image

    ES内部基于_version进行乐观锁并发控制

    image

    二、并发控制实践

    2.1、基于_version进行乐观锁并发控制

    1)构建数据

    PUT /test_index/test_type/7
    {
      "test_field": "test test"
    }
    
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "7",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": true
    }

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

    GET /test_index/test_type/7
    
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "7",
      "_version": 1,   #此时版本号是1
      "found": true,
      "_source": {
        "test_field": "test test"
      }
    }

    3)模拟一个客户端,先更新了这个数据

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

    PUT /test_index/test_type/7?version=1
    {
      "test_field": "test client 1"
    }
    
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "7",
      "_version": 2,   #此时实际版本号已经变成2了
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": false
    }

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

    PUT /test_index/test_type/7?version=1
    {
      "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": "6t1NqhChSpyuYY7m-Eq2jA",
            "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": "6t1NqhChSpyuYY7m-Eq2jA",
        "shard": "3",
        "index": "test_index"
      },
      "status": 409
    }

    5)在乐观锁成功阻止并发问题之后,尝试正确的完成更新

    首先获取正确的版本号

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

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

    PUT /test_index/test_type/7?version=2
    {
      "test_field": "test client 2"
    }
    
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "7",
      "_version": 3,   #修改成功,版本号再次更改
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": false
    }

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

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

    _version与external version区别

    1)格式区别

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

    2)修改区别

    _version:只有当你提供的version与es中的_version一模一样的时候,才可以进行修改,只要不一样,就报错

    version_type=external:只有当你提供的version比es中的_version大的时候,才能完成修改

    es,_version=1 ==> ?version=1,才能更新成功
    es,_version=1 ==> ?version>1&version_type=external,才能成功,比如说?version=2&version_type=external
    

    模拟external version乐观锁并发控制

    1)构建数据

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

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

    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": "6t1NqhChSpyuYY7m-Eq2jA",
            "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": "6t1NqhChSpyuYY7m-Eq2jA",
        "shard": "1",
        "index": "test_index"
      },
      "status": 409
    }

    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请求,version加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
    }

  • 相关阅读:
    利用正则表达式,完成参数的替换
    使用python读取yaml文件
    python+unittet在linux与windows使用的区别
    python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息
    jmeter线程组之间传参
    requests:json请求中中文乱码处理
    ddt源码修改:HtmlTestRunner报告依据接口名显示用例名字
    使用openpyxl的styles,实现写入值时加背景色
    批量数据请求接口
    locust参数化
  • 原文地址:https://www.cnblogs.com/hujinzhong/p/11447335.html
Copyright © 2011-2022 走看看