ES并發(fā)沖突問題與悲觀鎖與樂觀鎖并發(fā)控制

1、ES并發(fā)沖突問題

圖解ES并發(fā)沖突問題

2、悲觀鎖與樂觀鎖兩種并發(fā)控制解決方案

悲觀鎖與樂觀鎖并發(fā)控制方案
  • 悲觀鎖的優(yōu)點是:方便,直接加鎖,對應(yīng)用程序來說,透明,不需要做額外的操作;缺點,并發(fā)能力很低,同一時間只能一條線程操作數(shù)據(jù)
  • 樂觀鎖的優(yōu)點是:并發(fā)能力很高,不給數(shù)據(jù)加鎖,大量線程并發(fā)操作;缺點,麻煩,每次更新的時候,都要先對比版本號,然后可能需要重新加載數(shù)據(jù),再次修改,再寫;這個過程,可能要重復(fù)好幾次。

3、Elasticsearch內(nèi)部如何基于_version進(jìn)行樂觀鎖并發(fā)控制

(1)_version元數(shù)據(jù)

PUT /test_index/test_type/6
{
  "test_field": "test test"
}

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "6",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

第一次創(chuàng)建一個document的時候,它的_version內(nèi)部版本號就是1;以后,每次對這個document執(zhí)行修改或者刪除操作,都會對這個_version版本號自動加1;哪怕是刪除,也會對這條數(shù)據(jù)的版本號加1

{
  "found": true,
  "_index": "test_index",
  "_type": "test_type",
  "_id": "6",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

我們會發(fā)現(xiàn),在刪除一個document之后,可以從一個側(cè)面證明,它不是立即物理刪除掉的,因為它的一些版本號等信息還是保留著的。先刪除一條document,再重新創(chuàng)建這條document,其實會在delete version基礎(chǔ)之上,再把version號加1

PUT /test_index/test_type/7
{
  "test_field": "test test"
}
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "6",
  "_version": 1,  //這里是1
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

#刪除
DELETE  /test_insex/test_type/7

PUT /test_index/test_type/7
{
  "test_field": "test test"
}
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "6",
  "_version": 2,  //變成了2
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

(2)圖解內(nèi)部如何基于_version進(jìn)行樂觀鎖并發(fā)控制


_version進(jìn)行樂觀鎖并發(fā)控制

4、實戰(zhàn)演練基于_version進(jìn)行樂觀鎖并發(fā)控制

  1. 先構(gòu)造一條數(shù)據(jù)出來
#添加
PUT /test_index/test_type/7
{
  "test_field":"test test"
}
  1. 模擬兩個客戶端,都獲取到了同一條數(shù)據(jù)
#查詢
GET /test_index/test_type/7
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "7",
  "_version": 1,
  "found": true,
  "_source": {
    "test_field": "test test"
  }
}
  1. 其中一個客戶端,先更新了一下這個數(shù)據(jù),同時帶上數(shù)據(jù)的版本號,確保說,es中的數(shù)據(jù)的版本號,跟客戶端中的數(shù)據(jù)的版本號是相同的,才能修改
#修改
PUT /test_index/test_type/7?version=1
{
  "test_field":"test client1"
}
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "7",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}
  1. 另外一個客戶端,嘗試基于version=1的數(shù)據(jù)去進(jìn)行修改,同樣帶上version版本號,進(jìn)行樂觀鎖的并發(fā)控制
#再次修改
PUT /test_index/test_type/7?version=1 
{
  "test_field": "test client 2"
}
#返回報錯
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_type][7]: version conflict, current version [2] is different than the one provided [1]",
        "index_uuid": "EbphihYrQMWacVrHu8eWAw",
        "shard": "3",
        "index": "test_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][7]: version conflict, current version [2] is different than the one provided [1]",
    "index_uuid": "EbphihYrQMWacVrHu8eWAw",
    "shard": "3",
    "index": "test_index"
  },
  "status": 409
}
  1. 在樂觀鎖成功阻止并發(fā)問題之后,嘗試正確的完成更新。帶上當(dāng)前正確的版本號,在進(jìn)行修改。
#查詢獲取版本號
GET /test_index/test_type/7
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "7",
  "_version": 2,
  "found": true,
  "_source": {
    "test_field": "test client 1"
  }
}

#帶上當(dāng)前版本號進(jìn)行修改
PUT /test_index/test_type/7?version=2
{
  "test_field":"test client2"
}
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "7",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

基于最新的數(shù)據(jù)和版本號,去進(jìn)行修改,修改后,帶上最新的版本號,可能這個步驟會需要反復(fù)執(zhí)行好幾次,才能成功,特別是在多線程并發(fā)更新同一條數(shù)據(jù)很頻繁的情況下

5. 實戰(zhàn)演練基于external version進(jìn)行樂觀鎖并發(fā)控制

es提供了一個feature,就是說,你可以不用它提供的內(nèi)部_version版本號來進(jìn)行并發(fā)控制,可以基于你自己維護(hù)的一個版本號來進(jìn)行并發(fā)控制。舉個列子,加入你的數(shù)據(jù)在mysql里也有一份,然后你的應(yīng)用系統(tǒng)本身就維護(hù)了一個版本號,無論是什么自己生成的,程序控制的。這個時候,你進(jìn)行樂觀鎖并發(fā)控制的時候,可能并不是想要用es內(nèi)部的_version來進(jìn)行控制,而是用你自己維護(hù)的那個version來進(jìn)行控制。

#語法:
?version=1&version_type=external

version_type=external,唯一的區(qū)別在于。
_version,只有當(dāng)你提供的version與es中的_version一模一樣的時候,才可以進(jìn)行修改,只要不一樣,就報錯;
version_type=external,只有當(dāng)你提供的version比es中的_version大的時候,才能完成修改

# 1. 先添加一條數(shù)據(jù)
PUT /test_index/test_type/8
{
  "test_field":"test"
}

# 2. 模擬兩個客戶端同時查詢到這條數(shù)據(jù)
GET /test_index/test_type/8

# 3. 第一個客戶端先進(jìn)行修改,此時客戶端程序是在自己的數(shù)據(jù)庫中獲取到了這條數(shù)據(jù)的最新版本號,比如說版本號是2
PUT /test_index/test_type/8?version=2&version_type=external
{
  "test_field":"test2"
}
#成功返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "8",
  "_version": 2,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

# 4. 模擬第二個客戶端,同時拿到了自己數(shù)據(jù)庫中維護(hù)的那個版本號,也是2,同時基于version=2發(fā)起了修改
PUT /test_index/test_type/8?version=2&version_type=external
{
  "test_field":"test3"
}
#錯誤返回
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[test_type][8]: version conflict, current version [2] is higher or equal to the one provided [2]",
        "index_uuid": "BTr18MteTx6HxbRogSjrpw",
        "shard": "1",
        "index": "test_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][8]: version conflict, current version [2] is higher or equal to the one provided [2]",
    "index_uuid": "BTr18MteTx6HxbRogSjrpw",
    "shard": "1",
    "index": "test_index"
  },
  "status": 409
}

# 5. 在并發(fā)控制成功后,重新基于最新的版本號發(fā)起更新
#先獲取最新的版本號
GET /test_index/test_type/8
#version=2
#再使用大于最新的版本號的號碼進(jìn)行修改
PUT /test_index/test_type/8?version=3&version_type=external
{
  "test_field":"test3"
}
#成功返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "8",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,415評論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,647評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,130評論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,366評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,887評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,737評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,939評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,174評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,608評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,914評論 2 372

推薦閱讀更多精彩內(nèi)容