DocumentManager.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. const { promisifyAll } = require('@overleaf/promise-utils')
  2. const RedisManager = require('./RedisManager')
  3. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  4. const PersistenceManager = require('./PersistenceManager')
  5. const DiffCodec = require('./DiffCodec')
  6. const logger = require('@overleaf/logger')
  7. const Metrics = require('./Metrics')
  8. const HistoryManager = require('./HistoryManager')
  9. const Errors = require('./Errors')
  10. const RangesManager = require('./RangesManager')
  11. const MAX_UNFLUSHED_AGE = 300 * 1000 // 5 mins, document should be flushed to mongo this time after a change
  12. const DocumentManager = {
  13. getDoc(projectId, docId, _callback) {
  14. const timer = new Metrics.Timer('docManager.getDoc')
  15. const callback = (...args) => {
  16. timer.done()
  17. _callback(...args)
  18. }
  19. RedisManager.getDoc(
  20. projectId,
  21. docId,
  22. (
  23. error,
  24. lines,
  25. version,
  26. ranges,
  27. pathname,
  28. projectHistoryId,
  29. unflushedTime,
  30. lastUpdatedAt,
  31. lastUpdatedBy,
  32. historyRangesSupport
  33. ) => {
  34. if (error) {
  35. return callback(error)
  36. }
  37. if (lines == null || version == null) {
  38. logger.debug(
  39. { projectId, docId },
  40. 'doc not in redis so getting from persistence API'
  41. )
  42. PersistenceManager.getDoc(
  43. projectId,
  44. docId,
  45. (
  46. error,
  47. lines,
  48. version,
  49. ranges,
  50. pathname,
  51. projectHistoryId,
  52. historyRangesSupport
  53. ) => {
  54. if (error) {
  55. return callback(error)
  56. }
  57. logger.debug(
  58. {
  59. projectId,
  60. docId,
  61. lines,
  62. version,
  63. pathname,
  64. projectHistoryId,
  65. historyRangesSupport,
  66. },
  67. 'got doc from persistence API'
  68. )
  69. RedisManager.putDocInMemory(
  70. projectId,
  71. docId,
  72. lines,
  73. version,
  74. ranges,
  75. pathname,
  76. projectHistoryId,
  77. historyRangesSupport,
  78. error => {
  79. if (error) {
  80. return callback(error)
  81. }
  82. callback(
  83. null,
  84. lines,
  85. version,
  86. ranges || {},
  87. pathname,
  88. projectHistoryId,
  89. null,
  90. false,
  91. historyRangesSupport
  92. )
  93. }
  94. )
  95. }
  96. )
  97. } else {
  98. callback(
  99. null,
  100. lines,
  101. version,
  102. ranges,
  103. pathname,
  104. projectHistoryId,
  105. unflushedTime,
  106. true,
  107. historyRangesSupport
  108. )
  109. }
  110. }
  111. )
  112. },
  113. getDocAndRecentOps(projectId, docId, fromVersion, _callback) {
  114. const timer = new Metrics.Timer('docManager.getDocAndRecentOps')
  115. const callback = (...args) => {
  116. timer.done()
  117. _callback(...args)
  118. }
  119. DocumentManager.getDoc(
  120. projectId,
  121. docId,
  122. (error, lines, version, ranges, pathname, projectHistoryId) => {
  123. if (error) {
  124. return callback(error)
  125. }
  126. if (fromVersion === -1) {
  127. callback(null, lines, version, [], ranges, pathname, projectHistoryId)
  128. } else {
  129. RedisManager.getPreviousDocOps(
  130. docId,
  131. fromVersion,
  132. version,
  133. (error, ops) => {
  134. if (error) {
  135. return callback(error)
  136. }
  137. callback(
  138. null,
  139. lines,
  140. version,
  141. ops,
  142. ranges,
  143. pathname,
  144. projectHistoryId
  145. )
  146. }
  147. )
  148. }
  149. }
  150. )
  151. },
  152. setDoc(projectId, docId, newLines, source, userId, undoing, _callback) {
  153. const timer = new Metrics.Timer('docManager.setDoc')
  154. const callback = (...args) => {
  155. timer.done()
  156. _callback(...args)
  157. }
  158. if (newLines == null) {
  159. return callback(new Error('No lines were provided to setDoc'))
  160. }
  161. const UpdateManager = require('./UpdateManager')
  162. DocumentManager.getDoc(
  163. projectId,
  164. docId,
  165. (
  166. error,
  167. oldLines,
  168. version,
  169. ranges,
  170. pathname,
  171. projectHistoryId,
  172. unflushedTime,
  173. alreadyLoaded
  174. ) => {
  175. if (error) {
  176. return callback(error)
  177. }
  178. if (
  179. oldLines != null &&
  180. oldLines.length > 0 &&
  181. oldLines[0].text != null
  182. ) {
  183. logger.debug(
  184. { docId, projectId, oldLines, newLines },
  185. 'document is JSON so not updating'
  186. )
  187. return callback(null)
  188. }
  189. logger.debug(
  190. { docId, projectId, oldLines, newLines },
  191. 'setting a document via http'
  192. )
  193. DiffCodec.diffAsShareJsOp(oldLines, newLines, (error, op) => {
  194. if (error) {
  195. return callback(error)
  196. }
  197. if (undoing) {
  198. for (const o of op || []) {
  199. o.u = true
  200. } // Turn on undo flag for each op for track changes
  201. }
  202. const update = {
  203. doc: docId,
  204. op,
  205. v: version,
  206. meta: {
  207. type: 'external',
  208. source,
  209. user_id: userId,
  210. },
  211. }
  212. // Keep track of external updates, whether they are for live documents
  213. // (flush) or unloaded documents (evict), and whether the update is a no-op.
  214. Metrics.inc('external-update', 1, {
  215. status: op.length > 0 ? 'diff' : 'noop',
  216. method: alreadyLoaded ? 'flush' : 'evict',
  217. path: source,
  218. })
  219. const applyUpdateIfNeeded = cb => {
  220. if (op.length === 0) {
  221. // Do not notify the frontend about a noop update.
  222. // We still want to execute the callback code below
  223. // to evict the doc if we loaded it into redis for
  224. // this update, otherwise the doc would never be
  225. // removed from redis.
  226. return cb(null)
  227. }
  228. UpdateManager.applyUpdate(projectId, docId, update, cb)
  229. }
  230. applyUpdateIfNeeded(error => {
  231. if (error) {
  232. return callback(error)
  233. }
  234. // If the document was loaded already, then someone has it open
  235. // in a project, and the usual flushing mechanism will happen.
  236. // Otherwise we should remove it immediately since nothing else
  237. // is using it.
  238. if (alreadyLoaded) {
  239. DocumentManager.flushDocIfLoaded(
  240. projectId,
  241. docId,
  242. (error, result) => {
  243. if (error) {
  244. return callback(error)
  245. }
  246. callback(null, result)
  247. }
  248. )
  249. } else {
  250. DocumentManager.flushAndDeleteDoc(
  251. projectId,
  252. docId,
  253. {},
  254. (error, result) => {
  255. // There is no harm in flushing project history if the previous
  256. // call failed and sometimes it is required
  257. HistoryManager.flushProjectChangesAsync(projectId)
  258. if (error) {
  259. return callback(error)
  260. }
  261. callback(null, result)
  262. }
  263. )
  264. }
  265. })
  266. })
  267. }
  268. )
  269. },
  270. flushDocIfLoaded(projectId, docId, _callback) {
  271. const timer = new Metrics.Timer('docManager.flushDocIfLoaded')
  272. const callback = (...args) => {
  273. timer.done()
  274. _callback(...args)
  275. }
  276. RedisManager.getDoc(
  277. projectId,
  278. docId,
  279. (
  280. error,
  281. lines,
  282. version,
  283. ranges,
  284. pathname,
  285. projectHistoryId,
  286. unflushedTime,
  287. lastUpdatedAt,
  288. lastUpdatedBy
  289. ) => {
  290. if (error) {
  291. return callback(error)
  292. }
  293. if (lines == null || version == null) {
  294. Metrics.inc('flush-doc-if-loaded', 1, { status: 'not-loaded' })
  295. logger.debug(
  296. { projectId, docId },
  297. 'doc is not loaded so not flushing'
  298. )
  299. // TODO: return a flag to bail out, as we go on to remove doc from memory?
  300. callback(null)
  301. } else if (unflushedTime == null) {
  302. Metrics.inc('flush-doc-if-loaded', 1, { status: 'unmodified' })
  303. logger.debug(
  304. { projectId, docId },
  305. 'doc is not modified so not flushing'
  306. )
  307. callback(null)
  308. } else {
  309. logger.debug({ projectId, docId, version }, 'flushing doc')
  310. Metrics.inc('flush-doc-if-loaded', 1, { status: 'modified' })
  311. PersistenceManager.setDoc(
  312. projectId,
  313. docId,
  314. lines,
  315. version,
  316. ranges,
  317. lastUpdatedAt,
  318. lastUpdatedBy,
  319. (error, result) => {
  320. if (error) {
  321. return callback(error)
  322. }
  323. RedisManager.clearUnflushedTime(docId, err => {
  324. if (err) {
  325. return callback(err)
  326. }
  327. callback(null, result)
  328. })
  329. }
  330. )
  331. }
  332. }
  333. )
  334. },
  335. flushAndDeleteDoc(projectId, docId, options, _callback) {
  336. const timer = new Metrics.Timer('docManager.flushAndDeleteDoc')
  337. const callback = (...args) => {
  338. timer.done()
  339. _callback(...args)
  340. }
  341. DocumentManager.flushDocIfLoaded(projectId, docId, (error, result) => {
  342. if (error) {
  343. if (options.ignoreFlushErrors) {
  344. logger.warn(
  345. { projectId, docId, err: error },
  346. 'ignoring flush error while deleting document'
  347. )
  348. } else {
  349. return callback(error)
  350. }
  351. }
  352. RedisManager.removeDocFromMemory(projectId, docId, error => {
  353. if (error) {
  354. return callback(error)
  355. }
  356. callback(null, result)
  357. })
  358. })
  359. },
  360. acceptChanges(projectId, docId, changeIds, _callback) {
  361. if (changeIds == null) {
  362. changeIds = []
  363. }
  364. const timer = new Metrics.Timer('docManager.acceptChanges')
  365. const callback = (...args) => {
  366. timer.done()
  367. _callback(...args)
  368. }
  369. DocumentManager.getDoc(
  370. projectId,
  371. docId,
  372. (error, lines, version, ranges) => {
  373. if (error) {
  374. return callback(error)
  375. }
  376. if (lines == null || version == null) {
  377. return callback(
  378. new Errors.NotFoundError(`document not found: ${docId}`)
  379. )
  380. }
  381. let newRanges
  382. try {
  383. newRanges = RangesManager.acceptChanges(changeIds, ranges)
  384. } catch (err) {
  385. return callback(err)
  386. }
  387. RedisManager.updateDocument(
  388. projectId,
  389. docId,
  390. lines,
  391. version,
  392. [],
  393. newRanges,
  394. {},
  395. error => {
  396. if (error) {
  397. return callback(error)
  398. }
  399. callback()
  400. }
  401. )
  402. }
  403. )
  404. },
  405. deleteComment(projectId, docId, commentId, _callback) {
  406. const timer = new Metrics.Timer('docManager.deleteComment')
  407. const callback = (...args) => {
  408. timer.done()
  409. _callback(...args)
  410. }
  411. DocumentManager.getDoc(
  412. projectId,
  413. docId,
  414. (error, lines, version, ranges) => {
  415. if (error) {
  416. return callback(error)
  417. }
  418. if (lines == null || version == null) {
  419. return callback(
  420. new Errors.NotFoundError(`document not found: ${docId}`)
  421. )
  422. }
  423. let newRanges
  424. try {
  425. newRanges = RangesManager.deleteComment(commentId, ranges)
  426. } catch (err) {
  427. return callback(err)
  428. }
  429. RedisManager.updateDocument(
  430. projectId,
  431. docId,
  432. lines,
  433. version,
  434. [],
  435. newRanges,
  436. {},
  437. error => {
  438. if (error) {
  439. return callback(error)
  440. }
  441. callback()
  442. }
  443. )
  444. }
  445. )
  446. },
  447. renameDoc(projectId, docId, userId, update, projectHistoryId, _callback) {
  448. const timer = new Metrics.Timer('docManager.updateProject')
  449. const callback = (...args) => {
  450. timer.done()
  451. _callback(...args)
  452. }
  453. RedisManager.renameDoc(
  454. projectId,
  455. docId,
  456. userId,
  457. update,
  458. projectHistoryId,
  459. callback
  460. )
  461. },
  462. getDocAndFlushIfOld(projectId, docId, callback) {
  463. DocumentManager.getDoc(
  464. projectId,
  465. docId,
  466. (
  467. error,
  468. lines,
  469. version,
  470. ranges,
  471. pathname,
  472. projectHistoryId,
  473. unflushedTime,
  474. alreadyLoaded
  475. ) => {
  476. if (error) {
  477. return callback(error)
  478. }
  479. // if doc was already loaded see if it needs to be flushed
  480. if (
  481. alreadyLoaded &&
  482. unflushedTime != null &&
  483. Date.now() - unflushedTime > MAX_UNFLUSHED_AGE
  484. ) {
  485. DocumentManager.flushDocIfLoaded(projectId, docId, error => {
  486. if (error) {
  487. return callback(error)
  488. }
  489. callback(null, lines, version)
  490. })
  491. } else {
  492. callback(null, lines, version)
  493. }
  494. }
  495. )
  496. },
  497. resyncDocContents(projectId, docId, path, callback) {
  498. logger.debug({ projectId, docId, path }, 'start resyncing doc contents')
  499. RedisManager.getDoc(
  500. projectId,
  501. docId,
  502. (error, lines, version, ranges, pathname, projectHistoryId) => {
  503. if (error) {
  504. return callback(error)
  505. }
  506. // To avoid issues where the same docId appears with different paths,
  507. // we use the path from the resyncProjectStructure update. If we used
  508. // the path from the getDoc call to web then the two occurences of the
  509. // docId would map to the same path, and this would be rejected by
  510. // project-history as an unexpected resyncDocContent update.
  511. if (lines == null || version == null) {
  512. logger.debug(
  513. { projectId, docId },
  514. 'resyncing doc contents - not found in redis - retrieving from web'
  515. )
  516. PersistenceManager.getDoc(
  517. projectId,
  518. docId,
  519. { peek: true },
  520. (error, lines, version, ranges, pathname, projectHistoryId) => {
  521. if (error) {
  522. logger.error(
  523. { projectId, docId, getDocError: error },
  524. 'resyncing doc contents - error retrieving from web'
  525. )
  526. return callback(error)
  527. }
  528. ProjectHistoryRedisManager.queueResyncDocContent(
  529. projectId,
  530. projectHistoryId,
  531. docId,
  532. lines,
  533. version,
  534. path, // use the path from the resyncProjectStructure update
  535. callback
  536. )
  537. }
  538. )
  539. } else {
  540. logger.debug(
  541. { projectId, docId },
  542. 'resyncing doc contents - doc in redis - will queue in redis'
  543. )
  544. ProjectHistoryRedisManager.queueResyncDocContent(
  545. projectId,
  546. projectHistoryId,
  547. docId,
  548. lines,
  549. version,
  550. path, // use the path from the resyncProjectStructure update
  551. callback
  552. )
  553. }
  554. }
  555. )
  556. },
  557. getDocWithLock(projectId, docId, callback) {
  558. const UpdateManager = require('./UpdateManager')
  559. UpdateManager.lockUpdatesAndDo(
  560. DocumentManager.getDoc,
  561. projectId,
  562. docId,
  563. callback
  564. )
  565. },
  566. getDocAndRecentOpsWithLock(projectId, docId, fromVersion, callback) {
  567. const UpdateManager = require('./UpdateManager')
  568. UpdateManager.lockUpdatesAndDo(
  569. DocumentManager.getDocAndRecentOps,
  570. projectId,
  571. docId,
  572. fromVersion,
  573. callback
  574. )
  575. },
  576. getDocAndFlushIfOldWithLock(projectId, docId, callback) {
  577. const UpdateManager = require('./UpdateManager')
  578. UpdateManager.lockUpdatesAndDo(
  579. DocumentManager.getDocAndFlushIfOld,
  580. projectId,
  581. docId,
  582. callback
  583. )
  584. },
  585. setDocWithLock(projectId, docId, lines, source, userId, undoing, callback) {
  586. const UpdateManager = require('./UpdateManager')
  587. UpdateManager.lockUpdatesAndDo(
  588. DocumentManager.setDoc,
  589. projectId,
  590. docId,
  591. lines,
  592. source,
  593. userId,
  594. undoing,
  595. callback
  596. )
  597. },
  598. flushDocIfLoadedWithLock(projectId, docId, callback) {
  599. const UpdateManager = require('./UpdateManager')
  600. UpdateManager.lockUpdatesAndDo(
  601. DocumentManager.flushDocIfLoaded,
  602. projectId,
  603. docId,
  604. callback
  605. )
  606. },
  607. flushAndDeleteDocWithLock(projectId, docId, options, callback) {
  608. const UpdateManager = require('./UpdateManager')
  609. UpdateManager.lockUpdatesAndDo(
  610. DocumentManager.flushAndDeleteDoc,
  611. projectId,
  612. docId,
  613. options,
  614. callback
  615. )
  616. },
  617. acceptChangesWithLock(projectId, docId, changeIds, callback) {
  618. const UpdateManager = require('./UpdateManager')
  619. UpdateManager.lockUpdatesAndDo(
  620. DocumentManager.acceptChanges,
  621. projectId,
  622. docId,
  623. changeIds,
  624. callback
  625. )
  626. },
  627. deleteCommentWithLock(projectId, docId, threadId, callback) {
  628. const UpdateManager = require('./UpdateManager')
  629. UpdateManager.lockUpdatesAndDo(
  630. DocumentManager.deleteComment,
  631. projectId,
  632. docId,
  633. threadId,
  634. callback
  635. )
  636. },
  637. renameDocWithLock(
  638. projectId,
  639. docId,
  640. userId,
  641. update,
  642. projectHistoryId,
  643. callback
  644. ) {
  645. const UpdateManager = require('./UpdateManager')
  646. UpdateManager.lockUpdatesAndDo(
  647. DocumentManager.renameDoc,
  648. projectId,
  649. docId,
  650. userId,
  651. update,
  652. projectHistoryId,
  653. callback
  654. )
  655. },
  656. resyncDocContentsWithLock(projectId, docId, path, callback) {
  657. const UpdateManager = require('./UpdateManager')
  658. UpdateManager.lockUpdatesAndDo(
  659. DocumentManager.resyncDocContents,
  660. projectId,
  661. docId,
  662. path,
  663. callback
  664. )
  665. },
  666. }
  667. module.exports = DocumentManager
  668. module.exports.promises = promisifyAll(DocumentManager, {
  669. multiResult: {
  670. getDoc: [
  671. 'lines',
  672. 'version',
  673. 'ranges',
  674. 'pathname',
  675. 'projectHistoryId',
  676. 'unflushedTime',
  677. 'alreadyLoaded',
  678. 'historyRangesSupport',
  679. ],
  680. getDocWithLock: [
  681. 'lines',
  682. 'version',
  683. 'ranges',
  684. 'pathname',
  685. 'projectHistoryId',
  686. 'unflushedTime',
  687. 'alreadyLoaded',
  688. 'historyRangesSupport',
  689. ],
  690. getDocAndFlushIfOld: ['lines', 'version'],
  691. getDocAndFlushIfOldWithLock: ['lines', 'version'],
  692. getDocAndRecentOps: [
  693. 'lines',
  694. 'version',
  695. 'ops',
  696. 'ranges',
  697. 'pathname',
  698. 'projectHistoryId',
  699. ],
  700. getDocAndRecentOpsWithLock: [
  701. 'lines',
  702. 'version',
  703. 'ops',
  704. 'ranges',
  705. 'pathname',
  706. 'projectHistoryId',
  707. ],
  708. },
  709. })