(10) ElasticSearch version control

  ElasticSearch is multi-user operation, it is necessary to ensure the consistency of the data, it uses optimistic locking to ensure data consistency, each time a user operating a document, its version number is automatically incremented by 1, a document operation when the user does not need to document lock, unlock, only need to specify the version to be operated. When the same version number, ElasticSearch will allow smooth implementation of the operation, and when there is a conflict version, ElasticSearch will prompt conflict and throws an exception. ElasticSearch version number in the range of 1 to 2 ^ 63-1.

  1, the internal versioning

  Using _version, following is a simple example:

POST /lib2/books/_bulk
{"index":{"_id":1}}
{"title":"Html5","price":45}

  Displays the version number is 3, "_ version": 3, results are as follows:

{
  "took": 19,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "lib2",
        "_type": "books",
        "_id": "1",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 7,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}

  Now the price is revised to 50, execute the following statement, see "_version" from the results: 4, version number increased by 1,

PUT /lib2/books/1?version=3
{
  "title":"Html5",
  "price":50
}
{
  "_index": "lib2",
  "_type": "books",
  "_id": "1",
  "_version": 4,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 8,
  "_primary_term": 1
}

  Now the version number is 4, if the version number is the implementation of operations 5, the conflict will be reported as follows:

PUT /lib2/books/1?version=5
{
  "title":"Html5",
  "price":50
}
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[books][1]: version conflict, current version [4] is different than the one provided [5]",
        "index_uuid": "yL8koqQqQVqf3uoyvTcBHA",
        "shard": "3",
        "index": "lib2"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[books][1]: version conflict, current version [4] is different than the one provided [5]",
    "index_uuid": "yL8koqQqQVqf3uoyvTcBHA",
    "shard": "3",
    "index": "lib2"
  },
  "status": 409
}

  2, the external version control

  The external version number refers to the time ElasticSearch with a relational database use relational data to realize the version number of the lock mechanisms. ElasticSearch in dealing with the processing of the external version number build number is somewhat different. It is no longer checked _version same value specified in the request, check the current _version but is smaller than the specified value. If the request succeeds, the external version number will be stored in the document _version. In keeping with the external data _version version control, using version_type = external.

  Consider the following example, now the version number is 4, the operation is performed as the version number 5, can be successful, the version number changes 5

PUT /lib2/books/1?version=5&version_type=external
{
  "title":"Html5",
  "price":50
}
{
  "_index": "lib2",
  "_type": "books",
  "_id": "1",
  "_version": 5,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 9,
  "_primary_term": 1
}

  The version number is now 5, performs a version number 5 is operating will be reported conflict can only be performed is greater than the current. as follows:

PUT /lib2/books/1?version=5&version_type=external
{
  "title":"Html5",
  "price":50
}
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[books][1]: version conflict, current version [5] is higher or equal to the one provided [5]",
        "index_uuid": "yL8koqQqQVqf3uoyvTcBHA",
        "shard": "3",
        "index": "lib2"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[books][1]: version conflict, current version [5] is higher or equal to the one provided [5]",
    "index_uuid": "yL8koqQqQVqf3uoyvTcBHA",
    "shard": "3",
    "index": "lib2"
  },
  "status": 409
}

  The version number is now 5, to perform a version number is 12, after the implementation, the version number changes to 12, as follows:

PUT /lib2/books/1?version=12&version_type=external
{
  "title":"Html5",
  "price":50
}
{
  "_index": "lib2",
  "_type": "books",
  "_id": "1",
  "_version": 12,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 10,
  "_primary_term": 1
}

 

 


  

 

Guess you like

Origin www.cnblogs.com/javasl/p/11405365.html