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
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM