简述网站建设的基本特征,网页设计图片位置怎么设置,西地那非使用三大忌,houzz室内设计app鱼说#xff0c;你看不到我眼中的泪#xff0c;因为我在水中。水说#xff0c;我能感觉到你的泪#xff0c;因为你在我心中。 -村上春树 在分布式系统中#xff0c;多个并发操作对同一资源的修改可能导致数据不一致。为了解决这种问题#xff0c;Elasticsearch 提供了乐观… 鱼说你看不到我眼中的泪因为我在水中。水说我能感觉到你的泪因为你在我心中。 -村上春树 在分布式系统中多个并发操作对同一资源的修改可能导致数据不一致。为了解决这种问题Elasticsearch 提供了乐观锁Optimistic Locking机制通过控制文档的 _seq_no 和 _primary_term 来确保更新的原子性。本文将通过 Postman 对 Elasticsearch 进行一次乐观锁机制的测试模拟多个并发请求对同一文档的操作场景。
1.环境配置
在本文的测试中使用的 Elasticsearch 服务地址为http://192.168.56.100:9200。
首先我们通过 Postman 来测试乐观锁的应用场景。这里假设你已经安装并配置好了 Elasticsearch。
Step 1: 创建文档
通过 POST 请求在 Elasticsearch 中创建一个新的文档 URL:http://192.168.56.100:9200/chhei/system/100 请求方式 POST
请求体
{ name: chhei
}输出结果
{_index: chhei,_type: system,_id: 100,_version: 1,result: created,_shards: {total: 2,successful: 1,failed: 0},_seq_no: 2,_primary_term: 1
} 通过上面的请求成功创建了一个名为 chhei 的文档并分配了文档的 _seq_no 和 _primary_term这是乐观锁机制中的关键部分。初始版本号为 1_seq_no 为 2_primary_term 为 1。
Step 2: 乐观请求 1 —— 更新文档
模拟一次乐观锁请求通过控制 _seq_no 和 _primary_term 来进行文档更新
URL: http://192.168.56.100:9200/chhei/system/100?if_seq_no2if_primary_term1 请求方式 POST
请求体
{name: chhei01
}
查询文档 接下来通过 GET 请求查看文档更新后的状态 URL: http://192.168.56.100:9200/chhei/system/100 请求方式 GET
输出结果
{_index: chhei,_type: system,_id: 100,_version: 2,_seq_no: 3,_primary_term: 1,found: true,_source: {name: chhei01}
} 可以看到文档的 name 字段成功更新为了 chhei01文档版本号增加到 2_seq_no 变为 3_primary_term 保持为 1。
Step 3: 乐观请求 2 —— 模拟版本冲突
尝试使用过期的 _seq_no 和 _primary_term 再次更新文档模拟一次乐观锁冲突 URL: http://192.168.56.100:9200/chhei/system/100?if_seq_no2if_primary_term1 请求方式 POST
请求体
{name: chhei02
}输出结果
{error: {root_cause: [{type: version_conflict_engine_exception,reason: [100]: version conflict, required seqNo [2], primary term [1]. current document has seqNo [3] and primary term [1],index_uuid: ChcNr4J8QzyOk1SuFzvzdQ,shard: 0,index: chhei}],type: version_conflict_engine_exception,reason: [100]: version conflict, required seqNo [2], primary term [1]. current document has seqNo [3] and primary term [1],index_uuid: ChcNr4J8QzyOk1SuFzvzdQ,shard: 0,index: chhei},status: 409
}从返回的错误信息中可以看到Elasticsearch 检测到当前文档的 _seq_no 已经变为 3而请求中的 _seq_no 仍然是 2因此产生了 409 Conflict 错误提示版本冲突。 文章转载自: http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.gydth.cn.gov.cn.gydth.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.gprzp.cn.gov.cn.gprzp.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn