15.concurrent-control并发控制
文章目錄
- 1. 基于樂觀鎖的并發控制
1. 基于樂觀鎖的并發控制
Elasticsearch是分布式的。創建,更新或刪除文檔時,必須將文檔的新版本復制到群集中的其他節點。 Elasticsearch也是異步和并發的,這意味著這些復制請求是并行發送的,并且可能不按順序到達其目的地。 Elasticsearch需要一種方法來確保文檔的old version永遠不會覆蓋 a newer version。
為確保文檔的較舊版本不會覆蓋較新的版本,對文檔執行的每項操作均由主分片分配一個sequence number(序號),來協調更改。每次操作都會增加sequence number 序列號,因此可以確保較新的操作具有比較舊的操作更高的序列號。然后,Elasticsearch可以使用操作的序列號來確保分配給它的序列號較小的更改不會覆蓋較新的文檔版本。
For example, the following indexing command will create a document and assign it an initial sequence number and primary term:
舉例,如下的index操作create一個doc并賦予一個初始的sequence number和primary term
PUT products/_doc/1567 {"product" : "r2d2","details" : "A resourceful astromech droid" }{"_shards" : {"total" : 2,"failed" : 0,"successful" : 1},"_index" : "products","_type" : "_doc","_id" : "1567","_version" : 1,"_seq_no" : 362,"_primary_term" : 2,"result" : "created" }Elasticsearch會跟蹤上次操作的sequence number序列號和primary term,。sequence number序列號和primary term會在GET API的響應中的_seq_no和_primary_term字段中返回:
GET products/_doc/1567returns:{"_index" : "products","_type" : "_doc","_id" : "1567","_version" : 1,"_seq_no" : 362,"_primary_term" : 2,"found": true,"_source" : {"product" : "r2d2","details" : "A resourceful astromech droid"} }但是search api中默認不會返回,你可以通過一些設置來讓他返回。
Note: The Search API can return the _seq_no and _primary_term for each search hit by setting seq_no_primary_term parameter.
sequence number序列號和primary term唯一的標識了一次change。通過記下返回的序列號和主要術語,可以確保僅在從你的search 請求拿到對應的doc之后到你要對其做修改之前,沒有任何人對該文檔做過修改,在這種情況下你的修改才更改該文檔。這可以通過設置Index API或Delete API的if_seq_no和if_primary_term參數來完成。
For example, the following indexing call will make sure to add a tag to the document without losing any potential change to the description or an addition of another tag by another API:
PUT products/_doc/1567?if_seq_no=362&if_primary_term=2 {"product" : "r2d2","details" : "A resourceful astromech droid","tags": ["droid"] }總結
以上是生活随笔為你收集整理的15.concurrent-control并发控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 14.refresh操作
- 下一篇: 01.search_api_综述