DocArchiveManager.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. const { callbackify } = require('util')
  2. const MongoManager = require('./MongoManager').promises
  3. const Errors = require('./Errors')
  4. const logger = require('logger-sharelatex')
  5. const settings = require('@overleaf/settings')
  6. const crypto = require('crypto')
  7. const Streamifier = require('streamifier')
  8. const RangeManager = require('./RangeManager')
  9. const PersistorManager = require('./PersistorManager')
  10. const pMap = require('p-map')
  11. const PARALLEL_JOBS = settings.parallelArchiveJobs
  12. const ARCHIVE_BATCH_SIZE = settings.archiveBatchSize
  13. const UN_ARCHIVE_BATCH_SIZE = settings.unArchiveBatchSize
  14. const DESTROY_BATCH_SIZE = settings.destroyBatchSize
  15. const DESTROY_RETRY_COUNT = settings.destroyRetryCount
  16. module.exports = {
  17. archiveAllDocs: callbackify(archiveAllDocs),
  18. archiveDocById: callbackify(archiveDocById),
  19. archiveDoc: callbackify(archiveDoc),
  20. unArchiveAllDocs: callbackify(unArchiveAllDocs),
  21. unarchiveDoc: callbackify(unarchiveDoc),
  22. destroyAllDocs: callbackify(destroyAllDocs),
  23. destroyDoc: callbackify(destroyDoc),
  24. getDoc: callbackify(getDoc),
  25. promises: {
  26. archiveAllDocs,
  27. archiveDocById,
  28. archiveDoc,
  29. unArchiveAllDocs,
  30. unarchiveDoc,
  31. destroyAllDocs,
  32. destroyDoc,
  33. getDoc,
  34. },
  35. }
  36. async function archiveAllDocs(projectId) {
  37. while (true) {
  38. const docs = await MongoManager.getNonArchivedProjectDocs(
  39. projectId,
  40. ARCHIVE_BATCH_SIZE
  41. )
  42. if (!docs || docs.length === 0) {
  43. break
  44. }
  45. await pMap(docs, doc => archiveDoc(projectId, doc), {
  46. concurrency: PARALLEL_JOBS,
  47. })
  48. }
  49. }
  50. async function archiveDocById(projectId, docId) {
  51. const doc = await MongoManager.findDoc(projectId, docId, {
  52. lines: true,
  53. ranges: true,
  54. rev: true,
  55. inS3: true,
  56. })
  57. if (!doc) {
  58. throw new Errors.NotFoundError(
  59. `Cannot find doc ${docId} in project ${projectId}`
  60. )
  61. }
  62. // TODO(das7pad): consider refactoring MongoManager.findDoc to take a query
  63. if (doc.inS3) return
  64. return archiveDoc(projectId, doc)
  65. }
  66. async function archiveDoc(projectId, doc) {
  67. logger.log(
  68. { project_id: projectId, doc_id: doc._id },
  69. 'sending doc to persistor'
  70. )
  71. const key = `${projectId}/${doc._id}`
  72. if (doc.lines == null) {
  73. throw new Error('doc has no lines')
  74. }
  75. const json = JSON.stringify({
  76. lines: doc.lines,
  77. ranges: doc.ranges,
  78. schema_v: 1,
  79. })
  80. // this should never happen, but protects against memory-corruption errors that
  81. // have happened in the past
  82. if (json.indexOf('\u0000') > -1) {
  83. const error = new Error('null bytes detected')
  84. logger.err({ err: error, doc }, error.message)
  85. throw error
  86. }
  87. const md5 = crypto.createHash('md5').update(json).digest('hex')
  88. const stream = Streamifier.createReadStream(json)
  89. await PersistorManager.sendStream(settings.docstore.bucket, key, stream, {
  90. sourceMd5: md5,
  91. })
  92. await MongoManager.markDocAsArchived(doc._id, doc.rev)
  93. }
  94. async function unArchiveAllDocs(projectId) {
  95. while (true) {
  96. let docs
  97. if (settings.docstore.keepSoftDeletedDocsArchived) {
  98. docs = await MongoManager.getNonDeletedArchivedProjectDocs(
  99. projectId,
  100. UN_ARCHIVE_BATCH_SIZE
  101. )
  102. } else {
  103. docs = await MongoManager.getArchivedProjectDocs(
  104. projectId,
  105. UN_ARCHIVE_BATCH_SIZE
  106. )
  107. }
  108. if (!docs || docs.length === 0) {
  109. break
  110. }
  111. await pMap(docs, doc => unarchiveDoc(projectId, doc._id), {
  112. concurrency: PARALLEL_JOBS,
  113. })
  114. }
  115. }
  116. // get the doc from the PersistorManager without storing it in mongo
  117. async function getDoc(projectId, docId) {
  118. const key = `${projectId}/${docId}`
  119. const sourceMd5 = await PersistorManager.getObjectMd5Hash(
  120. settings.docstore.bucket,
  121. key
  122. )
  123. const stream = await PersistorManager.getObjectStream(
  124. settings.docstore.bucket,
  125. key
  126. )
  127. stream.resume()
  128. const json = await _streamToString(stream)
  129. const md5 = crypto.createHash('md5').update(json).digest('hex')
  130. if (sourceMd5 !== md5) {
  131. throw new Errors.Md5MismatchError('md5 mismatch when downloading doc', {
  132. key,
  133. sourceMd5,
  134. md5,
  135. })
  136. }
  137. const doc = JSON.parse(json)
  138. const mongoDoc = {}
  139. if (doc.schema_v === 1 && doc.lines != null) {
  140. mongoDoc.lines = doc.lines
  141. if (doc.ranges != null) {
  142. mongoDoc.ranges = RangeManager.jsonRangesToMongo(doc.ranges)
  143. }
  144. } else if (Array.isArray(doc)) {
  145. mongoDoc.lines = doc
  146. } else {
  147. throw new Error("I don't understand the doc format in s3")
  148. }
  149. return mongoDoc
  150. }
  151. // get the doc and unarchive it to mongo
  152. async function unarchiveDoc(projectId, docId) {
  153. logger.log(
  154. { project_id: projectId, doc_id: docId },
  155. 'getting doc from persistor'
  156. )
  157. const key = `${projectId}/${docId}`
  158. const originalDoc = await MongoManager.findDoc(projectId, docId, { inS3: 1 })
  159. if (!originalDoc.inS3) {
  160. // return if it's not actually in S3 as there's nothing to do
  161. return
  162. }
  163. let mongoDoc
  164. try {
  165. mongoDoc = await getDoc(projectId, docId)
  166. } catch (err) {
  167. // if we get a 404, we could be in a race and something else has unarchived the doc already
  168. if (err instanceof Errors.NotFoundError) {
  169. const doc = await MongoManager.findDoc(projectId, docId, { inS3: 1 })
  170. if (!doc.inS3) {
  171. // the doc has been archived while we were looking for it, so no error
  172. return
  173. }
  174. }
  175. throw err
  176. }
  177. await MongoManager.upsertIntoDocCollection(projectId, docId, mongoDoc)
  178. await PersistorManager.deleteObject(settings.docstore.bucket, key)
  179. }
  180. async function destroyAllDocs(projectId) {
  181. while (true) {
  182. const docs = await MongoManager.getProjectsDocs(
  183. projectId,
  184. { include_deleted: true, limit: DESTROY_BATCH_SIZE },
  185. { _id: 1 }
  186. )
  187. if (!docs || docs.length === 0) {
  188. break
  189. }
  190. await pMap(docs, doc => destroyDoc(projectId, doc._id), {
  191. concurrency: PARALLEL_JOBS,
  192. })
  193. }
  194. }
  195. async function destroyDoc(projectId, docId) {
  196. logger.log(
  197. { project_id: projectId, doc_id: docId },
  198. 'removing doc from mongo and persistor'
  199. )
  200. const doc = await MongoManager.findDoc(projectId, docId, {
  201. inS3: 1,
  202. })
  203. if (!doc) {
  204. throw new Errors.NotFoundError('Doc not found in Mongo')
  205. }
  206. if (doc.inS3) {
  207. await destroyArchiveWithRetry(projectId, docId)
  208. }
  209. await MongoManager.destroyDoc(docId)
  210. }
  211. async function destroyArchiveWithRetry(projectId, docId) {
  212. let attempt = 0
  213. let lastError
  214. while (attempt++ <= DESTROY_RETRY_COUNT) {
  215. try {
  216. await PersistorManager.deleteObject(
  217. settings.docstore.bucket,
  218. `${projectId}/${docId}`
  219. )
  220. return
  221. } catch (err) {
  222. lastError = err
  223. logger.warn(
  224. { projectId, docId, err, attempt },
  225. 'destroying archive failed'
  226. )
  227. }
  228. }
  229. throw lastError
  230. }
  231. async function _streamToString(stream) {
  232. const chunks = []
  233. return new Promise((resolve, reject) => {
  234. stream.on('data', chunk => chunks.push(chunk))
  235. stream.on('error', reject)
  236. stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
  237. })
  238. }