Elasticsearch乐观锁并发控制实战
主要参数及技术点
- Elasticsearch 乐观锁技术
- Elasticsearch 参数:
- version:(6.7.0 之后被弃用,需采用第二种方式)
- if_seq_no & if_primary_term :
概念解释:
悲观锁控制(悲观并发控制)
1 | /** |
乐观锁控制
1 | /** |
操作
原数据准备
1 | curl -XPUT 'elasticsearch_host:9200/note_index/_doc/1' -H 'Content-Type:application/json' -d' |
First: 两客户端获取上述记录 (record._version=1 or _seq_no=1)
1 |
|
if_seq_no
Second: 两个客户端依次更新记录
- 6.7.0 前使用version 进行版本控制
1 | # client 1: |
- 6.7.0 及之后使用 if_seq_no , if_primary_term 进行版本控制。
1 | /** |
1 | # client 1: |
Third: 更新重试
1 | /** |
注意:
若想记录被更新后,能及时检索到,需要设置refresh
参数为:wait_for
,该参数所有取值如下:
- false: Don’t refresh after this request. The default.
- true: Force a refresh as part of this request. This refresh policy does not scale for high indexing or search throughput but is useful
to present a consistent view to for indices with very low traffic. And it is wonderful for tests! - wait_for: Leave this request open until a refresh has made the contents of this request visible to search. This refresh policy is
compatible with high indexing and search throughput but it causes the request to wait to reply until a refresh occurs.
参考连接
参考连接:
Elasticsearch Reference [7.9] » REST APIs » Document APIs » ?refresh
Elasticsearch Document《Optimistic concurrency control》
elasticsearch学习笔记(十三)——Elasticsearch乐观锁并发控制实战
Elasticsearch系列—并发控制及乐观锁实现原理
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.