2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2014-11-25 10:26:33 -05:00
|
|
|
Settings = require('settings-sharelatex')
|
2017-05-04 10:22:54 -04:00
|
|
|
RedisWrapper = require("./RedisWrapper")
|
|
|
|
rclient = RedisWrapper.client("lock")
|
2014-11-25 10:26:33 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
module.exports = LockManager =
|
|
|
|
LOCK_TEST_INTERVAL: 50 # 50ms between each test of the lock
|
|
|
|
MAX_LOCK_WAIT_TIME: 10000 # 10s maximum time to spend trying to get the lock
|
2018-02-19 06:23:43 -05:00
|
|
|
REDIS_LOCK_EXPIRY: 30 # seconds. Time until lock auto expires in redis
|
|
|
|
SLOW_EXECUTION_THRESHOLD: 5000 # 5s, if execution takes longer than this then log
|
2014-11-25 10:26:33 -05:00
|
|
|
|
2018-02-19 06:23:43 -05:00
|
|
|
runWithLock: (namespace, id, runner = ( (releaseLock = (error) ->) -> ), callback = ( (error) -> )) ->
|
|
|
|
|
|
|
|
# The lock can expire in redis but the process carry on. This setTimout call
|
|
|
|
# is designed to log if this happens.
|
|
|
|
#
|
|
|
|
# error is defined here so we get a useful stacktrace
|
|
|
|
lockReleased = false
|
|
|
|
slowExecutionError = new Error "slow execution during lock"
|
|
|
|
countIfExceededLockTimeout = () ->
|
|
|
|
if !lockReleased
|
|
|
|
metrics.inc "lock.#{namespace}.exceeded_lock_timeout"
|
|
|
|
logger.log "exceeded lock timeout", { namespace, id, slowExecutionError }
|
|
|
|
|
2018-02-20 05:26:24 -05:00
|
|
|
setTimeout countIfExceededLockTimeout, LockManager.REDIS_LOCK_EXPIRY * 1000
|
2018-02-19 06:23:43 -05:00
|
|
|
|
|
|
|
timer = new metrics.Timer("lock.#{namespace}")
|
|
|
|
key = "lock:web:#{namespace}:#{id}"
|
2018-02-01 10:31:42 -05:00
|
|
|
LockManager._getLock key, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
runner (error1, values...) ->
|
|
|
|
LockManager._releaseLock key, (error2) ->
|
2018-02-19 06:23:43 -05:00
|
|
|
lockReleased = true
|
|
|
|
timeTaken = new Date - timer.start
|
|
|
|
if timeTaken > LockManager.SLOW_EXECUTION_THRESHOLD
|
|
|
|
logger.log "slow execution during lock", { namespace, id, timeTaken, slowExecutionError }
|
|
|
|
|
|
|
|
timer.done()
|
2018-02-01 10:31:42 -05:00
|
|
|
error = error1 or error2
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, values...
|
|
|
|
|
|
|
|
_tryLock : (key, callback = (err, isFree)->)->
|
|
|
|
rclient.set key, "locked", "EX", LockManager.REDIS_LOCK_EXPIRY, "NX", (err, gotLock)->
|
2014-11-25 10:26:33 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
if gotLock == "OK"
|
2017-05-11 10:27:01 -04:00
|
|
|
metrics.inc "lock-not-blocking"
|
2014-11-25 10:26:33 -05:00
|
|
|
callback err, true
|
|
|
|
else
|
2017-05-11 10:27:01 -04:00
|
|
|
metrics.inc "lock-blocking"
|
|
|
|
logger.log key: key, redis_response: gotLock, "lock is locked"
|
2014-11-25 10:26:33 -05:00
|
|
|
callback err, false
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
_getLock: (key, callback = (error) ->) ->
|
2014-11-25 10:26:33 -05:00
|
|
|
startTime = Date.now()
|
|
|
|
do attempt = () ->
|
|
|
|
if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
|
|
|
|
return callback(new Error("Timeout"))
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
LockManager._tryLock key, (error, gotLock) ->
|
2014-11-25 10:26:33 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
if gotLock
|
|
|
|
callback(null)
|
|
|
|
else
|
|
|
|
setTimeout attempt, LockManager.LOCK_TEST_INTERVAL
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
_checkLock: (key, callback = (err, isFree)->)->
|
2014-11-25 10:26:33 -05:00
|
|
|
multi = rclient.multi()
|
2018-02-01 10:31:42 -05:00
|
|
|
multi.exists key
|
2014-11-25 10:26:33 -05:00
|
|
|
multi.exec (err, replys)->
|
|
|
|
return callback(err) if err?
|
|
|
|
exists = parseInt replys[0]
|
|
|
|
if exists == 1
|
2017-05-11 10:27:01 -04:00
|
|
|
metrics.inc "lock-blocking"
|
2014-11-25 10:26:33 -05:00
|
|
|
callback err, false
|
|
|
|
else
|
2017-05-11 10:27:01 -04:00
|
|
|
metrics.inc "lock-not-blocking"
|
2014-11-25 10:26:33 -05:00
|
|
|
callback err, true
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
_releaseLock: (key, callback)->
|
|
|
|
rclient.del key, callback
|