2019-11-12 03:56:58 -05:00
|
|
|
const { callbackify, promisify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
const RedisWrapper = require('./RedisWrapper')
|
|
|
|
const rclient = RedisWrapper.client('lock')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const async = require('async')
|
2020-06-01 04:37:34 -04:00
|
|
|
const settings = require('settings-sharelatex')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
const HOST = os.hostname()
|
|
|
|
const PID = process.pid
|
|
|
|
const RND = crypto.randomBytes(4).toString('hex')
|
|
|
|
let COUNT = 0
|
|
|
|
|
|
|
|
const LOCK_QUEUES = new Map() // queue lock requests for each name/id so they get the lock on a first-come first-served basis
|
|
|
|
|
2020-06-01 04:37:34 -04:00
|
|
|
logger.log(
|
|
|
|
{ lockManagerSettings: settings.lockManager },
|
|
|
|
'LockManager initialising'
|
|
|
|
)
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
const LockManager = {
|
2020-06-01 04:37:34 -04:00
|
|
|
// ms between each test of the lock
|
|
|
|
LOCK_TEST_INTERVAL: settings.lockManager.lockTestInterval || 50,
|
|
|
|
// back off to ms between each test of the lock
|
|
|
|
MAX_TEST_INTERVAL: settings.lockManager.maxTestInterval || 1000,
|
|
|
|
// ms maximum time to spend trying to get the lock
|
|
|
|
MAX_LOCK_WAIT_TIME: settings.lockManager.maxLockWaitTime || 10000,
|
|
|
|
// seconds. Time until lock auto expires in redis
|
|
|
|
REDIS_LOCK_EXPIRY: settings.lockManager.redisLockExpiry || 30,
|
|
|
|
// ms, if execution takes longer than this then log
|
|
|
|
SLOW_EXECUTION_THRESHOLD: settings.lockManager.slowExecutionThreshold || 5000,
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
// 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() {
|
|
|
|
const time = Date.now()
|
|
|
|
return `locked:host=${HOST}:pid=${PID}:random=${RND}:time=${time}:count=${COUNT++}`
|
|
|
|
},
|
|
|
|
|
|
|
|
unlockScript:
|
|
|
|
'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end',
|
|
|
|
|
|
|
|
runWithLock(namespace, id, runner, callback) {
|
|
|
|
// runner must be a function accepting a callback, e.g. runner = (cb) ->
|
|
|
|
|
|
|
|
// This error is defined here so we get a useful stacktrace
|
|
|
|
const slowExecutionError = new Error('slow execution during lock')
|
|
|
|
|
|
|
|
const timer = new metrics.Timer(`lock.${namespace}`)
|
|
|
|
const key = `lock:web:${namespace}:${id}`
|
2019-11-04 04:50:15 -05:00
|
|
|
LockManager._getLock(key, namespace, (error, lockValue) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
// The lock can expire in redis but the process carry on. This setTimeout call
|
2019-05-29 05:21:06 -04:00
|
|
|
// is designed to log if this happens.
|
2019-11-04 04:50:15 -05:00
|
|
|
function countIfExceededLockTimeout() {
|
2019-05-29 05:21:06 -04:00
|
|
|
metrics.inc(`lock.${namespace}.exceeded_lock_timeout`)
|
2019-11-04 04:50:15 -05:00
|
|
|
logger.log('exceeded lock timeout', {
|
2019-05-29 05:21:06 -04:00
|
|
|
namespace,
|
|
|
|
id,
|
|
|
|
slowExecutionError
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const exceededLockTimeout = setTimeout(
|
|
|
|
countIfExceededLockTimeout,
|
|
|
|
LockManager.REDIS_LOCK_EXPIRY * 1000
|
|
|
|
)
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
runner((error1, ...values) =>
|
|
|
|
LockManager._releaseLock(key, lockValue, error2 => {
|
2019-05-29 05:21:06 -04:00
|
|
|
clearTimeout(exceededLockTimeout)
|
|
|
|
|
|
|
|
const timeTaken = new Date() - timer.start
|
|
|
|
if (timeTaken > LockManager.SLOW_EXECUTION_THRESHOLD) {
|
|
|
|
logger.log('slow execution during lock', {
|
|
|
|
namespace,
|
|
|
|
id,
|
|
|
|
timeTaken,
|
|
|
|
slowExecutionError
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.done()
|
|
|
|
error = error1 || error2
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(null, ...values)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_tryLock(key, namespace, callback) {
|
|
|
|
const lockValue = LockManager.randomLock()
|
2019-07-02 05:05:08 -04:00
|
|
|
rclient.set(
|
2019-05-29 05:21:06 -04:00
|
|
|
key,
|
|
|
|
lockValue,
|
|
|
|
'EX',
|
|
|
|
LockManager.REDIS_LOCK_EXPIRY,
|
|
|
|
'NX',
|
2019-11-04 04:50:15 -05:00
|
|
|
(err, gotLock) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (gotLock === 'OK') {
|
|
|
|
metrics.inc(`lock.${namespace}.try.success`)
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(err, true, lockValue)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
metrics.inc(`lock.${namespace}.try.failed`)
|
|
|
|
logger.log({ key, redis_response: gotLock }, 'lock is locked')
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(err, false)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
// it's sufficient to serialize within a process because that is where the parallel operations occur
|
|
|
|
_getLock(key, namespace, callback) {
|
|
|
|
// this is what we need to do for each lock we want to request
|
|
|
|
const task = next =>
|
2019-11-04 04:50:15 -05:00
|
|
|
LockManager._getLockByPolling(key, namespace, (error, lockValue) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
// tell the queue to start trying to get the next lock (if any)
|
|
|
|
next()
|
|
|
|
// we have got a lock result, so we can continue with our own execution
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(error, lockValue)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
// create a queue for this key if needed
|
|
|
|
const queueName = `${key}:${namespace}`
|
|
|
|
let queue = LOCK_QUEUES.get(queueName)
|
|
|
|
if (queue == null) {
|
|
|
|
const handler = (fn, cb) => fn(cb)
|
|
|
|
// set up a new queue for this key
|
|
|
|
queue = async.queue(handler, 1)
|
|
|
|
queue.push(task)
|
|
|
|
// remove the queue object when queue is empty
|
|
|
|
queue.drain = () => LOCK_QUEUES.delete(queueName)
|
|
|
|
// store the queue in our global map
|
2019-11-04 04:50:15 -05:00
|
|
|
LOCK_QUEUES.set(queueName, queue)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
// queue the request to get the lock
|
2019-11-04 04:50:15 -05:00
|
|
|
queue.push(task)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getLockByPolling(key, namespace, callback) {
|
|
|
|
const startTime = Date.now()
|
|
|
|
const testInterval = LockManager.LOCK_TEST_INTERVAL
|
|
|
|
let attempts = 0
|
2019-11-04 04:50:15 -05:00
|
|
|
function attempt() {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
|
|
|
|
metrics.inc(`lock.${namespace}.get.failed`)
|
|
|
|
return callback(new Error('Timeout'))
|
|
|
|
}
|
|
|
|
|
|
|
|
attempts += 1
|
2019-11-04 04:50:15 -05:00
|
|
|
LockManager._tryLock(key, namespace, (error, gotLock, lockValue) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (gotLock) {
|
|
|
|
metrics.gauge(`lock.${namespace}.get.success.tries`, attempts)
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(null, lockValue)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
setTimeout(attempt, testInterval)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
2019-11-04 04:50:15 -05:00
|
|
|
}
|
|
|
|
attempt()
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_releaseLock(key, lockValue, callback) {
|
2019-11-04 04:50:15 -05:00
|
|
|
rclient.eval(LockManager.unlockScript, 1, key, lockValue, (err, result) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (result != null && result !== 1) {
|
|
|
|
// successful unlock should release exactly one key
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ key, lockValue, redis_err: err, redis_result: result },
|
|
|
|
'unlocking error'
|
|
|
|
)
|
|
|
|
metrics.inc('unlock-error')
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(new Error('tried to release timed out lock'))
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-11-04 04:50:15 -05:00
|
|
|
callback(null, result)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
|
|
|
|
module.exports = LockManager
|
2019-11-12 03:56:58 -05:00
|
|
|
|
|
|
|
const promisifiedRunWithLock = promisify(LockManager.runWithLock)
|
|
|
|
LockManager.promises = {
|
|
|
|
runWithLock(namespace, id, runner) {
|
|
|
|
const cbRunner = callbackify(runner)
|
|
|
|
return promisifiedRunWithLock(namespace, id, cbRunner)
|
|
|
|
}
|
|
|
|
}
|