Elasticsearch 基于external的乐观锁的版本控制

来源:互联网 发布:中日韩总体实力知乎 编辑:程序博客网 时间:2024/05/22 04:29

version_type=external,唯一的区别在于,_version,只有当你提供的version与es中的_version一模一样的时候,才可以进行修改,只要不一样,就报错;当version_type=external的时候,只有当你提供的version比es中的_version大的时候,才能完成修改


es,_version=1,?version=1,才能更新成功
es,_version=1,?version>1&version_type=external,才能成功,比如说?version=2&version_type=external


(1)先构造一条数据


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


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


(2)模拟两个客户端同时查询到这条数据


GET /test_index/test_type/8


{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "8",
  "_version": 1,
  "found": true,
  "_source": {
    "test_field": "test"
  }
}


(3)第一个客户端先进行修改,此时客户端程序是在自己的数据库中获取到了这条数据的最新版本号,比如说是2


PUT /test_index/test_type/8?version=2&version_type=external
{
  "test_field": "test client 1"
}


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


(4)模拟第二个客户端,同时拿到了自己数据库中维护的那个版本号,也是2,同时基于version=2发起了修改


PUT /test_index/test_type/8?version=2&version_type=external
{
  "test_field": "test client 2"
}


{
  "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": "6m0G7yx7R1KECWWGnfH1sw",
        "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": "6m0G7yx7R1KECWWGnfH1sw",
    "shard": "1",
    "index": "test_index"
  },
  "status": 409
}


(5)在并发控制成功后,重新基于最新的版本号发起更新


GET /test_index/test_type/8


{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "8",
  "_version": 2,
  "found": true,
  "_source": {
    "test_field": "test client 1"
  }
}


PUT /test_index/test_type/8?version=3&version_type=external
{
  "test_field": "test client 2"
}


{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "8",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 叶飞唐若雪全文目录 叶飞日王玥瑶柳亦茹 王婿叶飞全文 惊世剑神叶飞 主角叶飞唐若雪 主角叫叶飞唐若雪 狂帝百美缘叶飞全部章节 枭雄本色叶飞 上门狂婿叶飞 少帝归来叶飞 男主叶飞获得太极医仙传承 叶飞张雨彤全文目录 神医仙尊叶飞 母亲柳雪晴被叶飞杨 绝品小神农一叶飞白 都市医武狂少叶飞 神级修真学生叶飞 女总裁超级高手叶飞 都市之医武狂少叶飞 叶鸣第三部 叶鸣 12345 叶鸣 叶鸣陈怡 1888叶鸣全文免费 叶鸣大结局情况 叶鸣和夏楚楚全文阅读 叶鸣和陈怡第三部 叶鸿 叶麟 叶黄 叶默 叶默李瑶 叶默老婆 太古龙帝诀叶默 天降暗黑纪元叶默 全球裂变主角叶默 宁城为什么不杀叶默 号令 87号令 61号令 号令如山