zoukankan      html  css  js  c++  java
  • 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
    }

     

  • 相关阅读:
    转 Python学习(九)
    转 Python学习(八)
    转 Python学习(七)
    转 Python学习(六)
    转 Python学习(五)
    转 Python学习(四)
    转Python学习(三)
    转Python学习(一)
    面向对象第三章(向上造型、重写、重载)
    面向对象第一章(成员变量、局部变量、重载)
  • 原文地址:https://www.cnblogs.com/ql211lin/p/10271123.html
Copyright © 2011-2022 走看看