concurrency-control.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. [[optimistic-concurrency-control]]
  2. == Optimistic concurrency control
  3. Elasticsearch is distributed. When documents are created, updated, or deleted,
  4. the new version of the document has to be replicated to other nodes in the cluster.
  5. Elasticsearch is also asynchronous and concurrent, meaning that these replication
  6. requests are sent in parallel, and may arrive at their destination out of sequence.
  7. Elasticsearch needs a way of ensuring that an older version of a document never
  8. overwrites a newer version.
  9. To ensure an older version of a document doesn't overwrite a newer version, every
  10. operation performed to a document is assigned a sequence number by the primary
  11. shard that coordinates that change. The sequence number is increased with each
  12. operation and thus newer operations are guaranteed to have a higher sequence
  13. number than older operations. Elasticsearch can then use the sequence number of
  14. operations to make sure a newer document version is never overridden by
  15. a change that has a smaller sequence number assigned to it.
  16. For example, the following indexing command will create a document and assign it
  17. an initial sequence number and primary term:
  18. [source,js]
  19. --------------------------------------------------
  20. PUT products/_doc/1567
  21. {
  22. "product" : "r2d2",
  23. "details" : "A resourceful astromech droid"
  24. }
  25. --------------------------------------------------
  26. // CONSOLE
  27. You can see the assigned sequence number and primary term in the
  28. `_seq_no` and `_primary_term` fields of the response:
  29. [source,js]
  30. --------------------------------------------------
  31. {
  32. "_shards" : {
  33. "total" : 2,
  34. "failed" : 0,
  35. "successful" : 1
  36. },
  37. "_index" : "products",
  38. "_type" : "_doc",
  39. "_id" : "1567",
  40. "_version" : 1,
  41. "_seq_no" : 362,
  42. "_primary_term" : 2,
  43. "result" : "created"
  44. }
  45. --------------------------------------------------
  46. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 2/"_primary_term" : $body._primary_term/]
  47. Elasticsearch keeps tracks of the sequence number and primary term of the last
  48. operation to have changed each of the documents it stores. The sequence number
  49. and primary term are returned in the `_seq_no` and `_primary_term` fields in
  50. the response of the <<docs-get,GET API>>:
  51. [source,js]
  52. --------------------------------------------------
  53. GET products/_doc/1567
  54. --------------------------------------------------
  55. // CONSOLE
  56. // TEST[continued]
  57. returns:
  58. [source,js]
  59. --------------------------------------------------
  60. {
  61. "_index" : "products",
  62. "_type" : "_doc",
  63. "_id" : "1567",
  64. "_version" : 1,
  65. "_seq_no" : 362,
  66. "_primary_term" : 2,
  67. "found": true,
  68. "_source" : {
  69. "product" : "r2d2",
  70. "details" : "A resourceful astromech droid"
  71. }
  72. }
  73. --------------------------------------------------
  74. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 2/"_primary_term" : $body._primary_term/]
  75. Note: The <<search-search,Search API>> can return the `_seq_no` and `_primary_term`
  76. for each search hit by setting <<search-request-seq-no-primary-term,`seq_no_primary_term` parameter>>.
  77. The sequence number and the primary term uniquely identify a change. By noting down
  78. the sequence number and primary term returned, you can make sure to only change the
  79. document if no other change was made to it since you retrieved it. This
  80. is done by setting the `if_seq_no` and `if_primary_term` parameters of either the
  81. <<docs-index_,Index API>> or the <<docs-delete,Delete API>>.
  82. For example, the following indexing call will make sure to add a tag to the
  83. document without losing any potential change to the description or an addition
  84. of another tag by another API:
  85. [source,js]
  86. --------------------------------------------------
  87. PUT products/_doc/1567?if_seq_no=362&if_primary_term=2
  88. {
  89. "product" : "r2d2",
  90. "details" : "A resourceful astromech droid",
  91. "tags": ["droid"]
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. // TEST[continued]
  96. // TEST[catch: conflict]