Lock: pessimistic, optimistic
Based on the version optimistic lock, the modification is performed with the current version. If the version is different, the new version needs to be obtained first.
Global lock: put /fs/lock/global/_create{} will make the system concurrency very low
fs: the index you want to lock
lock: is a type you specify for a global lock on this index
global: is the id of the doc corresponding to your global lock
_create: It must be created. If the doc /fs/lock/global already exists, the creation fails and an error is reported.
Delete: DELETE /fs/lock/global
document lock: script is locked
POST /fs/lock/1/_update { "upsert": { "process_id": 123 }, "script": "if ( ctx._source.process_id != process_id ) { assert false }; ctx.op = 'noop';" "params": { "process_id": 123 by } } |
The two process_ids are the same, and return success directly
Not the same, this doc has been locked by others before, failure, error, retry, lock success
Method 2: Put the script file in the config/script directory
scripts/judge-lock.groovy: if ( ctx._source.process_id != process_id ) { assert false }; ctx.op = 'noop';
POST /fs/lock/1/_update { "upsert": { "process_id": 123 }, "script": { "lang": "groovy", "file": "judge-lock", "params": { "process_id": 123 } } } |
freed:
POST /fs/_refresh delete /fs/lock/1
Delete the lock with id=1 in bulk
Put /fs/lock/_bulk{"delete":{"_id":1}}
Query which documents are locked by the current process
GET /fs/lock/_search?scroll=1m { "sort":["_doc"], "query": { "match": { "process_id": "123" } } } |
Shared lock: read lock can be multiple
judge-lock-2.groovy: if (ctx._source.lock_type == 'exclusive') { assert false }; ctx._source.lock_count++ |
|||
|
|||
unlock POST /fs/lock/1/_update { "script": { "lang": "groovy", "file": "unlock-shared" } 每次解一个共享锁,对lock_count先减1,如果减了1之后,是0, } 都解锁完了,将/fs/lock/1删除,彻底解锁所有的共享锁 |
排他锁:写锁 只有一个不能读
PUT /fs/lock/1/_create { "lock_type": "exclusive" }create语法,要求lock必须不能存在,直接自己是第一个上锁的人
如果已经有人上了共享锁,明显/fs/lock/1是存在的,create语法去上排他锁,肯定会报错
解锁排他锁DELETE /fs/lock/1 |