|
106 | 106 | * 一旦发生变化了,那么在取模算法时,就会有问题,会间接导致数据丢失 |
107 | 107 | * 但是 replica shard可以随意增删 |
108 | 108 |
|
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 | | - |
194 | 109 | --------------------------- |
195 | 110 | 活动分片 | |
196 | 111 | --------------------------- |
|
0 commit comments