2020-02-17 12:34:21 -05:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:04 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:34:28 -05:00
|
|
|
let LockManager
|
2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-11-10 06:32:05 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-02-17 12:34:28 -05:00
|
|
|
const rclient = redis.createClient(Settings.redis.lock)
|
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2015-10-08 09:20:36 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
const HOST = os.hostname()
|
|
|
|
const PID = process.pid
|
|
|
|
const RND = crypto.randomBytes(4).toString('hex')
|
|
|
|
let COUNT = 0
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
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
|
|
|
|
LOCK_TTL: 300, // seconds (allow 5 minutes for any operation to complete)
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05: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++}`
|
|
|
|
},
|
2015-10-08 09:20:36 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
unlockScript:
|
|
|
|
'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end',
|
2015-10-08 09:20:36 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
tryLock(key, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const lockValue = LockManager.randomLock()
|
2021-07-13 07:04:43 -04:00
|
|
|
return rclient.set(
|
|
|
|
key,
|
|
|
|
lockValue,
|
|
|
|
'EX',
|
|
|
|
this.LOCK_TTL,
|
|
|
|
'NX',
|
|
|
|
function (err, gotLock) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (gotLock === 'OK') {
|
|
|
|
return callback(err, true, lockValue)
|
|
|
|
} else {
|
|
|
|
return callback(err, false)
|
|
|
|
}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
getLock(key, callback) {
|
|
|
|
let attempt
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const startTime = Date.now()
|
2020-06-04 04:24:21 -04:00
|
|
|
return (attempt = function () {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
|
|
|
|
const e = new Error('Timeout')
|
|
|
|
e.key = key
|
|
|
|
return callback(e)
|
|
|
|
}
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return LockManager.tryLock(key, function (error, gotLock, lockValue) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (gotLock) {
|
|
|
|
return callback(null, lockValue)
|
|
|
|
} else {
|
|
|
|
return setTimeout(attempt, LockManager.LOCK_TEST_INTERVAL)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})()
|
|
|
|
},
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
checkLock(key, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-06-04 04:24:21 -04:00
|
|
|
return rclient.exists(key, function (err, exists) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
exists = parseInt(exists)
|
|
|
|
if (exists === 1) {
|
|
|
|
return callback(err, false)
|
|
|
|
} else {
|
|
|
|
return callback(err, true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
releaseLock(key, lockValue, callback) {
|
2021-07-13 07:04:43 -04:00
|
|
|
return rclient.eval(
|
|
|
|
LockManager.unlockScript,
|
|
|
|
1,
|
|
|
|
key,
|
|
|
|
lockValue,
|
|
|
|
function (err, result) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (result != null && result !== 1) {
|
|
|
|
// successful unlock should release exactly one key
|
|
|
|
logger.error(
|
|
|
|
{ key, lockValue, redis_err: err, redis_result: result },
|
|
|
|
'unlocking error'
|
|
|
|
)
|
|
|
|
return callback(new Error('tried to release timed out lock'))
|
|
|
|
}
|
|
|
|
return callback(err, result)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
runWithLock(key, runner, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-06-04 04:24:21 -04:00
|
|
|
return LockManager.getLock(key, function (error, lockValue) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
return runner(error1 =>
|
2020-06-04 04:24:21 -04:00
|
|
|
LockManager.releaseLock(key, lockValue, function (error2) {
|
2020-02-17 12:34:28 -05:00
|
|
|
error = error1 || error2
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2014-02-26 05:55:20 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
healthCheck(callback) {
|
2021-07-13 07:04:43 -04:00
|
|
|
const action = releaseLock => releaseLock()
|
2020-02-17 12:34:28 -05:00
|
|
|
return LockManager.runWithLock(
|
|
|
|
`HistoryLock:HealthCheck:host=${HOST}:pid=${PID}:random=${RND}`,
|
|
|
|
action,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2016-01-06 04:34:39 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
close(callback) {
|
|
|
|
rclient.quit()
|
|
|
|
return rclient.once('end', callback)
|
2021-07-13 07:04:43 -04:00
|
|
|
},
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|