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 ShareJsDB
|
2024-07-18 09:01:09 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-05-06 06:09:33 -04:00
|
|
|
const Keys = require('./UpdateKeys')
|
|
|
|
const RedisManager = require('./RedisManager')
|
|
|
|
const Errors = require('./Errors')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2024-07-23 05:36:52 -04:00
|
|
|
const TRANSFORM_UPDATES_COUNT_BUCKETS = [
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 50, 75, 100,
|
|
|
|
// prepare buckets for full-project history/larger buffer experiments
|
|
|
|
150, 200, 300, 400,
|
|
|
|
]
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = ShareJsDB = class ShareJsDB {
|
2023-03-21 08:06:13 -04:00
|
|
|
constructor(projectId, docId, lines, version) {
|
|
|
|
this.project_id = projectId
|
|
|
|
this.doc_id = docId
|
2020-05-06 06:09:33 -04:00
|
|
|
this.lines = lines
|
|
|
|
this.version = version
|
|
|
|
this.appliedOps = {}
|
|
|
|
// ShareJS calls this detacted from the instance, so we need
|
|
|
|
// bind it to keep our context that can access @appliedOps
|
|
|
|
this.writeOp = this._writeOp.bind(this)
|
2024-07-18 09:01:09 -04:00
|
|
|
this.startTimeShareJsDB = performance.now()
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
getOps(docKey, start, end, callback) {
|
2024-07-23 05:36:52 -04:00
|
|
|
if (start === end || (start === this.version && end === null)) {
|
|
|
|
const status = 'is-up-to-date'
|
2024-07-18 09:01:09 -04:00
|
|
|
Metrics.inc('transform-updates', 1, {
|
2024-07-23 05:36:52 -04:00
|
|
|
status,
|
2024-07-18 09:01:09 -04:00
|
|
|
path: 'sharejs',
|
|
|
|
})
|
2024-07-23 05:36:52 -04:00
|
|
|
Metrics.histogram(
|
|
|
|
'transform-updates.count',
|
|
|
|
0,
|
|
|
|
TRANSFORM_UPDATES_COUNT_BUCKETS,
|
|
|
|
{ path: 'sharejs', status }
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(null, [])
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
// In redis, lrange values are inclusive.
|
|
|
|
if (end != null) {
|
|
|
|
end--
|
|
|
|
} else {
|
|
|
|
end = -1
|
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
const [projectId, docId] = Array.from(Keys.splitProjectIdAndDocId(docKey))
|
2024-07-18 09:01:09 -04:00
|
|
|
const timer = new Metrics.Timer(
|
|
|
|
'transform-updates.timing',
|
|
|
|
1,
|
|
|
|
{ path: 'sharejs' },
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 50, 100, 200, 500, 1000]
|
|
|
|
)
|
|
|
|
RedisManager.getPreviousDocOps(docId, start, end, (err, ops) => {
|
|
|
|
let status
|
|
|
|
if (err) {
|
|
|
|
if (err instanceof Errors.OpRangeNotAvailableError) {
|
|
|
|
status = 'out-of-range'
|
|
|
|
} else {
|
|
|
|
status = 'error'
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ops.length === 0) {
|
|
|
|
status = 'fetched-zero'
|
2024-07-23 05:36:52 -04:00
|
|
|
|
|
|
|
// The sharejs processing is happening under a lock.
|
|
|
|
// In case there are no other ops available, something bypassed the lock (or we overran it).
|
|
|
|
logger.warn(
|
|
|
|
{
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
timeSinceShareJsDBInit:
|
|
|
|
performance.now() - this.startTimeShareJsDB,
|
|
|
|
},
|
|
|
|
'found zero docOps while transforming update'
|
|
|
|
)
|
2024-07-18 09:01:09 -04:00
|
|
|
} else {
|
|
|
|
status = 'fetched'
|
|
|
|
}
|
|
|
|
Metrics.histogram(
|
|
|
|
'transform-updates.count',
|
|
|
|
ops.length,
|
2024-07-23 05:36:52 -04:00
|
|
|
TRANSFORM_UPDATES_COUNT_BUCKETS,
|
2024-07-18 09:01:09 -04:00
|
|
|
{ path: 'sharejs', status }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.done({ status })
|
|
|
|
Metrics.inc('transform-updates', 1, { status, path: 'sharejs' })
|
|
|
|
callback(err, ops)
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
_writeOp(docKey, opData, callback) {
|
|
|
|
if (this.appliedOps[docKey] == null) {
|
|
|
|
this.appliedOps[docKey] = []
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
this.appliedOps[docKey].push(opData)
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
getSnapshot(docKey, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
2023-03-21 08:06:13 -04:00
|
|
|
docKey !== Keys.combineProjectIdAndDocId(this.project_id, this.doc_id)
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
|
|
|
return callback(
|
|
|
|
new Errors.NotFoundError(
|
2023-03-21 08:06:13 -04:00
|
|
|
`unexpected doc_key ${docKey}, expected ${Keys.combineProjectIdAndDocId(
|
2020-05-06 06:09:33 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id
|
|
|
|
)}`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback(null, {
|
|
|
|
snapshot: this.lines.join('\n'),
|
|
|
|
v: parseInt(this.version, 10),
|
2021-07-13 07:04:42 -04:00
|
|
|
type: 'text',
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// To be able to remove a doc from the ShareJS memory
|
|
|
|
// we need to called Model::delete, which calls this
|
|
|
|
// method on the database. However, we will handle removing
|
|
|
|
// it from Redis ourselves
|
|
|
|
delete(docName, dbMeta, callback) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
}
|