RedisManager.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS205: Consider reworking code to avoid use of IIFEs
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let RedisManager
  16. const Settings = require('settings-sharelatex')
  17. const redis = require('@overleaf/redis-wrapper')
  18. const rclient = redis.createClient(Settings.redis.history)
  19. const Keys = Settings.redis.history.key_schema
  20. const async = require('async')
  21. module.exports = RedisManager = {
  22. getOldestDocUpdates(doc_id, batchSize, callback) {
  23. if (callback == null) {
  24. callback = function (error, jsonUpdates) {}
  25. }
  26. const key = Keys.uncompressedHistoryOps({ doc_id })
  27. return rclient.lrange(key, 0, batchSize - 1, callback)
  28. },
  29. expandDocUpdates(jsonUpdates, callback) {
  30. let rawUpdates
  31. if (callback == null) {
  32. callback = function (error, rawUpdates) {}
  33. }
  34. try {
  35. rawUpdates = Array.from(jsonUpdates || []).map((update) =>
  36. JSON.parse(update)
  37. )
  38. } catch (e) {
  39. return callback(e)
  40. }
  41. return callback(null, rawUpdates)
  42. },
  43. deleteAppliedDocUpdates(project_id, doc_id, docUpdates, callback) {
  44. if (callback == null) {
  45. callback = function (error) {}
  46. }
  47. const multi = rclient.multi()
  48. // Delete all the updates which have been applied (exact match)
  49. for (const update of Array.from(docUpdates || [])) {
  50. multi.lrem(Keys.uncompressedHistoryOps({ doc_id }), 1, update)
  51. }
  52. return multi.exec(function (error, results) {
  53. if (error != null) {
  54. return callback(error)
  55. }
  56. // It's ok to delete the doc_id from the set here. Even though the list
  57. // of updates may not be empty, we will continue to process it until it is.
  58. return rclient.srem(
  59. Keys.docsWithHistoryOps({ project_id }),
  60. doc_id,
  61. function (error) {
  62. if (error != null) {
  63. return callback(error)
  64. }
  65. return callback(null)
  66. }
  67. )
  68. })
  69. },
  70. getDocIdsWithHistoryOps(project_id, callback) {
  71. if (callback == null) {
  72. callback = function (error, doc_ids) {}
  73. }
  74. return rclient.smembers(Keys.docsWithHistoryOps({ project_id }), callback)
  75. },
  76. // iterate over keys asynchronously using redis scan (non-blocking)
  77. // handle all the cluster nodes or single redis server
  78. _getKeys(pattern, callback) {
  79. const nodes = (typeof rclient.nodes === 'function'
  80. ? rclient.nodes('master')
  81. : undefined) || [rclient]
  82. const doKeyLookupForNode = (node, cb) =>
  83. RedisManager._getKeysFromNode(node, pattern, cb)
  84. return async.concatSeries(nodes, doKeyLookupForNode, callback)
  85. },
  86. _getKeysFromNode(node, pattern, callback) {
  87. let cursor = 0 // redis iterator
  88. const keySet = {} // use hash to avoid duplicate results
  89. // scan over all keys looking for pattern
  90. var doIteration = (cb) =>
  91. node.scan(cursor, 'MATCH', pattern, 'COUNT', 1000, function (
  92. error,
  93. reply
  94. ) {
  95. let keys
  96. if (error != null) {
  97. return callback(error)
  98. }
  99. ;[cursor, keys] = Array.from(reply)
  100. for (const key of Array.from(keys)) {
  101. keySet[key] = true
  102. }
  103. if (cursor === '0') {
  104. // note redis returns string result not numeric
  105. return callback(null, Object.keys(keySet))
  106. } else {
  107. return doIteration()
  108. }
  109. })
  110. return doIteration()
  111. },
  112. // extract ids from keys like DocsWithHistoryOps:57fd0b1f53a8396d22b2c24b
  113. // or DocsWithHistoryOps:{57fd0b1f53a8396d22b2c24b} (for redis cluster)
  114. _extractIds(keyList) {
  115. const ids = (() => {
  116. const result = []
  117. for (const key of Array.from(keyList)) {
  118. const m = key.match(/:\{?([0-9a-f]{24})\}?/) // extract object id
  119. result.push(m[1])
  120. }
  121. return result
  122. })()
  123. return ids
  124. },
  125. getProjectIdsWithHistoryOps(callback) {
  126. if (callback == null) {
  127. callback = function (error, project_ids) {}
  128. }
  129. return RedisManager._getKeys(
  130. Keys.docsWithHistoryOps({ project_id: '*' }),
  131. function (error, project_keys) {
  132. if (error != null) {
  133. return callback(error)
  134. }
  135. const project_ids = RedisManager._extractIds(project_keys)
  136. return callback(error, project_ids)
  137. }
  138. )
  139. },
  140. getAllDocIdsWithHistoryOps(callback) {
  141. // return all the docids, to find dangling history entries after
  142. // everything is flushed.
  143. if (callback == null) {
  144. callback = function (error, doc_ids) {}
  145. }
  146. return RedisManager._getKeys(
  147. Keys.uncompressedHistoryOps({ doc_id: '*' }),
  148. function (error, doc_keys) {
  149. if (error != null) {
  150. return callback(error)
  151. }
  152. const doc_ids = RedisManager._extractIds(doc_keys)
  153. return callback(error, doc_ids)
  154. }
  155. )
  156. }
  157. }