DocumentManager.js 18 KB

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