ProjectHistoryRedisManager.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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, extractOriginOrSource } = 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. originOrSource
  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. const { origin, source } = extractOriginOrSource(originOrSource)
  66. if (origin != null) {
  67. projectUpdate.meta.origin = origin
  68. if (origin.kind !== 'editor') {
  69. projectUpdate.meta.type = 'external'
  70. }
  71. } else if (source != null) {
  72. projectUpdate.meta.source = source
  73. if (source !== 'editor') {
  74. projectUpdate.meta.type = 'external'
  75. }
  76. }
  77. logger.debug(
  78. { projectId, projectUpdate },
  79. 'queue rename operation to project-history'
  80. )
  81. const jsonUpdate = JSON.stringify(projectUpdate)
  82. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  83. },
  84. async queueAddEntity(
  85. projectId,
  86. projectHistoryId,
  87. entityType,
  88. entityId,
  89. userId,
  90. projectUpdate,
  91. originOrSource
  92. ) {
  93. let docLines = projectUpdate.docLines
  94. let ranges
  95. if (projectUpdate.historyRangesSupport && projectUpdate.ranges) {
  96. docLines = addTrackedDeletesToContent(
  97. docLines,
  98. projectUpdate.ranges.changes ?? []
  99. )
  100. ranges = HistoryConversions.toHistoryRanges(projectUpdate.ranges)
  101. }
  102. projectUpdate = {
  103. pathname: projectUpdate.pathname,
  104. docLines,
  105. url: projectUpdate.url,
  106. meta: {
  107. user_id: userId,
  108. ts: new Date(),
  109. },
  110. version: projectUpdate.version,
  111. projectHistoryId,
  112. }
  113. if (ranges) {
  114. projectUpdate.ranges = ranges
  115. }
  116. projectUpdate[entityType] = entityId
  117. const { origin, source } = extractOriginOrSource(originOrSource)
  118. if (origin != null) {
  119. projectUpdate.meta.origin = origin
  120. if (origin.kind !== 'editor') {
  121. projectUpdate.meta.type = 'external'
  122. }
  123. } else if (source != null) {
  124. projectUpdate.meta.source = source
  125. if (source !== 'editor') {
  126. projectUpdate.meta.type = 'external'
  127. }
  128. }
  129. logger.debug(
  130. { projectId, projectUpdate },
  131. 'queue add operation to project-history'
  132. )
  133. const jsonUpdate = JSON.stringify(projectUpdate)
  134. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  135. },
  136. async queueResyncProjectStructure(projectId, projectHistoryId, docs, files) {
  137. logger.debug({ projectId, docs, files }, 'queue project structure resync')
  138. const projectUpdate = {
  139. resyncProjectStructure: { docs, files },
  140. projectHistoryId,
  141. meta: {
  142. ts: new Date(),
  143. },
  144. }
  145. const jsonUpdate = JSON.stringify(projectUpdate)
  146. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  147. },
  148. /**
  149. * Add a resync doc update to the project-history queue
  150. *
  151. * @param {string} projectId
  152. * @param {string} projectHistoryId
  153. * @param {string} docId
  154. * @param {string[]} lines
  155. * @param {Ranges} ranges
  156. * @param {string[]} resolvedCommentIds
  157. * @param {number} version
  158. * @param {string} pathname
  159. * @param {boolean} historyRangesSupport
  160. * @return {Promise<number>} the number of ops added
  161. */
  162. async queueResyncDocContent(
  163. projectId,
  164. projectHistoryId,
  165. docId,
  166. lines,
  167. ranges,
  168. resolvedCommentIds,
  169. version,
  170. pathname,
  171. historyRangesSupport
  172. ) {
  173. logger.debug(
  174. { projectId, docId, lines, version, pathname },
  175. 'queue doc content resync'
  176. )
  177. let content = lines.join('\n')
  178. if (historyRangesSupport) {
  179. content = addTrackedDeletesToContent(content, ranges.changes ?? [])
  180. }
  181. const projectUpdate = {
  182. resyncDocContent: { content, version },
  183. projectHistoryId,
  184. path: pathname,
  185. doc: docId,
  186. meta: {
  187. ts: new Date(),
  188. },
  189. }
  190. if (historyRangesSupport) {
  191. projectUpdate.resyncDocContent.ranges =
  192. HistoryConversions.toHistoryRanges(ranges)
  193. projectUpdate.resyncDocContent.resolvedCommentIds = resolvedCommentIds
  194. }
  195. const jsonUpdate = JSON.stringify(projectUpdate)
  196. // Do an optimised size check on the docLines using the serialised
  197. // project update length as an upper bound
  198. const sizeBound = jsonUpdate.length
  199. if (docIsTooLarge(sizeBound, lines, Settings.max_doc_length)) {
  200. throw new OError(
  201. 'blocking resync doc content insert into project history queue: doc is too large',
  202. { projectId, docId, docSize: sizeBound }
  203. )
  204. }
  205. return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
  206. },
  207. }
  208. module.exports = {
  209. ...callbackifyAll(ProjectHistoryRedisManager),
  210. promises: ProjectHistoryRedisManager,
  211. }