2020-02-17 17:34:21 +00:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 17:34:04 +00: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 17:34:28 +00:00
|
|
|
let LockManager
|
2021-07-12 16:47:16 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-11-10 11:32:05 +00:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-02-17 17:34:28 +00:00
|
|
|
const rclient = redis.createClient(Settings.redis.lock)
|
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2015-10-08 13:20:36 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
const HOST = os.hostname()
|
|
|
|
const PID = process.pid
|
|
|
|
const RND = crypto.randomBytes(4).toString('hex')
|
|
|
|
let COUNT = 0
|
2014-02-26 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00: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 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00: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 13:20:36 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
unlockScript:
|
|
|
|
'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end',
|
2015-10-08 13:20:36 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
tryLock(key, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-02-17 17:34:28 +00:00
|
|
|
}
|
|
|
|
const lockValue = LockManager.randomLock()
|
2021-07-13 11:04:43 +00: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 17:34:28 +00:00
|
|
|
}
|
2021-07-13 11:04:43 +00:00
|
|
|
)
|
2020-02-17 17:34:28 +00:00
|
|
|
},
|
2014-02-26 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
getLock(key, callback) {
|
|
|
|
let attempt
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-02-17 17:34:28 +00:00
|
|
|
}
|
|
|
|
const startTime = Date.now()
|
2020-06-04 08:24:21 +00:00
|
|
|
return (attempt = function () {
|
2020-02-17 17:34:28 +00:00
|
|
|
if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
|
|
|
|
const e = new Error('Timeout')
|
|
|
|
e.key = key
|
|
|
|
return callback(e)
|
|
|
|
}
|
2014-02-26 10:55:20 +00:00
|
|
|
|
2020-06-04 08:24:21 +00:00
|
|
|
return LockManager.tryLock(key, function (error, gotLock, lockValue) {
|
2020-02-17 17:34:28 +00:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (gotLock) {
|
|
|
|
return callback(null, lockValue)
|
|
|
|
} else {
|
|
|
|
return setTimeout(attempt, LockManager.LOCK_TEST_INTERVAL)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})()
|
|
|
|
},
|
2014-02-26 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
checkLock(key, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-02-17 17:34:28 +00:00
|
|
|
}
|
2020-06-04 08:24:21 +00:00
|
|
|
return rclient.exists(key, function (err, exists) {
|
2020-02-17 17:34:28 +00: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 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
releaseLock(key, lockValue, callback) {
|
2021-07-13 11:04:43 +00: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(
|
2023-03-21 12:21:51 +00:00
|
|
|
{ key, lockValue, redisErr: err, redisResult: result },
|
2021-07-13 11:04:43 +00:00
|
|
|
'unlocking error'
|
|
|
|
)
|
|
|
|
return callback(new Error('tried to release timed out lock'))
|
|
|
|
}
|
|
|
|
return callback(err, result)
|
2020-02-17 17:34:28 +00:00
|
|
|
}
|
2021-07-13 11:04:43 +00:00
|
|
|
)
|
2020-02-17 17:34:28 +00:00
|
|
|
},
|
2014-02-26 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
runWithLock(key, runner, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-02-17 17:34:28 +00:00
|
|
|
}
|
2020-06-04 08:24:21 +00:00
|
|
|
return LockManager.getLock(key, function (error, lockValue) {
|
2020-02-17 17:34:28 +00:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 11:04:43 +00:00
|
|
|
return runner(error1 =>
|
2020-06-04 08:24:21 +00:00
|
|
|
LockManager.releaseLock(key, lockValue, function (error2) {
|
2020-02-17 17:34:28 +00:00
|
|
|
error = error1 || error2
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2014-02-26 10:55:20 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
healthCheck(callback) {
|
2021-07-13 11:04:43 +00:00
|
|
|
const action = releaseLock => releaseLock()
|
2020-02-17 17:34:28 +00:00
|
|
|
return LockManager.runWithLock(
|
|
|
|
`HistoryLock:HealthCheck:host=${HOST}:pid=${PID}:random=${RND}`,
|
|
|
|
action,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2016-01-06 09:34:39 +00:00
|
|
|
|
2020-02-17 17:34:28 +00:00
|
|
|
close(callback) {
|
|
|
|
rclient.quit()
|
|
|
|
return rclient.once('end', callback)
|
2021-07-13 11:04:43 +00:00
|
|
|
},
|
2020-02-17 17:34:28 +00:00
|
|
|
}
|