UpdateManager.coffee 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. LockManager = require "./LockManager"
  2. RedisManager = require "./RedisManager"
  3. RealTimeRedisManager = require "./RealTimeRedisManager"
  4. ShareJsUpdateManager = require "./ShareJsUpdateManager"
  5. HistoryManager = require "./HistoryManager"
  6. Settings = require('settings-sharelatex')
  7. _ = require("underscore")
  8. async = require("async")
  9. logger = require('logger-sharelatex')
  10. Metrics = require "./Metrics"
  11. Errors = require "./Errors"
  12. DocumentManager = require "./DocumentManager"
  13. RangesManager = require "./RangesManager"
  14. Profiler = require "./Profiler"
  15. module.exports = UpdateManager =
  16. processOutstandingUpdates: (project_id, doc_id, callback = (error) ->) ->
  17. timer = new Metrics.Timer("updateManager.processOutstandingUpdates")
  18. UpdateManager.fetchAndApplyUpdates project_id, doc_id, (error) ->
  19. timer.done()
  20. return callback(error) if error?
  21. callback()
  22. processOutstandingUpdatesWithLock: (project_id, doc_id, callback = (error) ->) ->
  23. profile = new Profiler("processOutstandingUpdatesWithLock", {project_id, doc_id})
  24. LockManager.tryLock doc_id, (error, gotLock, lockValue) =>
  25. return callback(error) if error?
  26. return callback() if !gotLock
  27. profile.log("tryLock")
  28. UpdateManager.processOutstandingUpdates project_id, doc_id, (error) ->
  29. return UpdateManager._handleErrorInsideLock(doc_id, lockValue, error, callback) if error?
  30. profile.log("processOutstandingUpdates")
  31. LockManager.releaseLock doc_id, lockValue, (error) =>
  32. return callback(error) if error?
  33. profile.log("releaseLock").end()
  34. UpdateManager.continueProcessingUpdatesWithLock project_id, doc_id, callback
  35. continueProcessingUpdatesWithLock: (project_id, doc_id, callback = (error) ->) ->
  36. RealTimeRedisManager.getUpdatesLength doc_id, (error, length) =>
  37. return callback(error) if error?
  38. if length > 0
  39. UpdateManager.processOutstandingUpdatesWithLock project_id, doc_id, callback
  40. else
  41. callback()
  42. fetchAndApplyUpdates: (project_id, doc_id, callback = (error) ->) ->
  43. profile = new Profiler("fetchAndApplyUpdates", {project_id, doc_id})
  44. RealTimeRedisManager.getPendingUpdatesForDoc doc_id, (error, updates) =>
  45. return callback(error) if error?
  46. logger.log {project_id: project_id, doc_id: doc_id, count: updates.length}, "processing updates"
  47. if updates.length == 0
  48. return callback()
  49. profile.log("getPendingUpdatesForDoc")
  50. doUpdate = (update, cb)->
  51. UpdateManager.applyUpdate project_id, doc_id, update, (err) ->
  52. profile.log("applyUpdate")
  53. cb(err)
  54. finalCallback = (err) ->
  55. profile.log("async done").end()
  56. callback(err)
  57. async.eachSeries updates, doUpdate, finalCallback
  58. applyUpdate: (project_id, doc_id, update, _callback = (error) ->) ->
  59. callback = (error) ->
  60. if error?
  61. RealTimeRedisManager.sendData {project_id, doc_id, error: error.message || error}
  62. profile.log("sendData")
  63. profile.end()
  64. _callback(error)
  65. profile = new Profiler("applyUpdate", {project_id, doc_id})
  66. UpdateManager._sanitizeUpdate update
  67. profile.log("sanitizeUpdate")
  68. DocumentManager.getDoc project_id, doc_id, (error, lines, version, ranges, pathname, projectHistoryId) ->
  69. profile.log("getDoc")
  70. return callback(error) if error?
  71. if !lines? or !version?
  72. return callback(new Errors.NotFoundError("document not found: #{doc_id}"))
  73. ShareJsUpdateManager.applyUpdate project_id, doc_id, update, lines, version, (error, updatedDocLines, version, appliedOps) ->
  74. profile.log("sharejs.applyUpdate")
  75. return callback(error) if error?
  76. RangesManager.applyUpdate project_id, doc_id, ranges, appliedOps, updatedDocLines, (error, new_ranges) ->
  77. UpdateManager._addProjectHistoryMetadataToOps(appliedOps, pathname, projectHistoryId, lines)
  78. profile.log("RangesManager.applyUpdate")
  79. return callback(error) if error?
  80. RedisManager.updateDocument project_id, doc_id, updatedDocLines, version, appliedOps, new_ranges, (error, doc_ops_length, project_ops_length) ->
  81. profile.log("RedisManager.updateDocument")
  82. return callback(error) if error?
  83. HistoryManager.recordAndFlushHistoryOps project_id, doc_id, appliedOps, doc_ops_length, project_ops_length, (error) ->
  84. profile.log("recordAndFlushHistoryOps")
  85. callback(error)
  86. lockUpdatesAndDo: (method, project_id, doc_id, args..., callback) ->
  87. profile = new Profiler("lockUpdatesAndDo", {project_id, doc_id})
  88. LockManager.getLock doc_id, (error, lockValue) ->
  89. profile.log("getLock")
  90. return callback(error) if error?
  91. UpdateManager.processOutstandingUpdates project_id, doc_id, (error) ->
  92. return UpdateManager._handleErrorInsideLock(doc_id, lockValue, error, callback) if error?
  93. profile.log("processOutstandingUpdates")
  94. method project_id, doc_id, args..., (error, response_args...) ->
  95. return UpdateManager._handleErrorInsideLock(doc_id, lockValue, error, callback) if error?
  96. profile.log("method")
  97. LockManager.releaseLock doc_id, lockValue, (error) ->
  98. return callback(error) if error?
  99. profile.log("releaseLock").end()
  100. callback null, response_args...
  101. # We held the lock for a while so updates might have queued up
  102. UpdateManager.continueProcessingUpdatesWithLock project_id, doc_id
  103. _handleErrorInsideLock: (doc_id, lockValue, original_error, callback = (error) ->) ->
  104. LockManager.releaseLock doc_id, lockValue, (lock_error) ->
  105. callback(original_error)
  106. _sanitizeUpdate: (update) ->
  107. # In Javascript, characters are 16-bits wide. It does not understand surrogates as characters.
  108. #
  109. # From Wikipedia (http://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane):
  110. # "The High Surrogates (U+D800–U+DBFF) and Low Surrogate (U+DC00–U+DFFF) codes are reserved
  111. # for encoding non-BMP characters in UTF-16 by using a pair of 16-bit codes: one High Surrogate
  112. # and one Low Surrogate. A single surrogate code point will never be assigned a character.""
  113. #
  114. # The main offender seems to be \uD835 as a stand alone character, which would be the first
  115. # 16-bit character of a blackboard bold character (http://www.fileformat.info/info/unicode/char/1d400/index.htm).
  116. # Something must be going on client side that is screwing up the encoding and splitting the
  117. # two 16-bit characters so that \uD835 is standalone.
  118. for op in update.op or []
  119. if op.i?
  120. # Replace high and low surrogate characters with 'replacement character' (\uFFFD)
  121. op.i = op.i.replace(/[\uD800-\uDFFF]/g, "\uFFFD")
  122. return update
  123. _addProjectHistoryMetadataToOps: (updates, pathname, projectHistoryId, lines) ->
  124. doc_length = _.reduce lines,
  125. (chars, line) -> chars + line.length,
  126. 0
  127. doc_length += lines.length - 1 # count newline characters
  128. updates.forEach (update) ->
  129. update.projectHistoryId = projectHistoryId
  130. update.meta ||= {}
  131. update.meta.pathname = pathname
  132. update.meta.doc_length = doc_length
  133. # Each update may contain multiple ops, i.e.
  134. # [{
  135. # ops: [{i: "foo", p: 4}, {d: "bar", p:8}]
  136. # }, {
  137. # ops: [{d: "baz", p: 40}, {i: "qux", p:8}]
  138. # }]
  139. # We want to include the doc_length at the start of each update,
  140. # before it's ops are applied. However, we need to track any
  141. # changes to it for the next update.
  142. for op in update.op
  143. if op.i?
  144. doc_length += op.i.length
  145. if op.d?
  146. doc_length -= op.d.length