Skip to content

Commit 21d3858

Browse files
committed
ignore
1 parent 17f829e commit 21d3858

5 files changed

Lines changed: 92 additions & 86 deletions

File tree

Elasticsearch/es-document-删除.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
document - 删除 |
33
--------------------
44
# document不会被立即的物理删除,只会被标记为delete,当数据越来越多的试试,在后台自动的删除
5+
* 尝试删除一个doc后, 再次根据这个id创建一个doc, 发现_version字段会增加
6+
57

68
--------------------
79
document - 删除 |
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
----------------------------
2+
乐观锁并发控制 |
3+
----------------------------
4+
# 文档
5+
https://www.elastic.co/guide/en/elasticsearch/reference/current/optimistic-concurrency-control.html
6+
7+
8+
# 检索的结果中会存在两个元数据
9+
{
10+
...
11+
"_seq_no" : 28,
12+
"_primary_term" : 1
13+
}
14+
15+
16+
# CAS更新
17+
PUT /<index>/_doc/<id>?if_seq_no=<_seq_no>&if_primary_term=<_primary_term>
18+
19+
POST /<index>/_doc/<id>?if_seq_no=<_seq_no>&if_primary_term=<_primary_term>
20+
21+
POST /<index>/_update/<id>?if_seq_no=<_seq_no>&if_primary_term=<_primary_term>
22+
23+
24+
* 通过查询参数: if_seq_no if_primary_term 来控制版本号,如果更新失败, 抛出异常
25+
{
26+
"error": {
27+
"root_cause": [
28+
{
29+
"type": "version_conflict_engine_exception",
30+
"reason": "[1]: version conflict, required seqNo [8], primary term [1]. current document has seqNo [9] and primary term [1]",
31+
"index_uuid": "NhO0l2JpRW-MwwnQpjexcA",
32+
"shard": "0",
33+
"index": "goods"
34+
}
35+
],
36+
"type": "version_conflict_engine_exception",
37+
"reason": "[1]: version conflict, required seqNo [8], primary term [1]. current document has seqNo [9] and primary term [1]",
38+
"index_uuid": "NhO0l2JpRW-MwwnQpjexcA",
39+
"shard": "0",
40+
"index": "goods"
41+
},
42+
"status": 409
43+
}
44+
45+
* 如果是部分更新, 那么只有在更新成功(更新的内容有修改)的时候才会去判断版本号
46+
47+
----------------------------
48+
版本控制 |
49+
----------------------------
50+
# 每个doc都有一个 version 字段
51+
{
52+
"_version" : 20,
53+
}
54+
55+
* 默认从1开始, 并在每次更新时递增, 包括删除
56+
57+
# 执行修改时, 使用版本号
58+
PUT /<index>/_doc/<id>?version=<_version>&version_type=<version_type>
59+
{
60+
"message" : "elasticsearch now has versioning support, double cool!"
61+
}
62+
63+
* CAS更新,如果使用doc内部的版本号则, 使用 if_seq_no if_primary_term
64+
* CAS更新,如果使用外部的版本号, 则使用 version
65+
66+
# version_type ,枚举值
67+
internal
68+
* 仅在给定版本与存储文档的版本相同时才对文档编制索引
69+
* 已经不支持了: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead
70+
71+
external / external_gt
72+
external_gte
73+
74+
# 可以把版本号交给外部程序控制, external version
75+
* es提供了一个feature,可以不使用内容部的_version版本号来进行并发控制
76+
* 可以基于自己维护的'version版本号'来进行并发控制
77+
* 使用场景
78+
在mysql中也存在一份数据,应用系统本身就维护了一个版本号,此时使用乐观锁控制的时候,不想使用es的version,而是想使用应用系统中的version
79+
80+
* version控制语法
81+
?version=<_version>&version_type=external
82+
83+
* version_type=external 的时候,version参数必须要大于当前的_version才能更新成功
84+
* 在修改成功后,并且会把document的_version修改为version参数的值

Elasticsearch/es-document.java

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -106,91 +106,6 @@
106106
* 一旦发生变化了,那么在取模算法时,就会有问题,会间接导致数据丢失
107107
* 但是 replica shard可以随意增删
108108

109-
----------------------------
110-
乐观锁并发控制 |
111-
----------------------------
112-
# 文档
113-
https://www.elastic.co/guide/en/elasticsearch/reference/current/optimistic-concurrency-control.html
114-
115-
116-
# 检索的结果中会存在两个元数据
117-
{
118-
...
119-
"_seq_no" : 28,
120-
"_primary_term" : 1
121-
}
122-
123-
124-
# CAS更新
125-
PUT /<index>/_doc/<id>?if_seq_no=<_seq_no>&if_primary_term=<_primary_term>
126-
127-
POST /<index>/_doc/<id>?if_seq_no=<_seq_no>&if_primary_term=<_primary_term>
128-
129-
POST /<index>/_update/<id>?if_seq_no=<_seq_no>&if_primary_term=<_primary_term>
130-
131-
132-
* 通过查询参数: if_seq_no if_primary_term 来控制版本号,如果更新失败, 抛出异常
133-
{
134-
"error": {
135-
"root_cause": [
136-
{
137-
"type": "version_conflict_engine_exception",
138-
"reason": "[1]: version conflict, required seqNo [8], primary term [1]. current document has seqNo [9] and primary term [1]",
139-
"index_uuid": "NhO0l2JpRW-MwwnQpjexcA",
140-
"shard": "0",
141-
"index": "goods"
142-
}
143-
],
144-
"type": "version_conflict_engine_exception",
145-
"reason": "[1]: version conflict, required seqNo [8], primary term [1]. current document has seqNo [9] and primary term [1]",
146-
"index_uuid": "NhO0l2JpRW-MwwnQpjexcA",
147-
"shard": "0",
148-
"index": "goods"
149-
},
150-
"status": 409
151-
}
152-
153-
* 如果是部分更新, 那么只有在更新成功(更新的内容有修改)的时候才会去判断版本号
154-
155-
----------------------------
156-
版本控制 |
157-
----------------------------
158-
# 每个doc都有一个 version 字段
159-
{
160-
"_version" : 20,
161-
}
162-
163-
* 默认从1开始, 并在每次更新时递增, 包括删除
164-
165-
# 执行修改时, 使用版本号
166-
PUT /<index>/_doc/<id>?version=<_version>&version_type=<version_type>
167-
{
168-
"message" : "elasticsearch now has versioning support, double cool!"
169-
}
170-
171-
* CAS更新,如果使用doc内部的版本号则, 使用 if_seq_no if_primary_term
172-
* CAS更新,如果使用外部的版本号, 则使用 version
173-
174-
# version_type ,枚举值
175-
internal
176-
* 仅在给定版本与存储文档的版本相同时才对文档编制索引
177-
* 已经不支持了: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead
178-
179-
external / external_gt
180-
external_gte
181-
182-
# 可以把版本号交给外部程序控制, external version
183-
* es提供了一个feature,可以不使用内容部的_version版本号来进行并发控制
184-
* 可以基于自己维护的'version版本号'来进行并发控制
185-
* 使用场景
186-
在mysql中也存在一份数据,应用系统本身就维护了一个版本号,此时使用乐观锁控制的时候,不想使用es的version,而是想使用应用系统中的version
187-
188-
* version控制语法
189-
?version=<_version>&version_type=external
190-
191-
* version_type=external 的时候,version参数必须要大于当前的_version才能更新成功
192-
* 在修改成功后,并且会把document的_version修改为version参数的值
193-
194109
---------------------------
195110
活动分片 |
196111
---------------------------

Elasticsearch/es-集群.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* Master Shard和Replica Shard不能在同一个节点, 如果节点宕机, 主从都不能使用, 不能容错
2323
* Master Shard宕机, 某个Replica Shard会自动成为Master Shard
2424

25-
25+
# 集群节点之间的数据同步是多线程异步的
26+
* Replica节点在更新的时候, 使用_version乐观锁来保证数据的一致性
2627

2728

2829
----------------------------

Idea使用手册.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@
3333
file->setting->Editor->Filr and Code Templates->Includes->File Header
3434

3535

36+
--------------------
37+
IDEA-设置代码粗体 |
38+
--------------------
39+
Editor -> Color Scheme -> General -> Text -> Default Text -> Bold[√]

0 commit comments

Comments
 (0)