fix_oversized_docs.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const fs = require('fs')
  2. const minimist = require('minimist')
  3. const { waitForDb, ObjectId } = require('../app/src/infrastructure/mongodb')
  4. const DocstoreManager = require('../app/src/Features/Docstore/DocstoreManager')
  5. const FileStoreHandler = require('../app/src/Features/FileStore/FileStoreHandler')
  6. const FileWriter = require('../app/src/infrastructure/FileWriter')
  7. const ProjectEntityMongoUpdateHandler = require('../app/src/Features/Project/ProjectEntityMongoUpdateHandler')
  8. const RedisWrapper = require('@overleaf/redis-wrapper')
  9. const Settings = require('@overleaf/settings')
  10. const opts = parseArgs()
  11. const redis = RedisWrapper.createClient(Settings.redis.web)
  12. function parseArgs() {
  13. const args = minimist(process.argv.slice(2), {
  14. boolean: ['commit', 'ignore-ranges'],
  15. })
  16. const projectIds = args._
  17. if (projectIds.length === 0) {
  18. console.log(`Usage: ${process.argv[1]} [OPTS] PROJECT_ID
  19. Options:
  20. --commit Actually convert oversized docs to binary files
  21. --max-doc-size Size over which docs are converted to binary files
  22. --ignore-ranges Convert docs even if they contain ranges
  23. `)
  24. process.exit(0)
  25. }
  26. const commit = args.commit
  27. const ignoreRanges = args['ignore-ranges']
  28. const maxDocSize = args['max-doc-size']
  29. ? parseInt(args['max-doc-size'], 10)
  30. : 2 * 1024 * 1024
  31. return { projectIds, commit, ignoreRanges, maxDocSize }
  32. }
  33. async function main() {
  34. await waitForDb()
  35. for (const projectId of opts.projectIds) {
  36. await processProject(projectId)
  37. }
  38. if (!opts.commit) {
  39. console.log('This was a dry run. Re-run with --commit to apply changes')
  40. }
  41. }
  42. async function processProject(projectId) {
  43. const docIds = await getDocIds(projectId)
  44. for (const docId of docIds) {
  45. await processDoc(projectId, docId)
  46. }
  47. }
  48. async function processDoc(projectId, docId) {
  49. const doc = await getDocFromRedis(docId)
  50. const size = doc.lines.reduce((sum, line) => sum + line.length + 1, 0)
  51. if (size > opts.maxDocSize) {
  52. if (
  53. !opts.ignoreRanges &&
  54. ((doc.ranges.comments && doc.ranges.comments.length > 0) ||
  55. (doc.ranges.changes && doc.ranges.changes.length > 0))
  56. ) {
  57. console.log(
  58. `Skipping doc ${doc.path} in project ${projectId} because it has ranges`
  59. )
  60. return
  61. }
  62. console.log(
  63. `Converting doc ${doc.path} in project ${projectId} to binary (${size} bytes)`
  64. )
  65. if (opts.commit) {
  66. const fileRef = await sendDocToFilestore(projectId, doc)
  67. await ProjectEntityMongoUpdateHandler.promises.replaceDocWithFile(
  68. ObjectId(projectId),
  69. ObjectId(docId),
  70. fileRef
  71. )
  72. await deleteDocFromMongo(projectId, doc)
  73. await deleteDocFromRedis(projectId, docId)
  74. }
  75. }
  76. }
  77. async function getDocIds(projectId) {
  78. const docIds = await redis.smembers(`DocsIn:{${projectId}}`)
  79. return docIds
  80. }
  81. async function getDocFromRedis(docId) {
  82. const lines = await redis.get(`doclines:{${docId}}`)
  83. const path = await redis.get(`Pathname:{${docId}}`)
  84. const ranges = await redis.get(`Ranges:{${docId}}`)
  85. return {
  86. id: docId,
  87. lines: JSON.parse(lines),
  88. ranges: ranges ? JSON.parse(ranges) : {},
  89. path,
  90. }
  91. }
  92. async function sendDocToFilestore(projectId, doc) {
  93. const basename = doc.path.split('/').pop()
  94. const tmpFilePath = await FileWriter.promises.writeLinesToDisk(
  95. projectId,
  96. doc.lines
  97. )
  98. try {
  99. const { fileRef } = await FileStoreHandler.promises.uploadFileFromDisk(
  100. projectId,
  101. { name: basename, rev: doc.version + 1 },
  102. tmpFilePath
  103. )
  104. return fileRef
  105. } finally {
  106. fs.promises.unlink(tmpFilePath)
  107. }
  108. }
  109. async function deleteDocFromMongo(projectId, doc) {
  110. const basename = doc.path.split('/').pop()
  111. const deletedAt = new Date()
  112. await DocstoreManager.promises.deleteDoc(
  113. projectId,
  114. doc.id,
  115. basename,
  116. deletedAt
  117. )
  118. }
  119. async function deleteDocFromRedis(projectId, docId) {
  120. await redis.del(
  121. `Blocking:{${docId}}`,
  122. `doclines:{${docId}}`,
  123. `DocOps:{${docId}}`,
  124. `DocVersion:{${docId}}`,
  125. `DocHash:{${docId}}`,
  126. `ProjectId:{${docId}}`,
  127. `Ranges:{${docId}}`,
  128. `UnflushedTime:{${docId}}`,
  129. `Pathname:{${docId}}`,
  130. `ProjectHistoryId:{${docId}}`,
  131. `ProjectHistoryType:{${docId}}`,
  132. `PendingUpdates:{${docId}}`,
  133. `lastUpdatedAt:{${docId}}`,
  134. `lastUpdatedBy:{${docId}}`
  135. )
  136. await redis.srem(`DocsIn:{${projectId}}`, projectId)
  137. }
  138. main()
  139. .then(() => {
  140. process.exit(0)
  141. })
  142. .catch(err => {
  143. console.error(err)
  144. process.exit(1)
  145. })