UpdatesManager.coffee 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. MongoManager = require "./MongoManager"
  2. RedisManager = require "./RedisManager"
  3. UpdateCompressor = require "./UpdateCompressor"
  4. LockManager = require "./LockManager"
  5. WebApiManager = require "./WebApiManager"
  6. logger = require "logger-sharelatex"
  7. async = require "async"
  8. module.exports = UpdatesManager =
  9. compressAndSaveRawUpdates: (project_id, doc_id, rawUpdates, callback = (error) ->) ->
  10. length = rawUpdates.length
  11. if length == 0
  12. return callback()
  13. MongoManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) ->
  14. return callback(error) if error?
  15. # Ensure that raw updates start where lastCompressedUpdate left off
  16. if lastCompressedUpdate?
  17. rawUpdates = rawUpdates.slice(0)
  18. while rawUpdates[0]? and rawUpdates[0].v <= lastCompressedUpdate.v
  19. rawUpdates.shift()
  20. if rawUpdates[0]? and rawUpdates[0].v != lastCompressedUpdate.v + 1
  21. error = new Error("Tried to apply raw op at version #{rawUpdates[0].v} to last compressed update with version #{lastCompressedUpdate.v}")
  22. logger.error err: error, doc_id: doc_id, project_id: project_id, "inconsistent doc versions"
  23. # Push the update back into Mongo - catching errors at this
  24. # point is useless, we're already bailing
  25. MongoManager.insertCompressedUpdates project_id, doc_id, [lastCompressedUpdate], () ->
  26. return callback error
  27. return
  28. compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
  29. MongoManager.insertCompressedUpdates project_id, doc_id, compressedUpdates, (error) ->
  30. return callback(error) if error?
  31. logger.log project_id: project_id, doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"
  32. callback()
  33. REDIS_READ_BATCH_SIZE: 100
  34. processUncompressedUpdates: (project_id, doc_id, callback = (error) ->) ->
  35. MongoManager.backportProjectId project_id, doc_id, (error) ->
  36. return callback(error) if error?
  37. RedisManager.getOldestRawUpdates doc_id, UpdatesManager.REDIS_READ_BATCH_SIZE, (error, rawUpdates) ->
  38. return callback(error) if error?
  39. length = rawUpdates.length
  40. UpdatesManager.compressAndSaveRawUpdates project_id, doc_id, rawUpdates, (error) ->
  41. return callback(error) if error?
  42. logger.log project_id: project_id, doc_id: doc_id, "compressed and saved doc updates"
  43. RedisManager.deleteOldestRawUpdates project_id, doc_id, length, (error) ->
  44. return callback(error) if error?
  45. if length == UpdatesManager.REDIS_READ_BATCH_SIZE
  46. # There might be more updates
  47. logger.log project_id: project_id, doc_id: doc_id, "continuing processing updates"
  48. setTimeout () ->
  49. UpdatesManager.processUncompressedUpdates project_id, doc_id, callback
  50. , 0
  51. else
  52. logger.log project_id: project_id, doc_id: doc_id, "all raw updates processed"
  53. callback()
  54. processUncompressedUpdatesWithLock: (project_id, doc_id, callback = (error) ->) ->
  55. LockManager.runWithLock(
  56. "HistoryLock:#{doc_id}",
  57. (releaseLock) ->
  58. UpdatesManager.processUncompressedUpdates project_id, doc_id, releaseLock
  59. callback
  60. )
  61. processUncompressedUpdatesForProject: (project_id, callback = (error) ->) ->
  62. RedisManager.getDocIdsWithHistoryOps project_id, (error, doc_ids) ->
  63. return callback(error) if error?
  64. jobs = []
  65. for doc_id in doc_ids
  66. do (doc_id) ->
  67. jobs.push (callback) ->
  68. UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, callback
  69. async.parallelLimit jobs, 5, callback
  70. getDocUpdates: (project_id, doc_id, options = {}, callback = (error, updates) ->) ->
  71. UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, (error) ->
  72. return callback(error) if error?
  73. MongoManager.getDocUpdates doc_id, options, callback
  74. getDocUpdatesWithUserInfo: (project_id, doc_id, options = {}, callback = (error, updates) ->) ->
  75. UpdatesManager.getDocUpdates project_id, doc_id, options, (error, updates) ->
  76. return callback(error) if error?
  77. UpdatesManager.fillUserInfo updates, (error, updates) ->
  78. return callback(error) if error?
  79. callback null, updates
  80. getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  81. UpdatesManager.processUncompressedUpdatesForProject project_id, (error) ->
  82. return callback(error) if error?
  83. MongoManager.getProjectUpdates project_id, options, callback
  84. getProjectUpdatesWithUserInfo: (project_id, options = {}, callback = (error, updates) ->) ->
  85. UpdatesManager.getProjectUpdates project_id, options, (error, updates) ->
  86. return callback(error) if error?
  87. UpdatesManager.fillUserInfo updates, (error, updates) ->
  88. return callback(error) if error?
  89. callback null, updates
  90. getSummarizedProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  91. options.min_count ||= 25
  92. summarizedUpdates = []
  93. before = options.before
  94. do fetchNextBatch = () ->
  95. UpdatesManager._extendBatchOfSummarizedUpdates project_id, summarizedUpdates, before, options.min_count, (error, updates, nextBeforeUpdate) ->
  96. return callback(error) if error?
  97. if !nextBeforeUpdate? or updates.length >= options.min_count
  98. callback null, updates, nextBeforeUpdate
  99. else
  100. before = nextBeforeUpdate
  101. summarizedUpdates = updates
  102. fetchNextBatch()
  103. _extendBatchOfSummarizedUpdates: (
  104. project_id,
  105. existingSummarizedUpdates,
  106. before, desiredLength,
  107. callback = (error, summarizedUpdates, endOfDatabase) ->
  108. ) ->
  109. UpdatesManager.getProjectUpdatesWithUserInfo project_id, { before: before, limit: 3 * desiredLength }, (error, updates) ->
  110. return callback(error) if error?
  111. # Suppose in this request we have fetch the solid updates. In the next request we need
  112. # to fetch the dotted updates. These are defined by having an end timestamp less than
  113. # the last update's end timestamp (updates are ordered by descending end_ts). I.e.
  114. # start_ts--v v--end_ts
  115. # doc1: |......| |...| |-------|
  116. # doc2: |------------------|
  117. # ^----- Next time, fetch all updates with an
  118. # end_ts less than this
  119. #
  120. if updates? and updates.length > 0
  121. nextBeforeTimestamp = updates[updates.length - 1].meta.end_ts
  122. else
  123. nextBeforeTimestamp = null
  124. summarizedUpdates = UpdatesManager._summarizeUpdates(
  125. updates, existingSummarizedUpdates
  126. )
  127. callback null,
  128. summarizedUpdates,
  129. nextBeforeTimestamp
  130. fillUserInfo: (updates, callback = (error, updates) ->) ->
  131. users = {}
  132. for update in updates
  133. if UpdatesManager._validUserId(update.meta.user_id)
  134. users[update.meta.user_id] = true
  135. jobs = []
  136. for user_id, _ of users
  137. do (user_id) ->
  138. jobs.push (callback) ->
  139. WebApiManager.getUserInfo user_id, (error, userInfo) ->
  140. return callback(error) if error?
  141. users[user_id] = userInfo
  142. callback()
  143. async.series jobs, (error) ->
  144. return callback(error) if error?
  145. for update in updates
  146. user_id = update.meta.user_id
  147. delete update.meta.user_id
  148. if UpdatesManager._validUserId(user_id)
  149. update.meta.user = users[user_id]
  150. callback null, updates
  151. _validUserId: (user_id) ->
  152. if !user_id?
  153. return false
  154. else
  155. return !!user_id.match(/^[a-f0-9]{24}$/)
  156. TIME_BETWEEN_DISTINCT_UPDATES: fiveMinutes = 5 * 60 * 1000
  157. _summarizeUpdates: (updates, existingSummarizedUpdates = []) ->
  158. summarizedUpdates = existingSummarizedUpdates.slice()
  159. for update in updates
  160. earliestUpdate = summarizedUpdates[summarizedUpdates.length - 1]
  161. if earliestUpdate and earliestUpdate.meta.start_ts - update.meta.end_ts < @TIME_BETWEEN_DISTINCT_UPDATES
  162. if update.meta.user?
  163. userExists = false
  164. for user in earliestUpdate.meta.users
  165. if user.id == update.meta.user.id
  166. userExists = true
  167. break
  168. if !userExists
  169. earliestUpdate.meta.users.push update.meta.user
  170. doc_id = update.doc_id.toString()
  171. doc = earliestUpdate.docs[doc_id]
  172. if doc?
  173. doc.fromV = Math.min(doc.fromV, update.v)
  174. doc.toV = Math.max(doc.toV, update.v)
  175. else
  176. earliestUpdate.docs[doc_id] =
  177. fromV: update.v
  178. toV: update.v
  179. earliestUpdate.meta.start_ts = Math.min(earliestUpdate.meta.start_ts, update.meta.start_ts)
  180. earliestUpdate.meta.end_ts = Math.max(earliestUpdate.meta.end_ts, update.meta.end_ts)
  181. else
  182. newUpdate =
  183. meta:
  184. users: []
  185. start_ts: update.meta.start_ts
  186. end_ts: update.meta.end_ts
  187. docs: {}
  188. newUpdate.docs[update.doc_id.toString()] =
  189. fromV: update.v
  190. toV: update.v
  191. if update.meta.user?
  192. newUpdate.meta.users.push update.meta.user
  193. summarizedUpdates.push newUpdate
  194. return summarizedUpdates