2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// 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:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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 DeleteQueueManager
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-05-06 06:09:33 -04:00
|
|
|
const RedisManager = require('./RedisManager')
|
|
|
|
const ProjectManager = require('./ProjectManager')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
const metrics = require('./Metrics')
|
2019-09-25 11:42:49 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
// Maintain a sorted set of project flushAndDelete requests, ordered by timestamp
|
|
|
|
// (ZADD), and process them from oldest to newest. A flushAndDelete request comes
|
2020-05-06 06:09:33 -04:00
|
|
|
// from real-time and is triggered when a user leaves a project.
|
2020-05-06 06:08:21 -04:00
|
|
|
//
|
|
|
|
// The aim is to remove the project from redis 5 minutes after the last request
|
|
|
|
// if there has been no activity (document updates) in that time. If there is
|
|
|
|
// activity we can expect a further flushAndDelete request when the editing user
|
2020-05-06 06:09:33 -04:00
|
|
|
// leaves the project.
|
2020-05-06 06:08:21 -04:00
|
|
|
//
|
|
|
|
// If a new flushAndDelete request comes in while an existing request is already
|
|
|
|
// in the queue we update the timestamp as we can postpone flushing further.
|
|
|
|
//
|
|
|
|
// Documents are processed by checking the queue, seeing if the first entry is
|
|
|
|
// older than 5 minutes, and popping it from the queue in that case.
|
2019-09-26 05:14:49 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = DeleteQueueManager = {
|
|
|
|
flushAndDeleteOldProjects(options, callback) {
|
|
|
|
const startTime = Date.now()
|
|
|
|
const cutoffTime =
|
|
|
|
startTime - options.min_delete_age + 100 * (Math.random() - 0.5)
|
|
|
|
let count = 0
|
2019-09-25 11:42:49 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
const flushProjectIfNotModified = (projectId, flushTimestamp, cb) =>
|
2021-07-13 07:04:42 -04:00
|
|
|
ProjectManager.getProjectDocsTimestamps(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
2021-07-13 07:04:42 -04:00
|
|
|
function (err, timestamps) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (timestamps.length === 0) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId },
|
2021-07-13 07:04:42 -04:00
|
|
|
'skipping flush of queued project - no timestamps'
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
|
|
|
return cb()
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
// are any of the timestamps newer than the time the project was flushed?
|
|
|
|
for (const timestamp of Array.from(timestamps)) {
|
|
|
|
if (timestamp > flushTimestamp) {
|
|
|
|
metrics.inc('queued-delete-skipped')
|
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId, timestamps, flushTimestamp },
|
2021-07-13 07:04:42 -04:00
|
|
|
'found newer timestamp, will skip delete'
|
|
|
|
)
|
|
|
|
return cb()
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ projectId, flushTimestamp }, 'flushing queued project')
|
2021-07-13 07:04:42 -04:00
|
|
|
return ProjectManager.flushAndDeleteProjectWithLocks(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
2021-07-13 07:04:42 -04:00
|
|
|
{ skip_history_flush: false },
|
|
|
|
function (err) {
|
|
|
|
if (err != null) {
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.err({ projectId, err }, 'error flushing queued project')
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
|
|
|
metrics.inc('queued-delete-completed')
|
|
|
|
return cb(null, true)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-09-25 11:42:49 -04:00
|
|
|
|
2021-10-26 04:08:56 -04:00
|
|
|
function flushNextProject() {
|
2020-05-06 06:09:33 -04:00
|
|
|
const now = Date.now()
|
|
|
|
if (now - startTime > options.timeout) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug('hit time limit on flushing old projects')
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(null, count)
|
|
|
|
}
|
|
|
|
if (count > options.limit) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug('hit count limit on flushing old projects')
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(null, count)
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
return RedisManager.getNextProjectToFlushAndDelete(
|
|
|
|
cutoffTime,
|
2023-03-21 08:06:13 -04:00
|
|
|
function (err, projectId, flushTimestamp, queueLength) {
|
2021-07-13 07:04:42 -04:00
|
|
|
if (err != null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
return callback(err, count)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
if (projectId == null) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(null, count)
|
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ projectId, queueLength }, 'flushing queued project')
|
2021-07-13 07:04:42 -04:00
|
|
|
metrics.globalGauge('queued-flush-backlog', queueLength)
|
|
|
|
return flushProjectIfNotModified(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
2021-07-13 07:04:42 -04:00
|
|
|
flushTimestamp,
|
|
|
|
function (err, flushed) {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) {
|
|
|
|
// Do not stop processing the queue in case the flush fails.
|
|
|
|
// Slowing down the processing can fill up redis.
|
|
|
|
metrics.inc('queued-delete-error')
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
if (flushed) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return flushNextProject()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2019-09-25 11:42:49 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return flushNextProject()
|
|
|
|
},
|
2019-09-30 10:35:05 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
startBackgroundFlush() {
|
|
|
|
const SHORT_DELAY = 10
|
|
|
|
const LONG_DELAY = 1000
|
2021-10-26 04:08:56 -04:00
|
|
|
function doFlush() {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (Settings.shuttingDown) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.info('discontinuing background flush due to shutdown')
|
2020-05-06 06:09:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return DeleteQueueManager.flushAndDeleteOldProjects(
|
|
|
|
{
|
|
|
|
timeout: 1000,
|
|
|
|
min_delete_age: 3 * 60 * 1000,
|
2021-07-13 07:04:42 -04:00
|
|
|
limit: 1000, // high value, to ensure we always flush enough projects
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2021-10-27 05:49:18 -04:00
|
|
|
(_err, flushed) =>
|
2020-05-06 06:09:33 -04:00
|
|
|
setTimeout(doFlush, flushed > 10 ? SHORT_DELAY : LONG_DELAY)
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return doFlush()
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|