DocumentManager.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. (
  43. error,
  44. lines,
  45. version,
  46. ranges,
  47. pathname,
  48. projectHistoryId,
  49. projectHistoryType
  50. ) => {
  51. if (error) {
  52. return callback(error)
  53. }
  54. logger.debug(
  55. {
  56. projectId,
  57. docId,
  58. lines,
  59. version,
  60. pathname,
  61. projectHistoryId,
  62. projectHistoryType,
  63. },
  64. 'got doc from persistence API'
  65. )
  66. RedisManager.putDocInMemory(
  67. projectId,
  68. docId,
  69. lines,
  70. version,
  71. ranges,
  72. pathname,
  73. projectHistoryId,
  74. error => {
  75. if (error) {
  76. return callback(error)
  77. }
  78. RedisManager.setHistoryType(
  79. docId,
  80. projectHistoryType,
  81. error => {
  82. if (error) {
  83. return callback(error)
  84. }
  85. callback(
  86. null,
  87. lines,
  88. version,
  89. ranges || {},
  90. pathname,
  91. projectHistoryId,
  92. null,
  93. false
  94. )
  95. }
  96. )
  97. }
  98. )
  99. }
  100. )
  101. } else {
  102. callback(
  103. null,
  104. lines,
  105. version,
  106. ranges,
  107. pathname,
  108. projectHistoryId,
  109. unflushedTime,
  110. true
  111. )
  112. }
  113. }
  114. )
  115. },
  116. getDocAndRecentOps(projectId, docId, fromVersion, _callback) {
  117. const timer = new Metrics.Timer('docManager.getDocAndRecentOps')
  118. const callback = (...args) => {
  119. timer.done()
  120. _callback(...args)
  121. }
  122. DocumentManager.getDoc(
  123. projectId,
  124. docId,
  125. (error, lines, version, ranges, pathname, projectHistoryId) => {
  126. if (error) {
  127. return callback(error)
  128. }
  129. if (fromVersion === -1) {
  130. callback(null, lines, version, [], ranges, pathname, projectHistoryId)
  131. } else {
  132. RedisManager.getPreviousDocOps(
  133. docId,
  134. fromVersion,
  135. version,
  136. (error, ops) => {
  137. if (error) {
  138. return callback(error)
  139. }
  140. callback(
  141. null,
  142. lines,
  143. version,
  144. ops,
  145. ranges,
  146. pathname,
  147. projectHistoryId
  148. )
  149. }
  150. )
  151. }
  152. }
  153. )
  154. },
  155. setDoc(projectId, docId, newLines, source, userId, undoing, _callback) {
  156. const timer = new Metrics.Timer('docManager.setDoc')
  157. const callback = (...args) => {
  158. timer.done()
  159. _callback(...args)
  160. }
  161. if (newLines == null) {
  162. return callback(new Error('No lines were provided to setDoc'))
  163. }
  164. const UpdateManager = require('./UpdateManager')
  165. DocumentManager.getDoc(
  166. projectId,
  167. docId,
  168. (
  169. error,
  170. oldLines,
  171. version,
  172. ranges,
  173. pathname,
  174. projectHistoryId,
  175. unflushedTime,
  176. alreadyLoaded
  177. ) => {
  178. if (error) {
  179. return callback(error)
  180. }
  181. if (
  182. oldLines != null &&
  183. oldLines.length > 0 &&
  184. oldLines[0].text != null
  185. ) {
  186. logger.debug(
  187. { docId, projectId, oldLines, newLines },
  188. 'document is JSON so not updating'
  189. )
  190. return callback(null)
  191. }
  192. logger.debug(
  193. { docId, projectId, oldLines, newLines },
  194. 'setting a document via http'
  195. )
  196. DiffCodec.diffAsShareJsOp(oldLines, newLines, (error, op) => {
  197. if (error) {
  198. return callback(error)
  199. }
  200. if (undoing) {
  201. for (const o of op || []) {
  202. o.u = true
  203. } // Turn on undo flag for each op for track changes
  204. }
  205. const update = {
  206. doc: docId,
  207. op,
  208. v: version,
  209. meta: {
  210. type: 'external',
  211. source,
  212. user_id: userId,
  213. },
  214. }
  215. // Keep track of external updates, whether they are for live documents
  216. // (flush) or unloaded documents (evict), and whether the update is a no-op.
  217. Metrics.inc('external-update', 1, {
  218. status: op.length > 0 ? 'diff' : 'noop',
  219. method: alreadyLoaded ? 'flush' : 'evict',
  220. path: source,
  221. })
  222. UpdateManager.applyUpdate(projectId, docId, update, error => {
  223. if (error) {
  224. return callback(error)
  225. }
  226. // If the document was loaded already, then someone has it open
  227. // in a project, and the usual flushing mechanism will happen.
  228. // Otherwise we should remove it immediately since nothing else
  229. // is using it.
  230. if (alreadyLoaded) {
  231. DocumentManager.flushDocIfLoaded(
  232. projectId,
  233. docId,
  234. (error, result) => {
  235. if (error) {
  236. return callback(error)
  237. }
  238. callback(null, result)
  239. }
  240. )
  241. } else {
  242. DocumentManager.flushAndDeleteDoc(
  243. projectId,
  244. docId,
  245. {},
  246. (error, result) => {
  247. // There is no harm in flushing project history if the previous
  248. // call failed and sometimes it is required
  249. HistoryManager.flushProjectChangesAsync(projectId)
  250. if (error) {
  251. return callback(error)
  252. }
  253. callback(null, result)
  254. }
  255. )
  256. }
  257. })
  258. })
  259. }
  260. )
  261. },
  262. flushDocIfLoaded(projectId, docId, _callback) {
  263. const timer = new Metrics.Timer('docManager.flushDocIfLoaded')
  264. const callback = (...args) => {
  265. timer.done()
  266. _callback(...args)
  267. }
  268. RedisManager.getDoc(
  269. projectId,
  270. docId,
  271. (
  272. error,
  273. lines,
  274. version,
  275. ranges,
  276. pathname,
  277. projectHistoryId,
  278. unflushedTime,
  279. lastUpdatedAt,
  280. lastUpdatedBy
  281. ) => {
  282. if (error) {
  283. return callback(error)
  284. }
  285. if (lines == null || version == null) {
  286. logger.debug(
  287. { projectId, docId },
  288. 'doc is not loaded so not flushing'
  289. )
  290. // TODO: return a flag to bail out, as we go on to remove doc from memory?
  291. callback(null)
  292. } else {
  293. logger.debug({ projectId, docId, version }, 'flushing doc')
  294. PersistenceManager.setDoc(
  295. projectId,
  296. docId,
  297. lines,
  298. version,
  299. ranges,
  300. lastUpdatedAt,
  301. lastUpdatedBy,
  302. (error, result) => {
  303. if (error) {
  304. return callback(error)
  305. }
  306. RedisManager.clearUnflushedTime(docId, err => {
  307. if (err) {
  308. return callback(err)
  309. }
  310. callback(null, result)
  311. })
  312. }
  313. )
  314. }
  315. }
  316. )
  317. },
  318. flushAndDeleteDoc(projectId, docId, options, _callback) {
  319. const timer = new Metrics.Timer('docManager.flushAndDeleteDoc')
  320. const callback = (...args) => {
  321. timer.done()
  322. _callback(...args)
  323. }
  324. DocumentManager.flushDocIfLoaded(projectId, docId, (error, result) => {
  325. if (error) {
  326. if (options.ignoreFlushErrors) {
  327. logger.warn(
  328. { projectId, docId, err: error },
  329. 'ignoring flush error while deleting document'
  330. )
  331. } else {
  332. return callback(error)
  333. }
  334. }
  335. // Flush in the background since it requires a http request
  336. HistoryManager.flushDocChangesAsync(projectId, docId)
  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. }