一、基于version进行乐观锁并发控制
1)、查看一条document
GET /test_version/test_version_type/
{
"_index" : "test_version",
"_type" : "test_version_type",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"test_field" : "test test"
}
}
2)、模拟多并发下,利用version进行更新
同时带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改
PUT /test_version/test_version_type/?version=
{
"test_field": "test client 1"
} {
"_index" : "test_version",
"_type" : "test_version_type",
"_id" : "",
"_version" : ,
"result" : "updated",
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"_seq_no" : ,
"_primary_term" :
}
PUT /test_version/test_version_type/?version=
{
"test_field": "test client 2"
} {
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_version_type][1]: version conflict, current version [2] is different than the one provided [1]",
"index_uuid": "VT8uFhvTS_qawAksysahtQ",
"shard": "",
"index": "test_version"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_version_type][1]: version conflict, current version [2] is different than the one provided [1]",
"index_uuid": "VT8uFhvTS_qawAksysahtQ",
"shard": "",
"index": "test_version"
},
"status":
}
二、基于external version进行乐观锁并发控制
es提供了一个feature,就是说,你可以不用它提供的内部_version版本号来进行并发控制,可以基于你自己维护的一个版本号来进行并发控制。
1)、查看一条document
GET /test_version/test_version_type/
{
"_index" : "test_version",
"_type" : "test_version_type",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"test_field" : "test"
}
}
2)、语法与区别
?version=1
?version=1&version_type=external
version_type=external,唯一的区别在于,_version,只有当你提供的version与es中的_version一模一样的时候,才可以进行修改,只要不一样,就报错;当version_type=external的时候,只有当你提供的version比es中的_version大的时候,才能完成修改
3)、模拟多并发下,利用version进行更新
PUT /test_version/test_version_type/?version=&version_type=external
{
"test_field": "test client 1"
} {
"_index" : "test_version",
"_type" : "test_version_type",
"_id" : "",
"_version" : ,
"result" : "updated",
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"_seq_no" : ,
"_primary_term" :
}
PUT /test_version/test_version_type/?version=&version_type=external
{
"test_field": "test client 2"
} {
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_version_type][3]: version conflict, current version [2] is higher or equal to the one provided [2]",
"index_uuid": "VT8uFhvTS_qawAksysahtQ",
"shard": "",
"index": "test_version"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_version_type][3]: version conflict, current version [2] is higher or equal to the one provided [2]",
"index_uuid": "VT8uFhvTS_qawAksysahtQ",
"shard": "",
"index": "test_version"
},
"status":
}