2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04: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-05-06 06:09:33 -04:00
|
|
|
let LockManager
|
|
|
|
const metrics = require('./Metrics')
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-11-10 06:32:04 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-05-06 06:09:33 -04:00
|
|
|
const rclient = redis.createClient(Settings.redis.lock)
|
|
|
|
const keys = Settings.redis.lock.key_schema
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
2016-04-13 06:59:56 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const Profiler = require('./Profiler')
|
2017-05-18 10:04:12 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const HOST = os.hostname()
|
|
|
|
const PID = process.pid
|
|
|
|
const RND = crypto.randomBytes(4).toString('hex')
|
|
|
|
let COUNT = 0
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const MAX_REDIS_REQUEST_LENGTH = 5000 // 5 seconds
|
2017-07-11 09:25:59 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = LockManager = {
|
|
|
|
LOCK_TEST_INTERVAL: 50, // 50ms between each test of the lock
|
|
|
|
MAX_TEST_INTERVAL: 1000, // back off to 1s between each test of the lock
|
|
|
|
MAX_LOCK_WAIT_TIME: 10000, // 10s maximum time to spend trying to get the lock
|
|
|
|
LOCK_TTL: 30, // seconds. Time until lock auto expires in redis.
|
2016-04-13 06:59:56 -04:00
|
|
|
|
2020-05-06 06:09:33 -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++}`
|
|
|
|
},
|
2016-04-13 06:59:56 -04:00
|
|
|
|
2020-05-06 06:09:33 -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
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
tryLock(doc_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const lockValue = LockManager.randomLock()
|
|
|
|
const key = keys.blockingKey({ doc_id })
|
|
|
|
const profile = new Profiler('tryLock', { doc_id, key, lockValue })
|
2021-07-13 07:04:42 -04:00
|
|
|
return rclient.set(
|
|
|
|
key,
|
|
|
|
lockValue,
|
|
|
|
'EX',
|
|
|
|
this.LOCK_TTL,
|
|
|
|
'NX',
|
|
|
|
function (err, gotLock) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (gotLock === 'OK') {
|
|
|
|
metrics.inc('doc-not-blocking')
|
|
|
|
const timeTaken = profile.log('got lock').end()
|
|
|
|
if (timeTaken > MAX_REDIS_REQUEST_LENGTH) {
|
|
|
|
// took too long, so try to free the lock
|
|
|
|
return LockManager.releaseLock(
|
|
|
|
doc_id,
|
|
|
|
lockValue,
|
|
|
|
function (err, result) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
} // error freeing lock
|
|
|
|
return callback(null, false)
|
|
|
|
}
|
|
|
|
) // tell caller they didn't get the lock
|
|
|
|
} else {
|
|
|
|
return callback(null, true, lockValue)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
} else {
|
2021-07-13 07:04:42 -04:00
|
|
|
metrics.inc('doc-blocking')
|
|
|
|
profile.log('doc is locked').end()
|
|
|
|
return callback(null, false)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
getLock(doc_id, callback) {
|
|
|
|
let attempt
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const startTime = Date.now()
|
|
|
|
let testInterval = LockManager.LOCK_TEST_INTERVAL
|
|
|
|
const profile = new Profiler('getLock', { doc_id })
|
|
|
|
return (attempt = function () {
|
|
|
|
if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
|
|
|
|
const e = new Error('Timeout')
|
|
|
|
e.doc_id = doc_id
|
|
|
|
profile.log('timeout').end()
|
|
|
|
return callback(e)
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return LockManager.tryLock(doc_id, function (error, gotLock, lockValue) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
profile.log('tryLock')
|
|
|
|
if (gotLock) {
|
|
|
|
profile.end()
|
|
|
|
return callback(null, lockValue)
|
|
|
|
} else {
|
|
|
|
setTimeout(attempt, testInterval)
|
|
|
|
// back off when the lock is taken to avoid overloading
|
|
|
|
return (testInterval = Math.min(
|
|
|
|
testInterval * 2,
|
|
|
|
LockManager.MAX_TEST_INTERVAL
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})()
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
checkLock(doc_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const key = keys.blockingKey({ doc_id })
|
|
|
|
return rclient.exists(key, function (err, exists) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
exists = parseInt(exists)
|
|
|
|
if (exists === 1) {
|
|
|
|
metrics.inc('doc-blocking')
|
|
|
|
return callback(null, false)
|
|
|
|
} else {
|
|
|
|
metrics.inc('doc-not-blocking')
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
releaseLock(doc_id, lockValue, callback) {
|
|
|
|
const key = keys.blockingKey({ doc_id })
|
|
|
|
const profile = new Profiler('releaseLock', { doc_id, key, lockValue })
|
2021-07-13 07:04:42 -04:00
|
|
|
return rclient.eval(
|
|
|
|
LockManager.unlockScript,
|
|
|
|
1,
|
|
|
|
key,
|
|
|
|
lockValue,
|
|
|
|
function (err, result) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
} else if (result != null && result !== 1) {
|
|
|
|
// successful unlock should release exactly one key
|
|
|
|
profile.log('unlockScript:expired-lock').end()
|
|
|
|
logger.error(
|
|
|
|
{ doc_id, key, lockValue, redis_err: err, redis_result: result },
|
|
|
|
'unlocking error'
|
|
|
|
)
|
|
|
|
metrics.inc('unlock-error')
|
|
|
|
return callback(new Error('tried to release timed out lock'))
|
|
|
|
} else {
|
|
|
|
profile.log('unlockScript:ok').end()
|
|
|
|
return callback(null, result)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|