2014-02-12 05:40:42 -05:00
|
|
|
metrics = require('./Metrics')
|
|
|
|
Settings = require('settings-sharelatex')
|
2016-07-06 06:50:02 -04:00
|
|
|
redis = require("redis-sharelatex")
|
2017-05-03 11:27:32 -04:00
|
|
|
rclient = redis.createClient(Settings.redis.lock)
|
|
|
|
keys = Settings.redis.lock.key_schema
|
2014-02-12 05:40:42 -05:00
|
|
|
logger = require "logger-sharelatex"
|
2016-04-13 06:59:56 -04:00
|
|
|
os = require "os"
|
|
|
|
crypto = require "crypto"
|
|
|
|
|
2017-05-18 10:04:12 -04:00
|
|
|
Profiler = require "./Profiler"
|
|
|
|
|
2016-04-13 06:59:56 -04:00
|
|
|
HOST = os.hostname()
|
|
|
|
PID = process.pid
|
2016-04-13 11:27:53 -04:00
|
|
|
RND = crypto.randomBytes(4).toString('hex')
|
|
|
|
COUNT = 0
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2017-07-11 09:25:59 -04:00
|
|
|
MAX_REDIS_REQUEST_LENGTH = 5000 # 5 seconds
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
module.exports = LockManager =
|
|
|
|
LOCK_TEST_INTERVAL: 50 # 50ms between each test of the lock
|
2017-05-09 09:06:09 -04:00
|
|
|
MAX_TEST_INTERVAL: 1000 # back off to 1s between each test of the lock
|
2014-02-12 05:40:42 -05:00
|
|
|
MAX_LOCK_WAIT_TIME: 10000 # 10s maximum time to spend trying to get the lock
|
2016-04-13 06:59:56 -04:00
|
|
|
LOCK_TTL: 30 # seconds. Time until lock auto expires in redis.
|
|
|
|
|
|
|
|
# Use a signed lock value as described in
|
|
|
|
# http://redis.io/topics/distlock#correct-implementation-with-a-single-instance
|
|
|
|
# to prevent accidental unlocking by multiple processes
|
|
|
|
randomLock : () ->
|
|
|
|
time = Date.now()
|
2016-04-13 11:27:53 -04:00
|
|
|
return "locked:host=#{HOST}:pid=#{PID}:random=#{RND}:time=#{time}:count=#{COUNT++}"
|
2016-04-13 06:59:56 -04:00
|
|
|
|
|
|
|
unlockScript: 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end';
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
tryLock : (doc_id, callback = (err, isFree)->)->
|
2016-04-13 06:59:56 -04:00
|
|
|
lockValue = LockManager.randomLock()
|
|
|
|
key = keys.blockingKey(doc_id:doc_id)
|
2017-05-18 10:04:12 -04:00
|
|
|
profile = new Profiler("tryLock", {doc_id, key, lockValue})
|
2016-04-13 06:59:56 -04:00
|
|
|
rclient.set key, lockValue, "EX", @LOCK_TTL, "NX", (err, gotLock)->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
if gotLock == "OK"
|
|
|
|
metrics.inc "doc-not-blocking"
|
2017-07-11 09:25:59 -04:00
|
|
|
timeTaken = profile.log("got lock").end()
|
|
|
|
if timeTaken > MAX_REDIS_REQUEST_LENGTH
|
2017-07-12 05:45:23 -04:00
|
|
|
# took too long, so try to free the lock
|
|
|
|
LockManager.releaseLock doc_id, lockValue, (err, result) ->
|
|
|
|
return callback(err) if err? # error freeing lock
|
|
|
|
callback null, false # tell caller they didn't get the lock
|
2017-07-11 09:25:59 -04:00
|
|
|
else
|
2017-07-12 05:47:23 -04:00
|
|
|
callback null, true, lockValue
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
|
|
|
metrics.inc "doc-blocking"
|
2017-05-18 10:04:12 -04:00
|
|
|
profile.log("doc is locked").end()
|
2017-07-12 05:47:23 -04:00
|
|
|
callback null, false
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2017-03-30 10:31:47 -04:00
|
|
|
getLock: (doc_id, callback = (error, lockValue) ->) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
startTime = Date.now()
|
2017-05-09 09:06:09 -04:00
|
|
|
testInterval = LockManager.LOCK_TEST_INTERVAL
|
2017-05-18 10:04:12 -04:00
|
|
|
profile = new Profiler("getLock", {doc_id})
|
2014-02-12 05:40:42 -05:00
|
|
|
do attempt = () ->
|
|
|
|
if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
|
2016-04-13 06:59:56 -04:00
|
|
|
e = new Error("Timeout")
|
|
|
|
e.doc_id = doc_id
|
2017-05-18 10:04:12 -04:00
|
|
|
profile.log("timeout").end()
|
2016-04-13 06:59:56 -04:00
|
|
|
return callback(e)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-04-13 06:59:56 -04:00
|
|
|
LockManager.tryLock doc_id, (error, gotLock, lockValue) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(error) if error?
|
2017-05-18 10:04:12 -04:00
|
|
|
profile.log("tryLock")
|
2014-02-12 05:40:42 -05:00
|
|
|
if gotLock
|
2017-05-18 10:04:12 -04:00
|
|
|
profile.end()
|
2016-04-13 06:59:56 -04:00
|
|
|
callback(null, lockValue)
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
2017-05-09 09:06:09 -04:00
|
|
|
setTimeout attempt, testInterval
|
|
|
|
# back off when the lock is taken to avoid overloading
|
2017-05-16 11:31:28 -04:00
|
|
|
testInterval = Math.min(testInterval * 2, LockManager.MAX_TEST_INTERVAL)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
checkLock: (doc_id, callback = (err, isFree)->)->
|
2016-04-13 06:59:56 -04:00
|
|
|
key = keys.blockingKey(doc_id:doc_id)
|
|
|
|
rclient.exists key, (err, exists) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(err) if err?
|
2016-04-13 06:59:56 -04:00
|
|
|
exists = parseInt exists
|
2014-02-12 05:40:42 -05:00
|
|
|
if exists == 1
|
|
|
|
metrics.inc "doc-blocking"
|
2017-07-12 05:47:23 -04:00
|
|
|
callback null, false
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
|
|
|
metrics.inc "doc-not-blocking"
|
2017-07-12 05:47:23 -04:00
|
|
|
callback null, true
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-04-13 06:59:56 -04:00
|
|
|
releaseLock: (doc_id, lockValue, callback)->
|
|
|
|
key = keys.blockingKey(doc_id:doc_id)
|
2017-05-18 10:04:12 -04:00
|
|
|
profile = new Profiler("releaseLock", {doc_id, key, lockValue})
|
2017-03-30 10:31:34 -04:00
|
|
|
rclient.eval LockManager.unlockScript, 1, key, lockValue, (err, result) ->
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
2017-05-17 11:11:48 -04:00
|
|
|
else if result? and result isnt 1 # successful unlock should release exactly one key
|
2017-05-18 10:04:12 -04:00
|
|
|
profile.log("unlockScript:expired-lock").end()
|
2017-05-17 11:11:48 -04:00
|
|
|
logger.error {doc_id:doc_id, key:key, lockValue:lockValue, redis_err:err, redis_result:result}, "unlocking error"
|
2017-06-05 11:29:58 -04:00
|
|
|
metrics.inc "unlock-error"
|
2017-03-30 10:31:34 -04:00
|
|
|
return callback(new Error("tried to release timed out lock"))
|
2017-05-17 11:11:48 -04:00
|
|
|
else
|
2017-05-18 10:04:12 -04:00
|
|
|
profile.log("unlockScript:ok").end()
|
2017-07-12 05:47:23 -04:00
|
|
|
callback(null,result)
|