ProjectHistoryRedisManager.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // @ts-check
  2. const Settings = require('@overleaf/settings')
  3. const { callbackifyAll } = require('@overleaf/promise-utils')
  4. const projectHistoryKeys = Settings.redis?.project_history?.key_schema
  5. const rclient = require('@overleaf/redis-wrapper').createClient(
  6. Settings.redis.project_history
  7. )
  8. const logger = require('@overleaf/logger')
  9. const metrics = require('./Metrics')
  10. const { docIsTooLarge } = require('./Limits')
  11. const { addTrackedDeletesToContent } = require('./Utils')
  12. const HistoryConversions = require('./HistoryConversions')
  13. const OError = require('@overleaf/o-error')
  14. /**
  15. * @typedef {import('./types').Ranges} Ranges
  16. */
  17. const ProjectHistoryRedisManager = {
  18. async queueOps(projectId, ...ops) {
  19. // Record metric for ops pushed onto queue
  20. for (const op of ops) {
  21. metrics.summary('redis.projectHistoryOps', op.length, { status: 'push' })
  22. }
  23. // Make sure that this MULTI operation only operates on project
  24. // specific keys, i.e. keys that have the project id in curly braces.
  25. // The curly braces identify a hash key for Redis and ensures that
  26. // the MULTI's operations are all done on the same node in a
  27. // cluster environment.
  28. const multi = rclient.multi()
  29. // Push the ops onto the project history queue
  30. multi.rpush(
  31. projectHistoryKeys.projectHistoryOps({ project_id: projectId }),
  32. ...ops
  33. )
  34. // To record the age of the oldest op on the queue set a timestamp if not
  35. // already present (SETNX).
  36. multi.setnx(
  37. projectHistoryKeys.projectHistoryFirstOpTimestamp({
  38. project_id: projectId,
  39. }),
  40. Date.now()
  41. )
  42. const result = await multi.exec()
  43. return result[0]
  44. },
  45. async queueRenameEntity(
  46. projectId,
  47. projectHistoryId,
  48. entityType,
  49. entityId,
  50. userId,
  51. projectUpdate,
  52. source
  53. ) {
  54. projectUpdate = {
  55. pathname: projectUpdate.pathname,
  56. new_pathname: projectUpdate.newPathname,
  57. meta: {
  58. user_id: userId,
  59. ts: new Date(),
  60. },
  61. version: projectUpdate.version,
  62. projectHistoryId,
  63. }
  64. projectUpdate[entityType] = entityId
  65. if (source != null) {
  66. projectUpdate.meta.source = source
  67. if (source !== 'editor') {
  68. projectUpdate.meta.type = 'external'
  69. }
  70. }
  71. logger.debug(
  72. { projectId, projectUpdate },
  73. 'queue rename operation to project-history'
  74. )
  75. const jsonUpdate = JSON.stringify(projectUpdate)
  76. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  77. },
  78. async queueAddEntity(
  79. projectId,
  80. projectHistoryId,
  81. entityType,
  82. entityId,
  83. userId,
  84. projectUpdate,
  85. source
  86. ) {
  87. let docLines = projectUpdate.docLines
  88. let ranges
  89. if (projectUpdate.historyRangesSupport && projectUpdate.ranges) {
  90. docLines = addTrackedDeletesToContent(
  91. docLines,
  92. projectUpdate.ranges.changes ?? []
  93. )
  94. ranges = HistoryConversions.toHistoryRanges(projectUpdate.ranges)
  95. }
  96. projectUpdate = {
  97. pathname: projectUpdate.pathname,
  98. docLines,
  99. url: projectUpdate.url,
  100. meta: {
  101. user_id: userId,
  102. ts: new Date(),
  103. },
  104. version: projectUpdate.version,
  105. projectHistoryId,
  106. }
  107. if (ranges) {
  108. projectUpdate.ranges = ranges
  109. }
  110. projectUpdate[entityType] = entityId
  111. if (source != null) {
  112. projectUpdate.meta.source = source
  113. if (source !== 'editor') {
  114. projectUpdate.meta.type = 'external'
  115. }
  116. }
  117. logger.debug(
  118. { projectId, projectUpdate },
  119. 'queue add operation to project-history'
  120. )
  121. const jsonUpdate = JSON.stringify(projectUpdate)
  122. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  123. },
  124. async queueResyncProjectStructure(projectId, projectHistoryId, docs, files) {
  125. logger.debug({ projectId, docs, files }, 'queue project structure resync')
  126. const projectUpdate = {
  127. resyncProjectStructure: { docs, files },
  128. projectHistoryId,
  129. meta: {
  130. ts: new Date(),
  131. },
  132. }
  133. const jsonUpdate = JSON.stringify(projectUpdate)
  134. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  135. },
  136. /**
  137. * Add a resync doc update to the project-history queue
  138. *
  139. * @param {string} projectId
  140. * @param {string} projectHistoryId
  141. * @param {string} docId
  142. * @param {string[]} lines
  143. * @param {Ranges} ranges
  144. * @param {string[]} resolvedCommentIds
  145. * @param {number} version
  146. * @param {string} pathname
  147. * @param {boolean} historyRangesSupport
  148. * @return {Promise<number>} the number of ops added
  149. */
  150. async queueResyncDocContent(
  151. projectId,
  152. projectHistoryId,
  153. docId,
  154. lines,
  155. ranges,
  156. resolvedCommentIds,
  157. version,
  158. pathname,
  159. historyRangesSupport
  160. ) {
  161. logger.debug(
  162. { projectId, docId, lines, version, pathname },
  163. 'queue doc content resync'
  164. )
  165. let content = lines.join('\n')
  166. if (historyRangesSupport) {
  167. content = addTrackedDeletesToContent(content, ranges.changes ?? [])
  168. }
  169. const projectUpdate = {
  170. resyncDocContent: { content, version },
  171. projectHistoryId,
  172. path: pathname,
  173. doc: docId,
  174. meta: {
  175. ts: new Date(),
  176. },
  177. }
  178. if (historyRangesSupport) {
  179. projectUpdate.resyncDocContent.ranges =
  180. HistoryConversions.toHistoryRanges(ranges)
  181. projectUpdate.resyncDocContent.resolvedCommentIds = resolvedCommentIds
  182. }
  183. const jsonUpdate = JSON.stringify(projectUpdate)
  184. // Do an optimised size check on the docLines using the serialised
  185. // project update length as an upper bound
  186. const sizeBound = jsonUpdate.length
  187. if (docIsTooLarge(sizeBound, lines, Settings.max_doc_length)) {
  188. throw new OError(
  189. 'blocking resync doc content insert into project history queue: doc is too large',
  190. { projectId, docId, docSize: sizeBound }
  191. )
  192. }
  193. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  194. },
  195. }
  196. module.exports = {
  197. ...callbackifyAll(ProjectHistoryRedisManager),
  198. promises: ProjectHistoryRedisManager,
  199. }