fix_oversized_docs.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import fs from 'fs'
  2. import minimist from 'minimist'
  3. import { ObjectId } from '../app/src/infrastructure/mongodb.js'
  4. import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js'
  5. import FileStoreHandler from '../app/src/Features/FileStore/FileStoreHandler.js'
  6. import FileWriter from '../app/src/infrastructure/FileWriter.js'
  7. import ProjectEntityMongoUpdateHandler from '../app/src/Features/Project/ProjectEntityMongoUpdateHandler.js'
  8. import ProjectLocator from '../app/src/Features/Project/ProjectLocator.js'
  9. import RedisWrapper from '@overleaf/redis-wrapper'
  10. import Settings from '@overleaf/settings'
  11. const opts = parseArgs()
  12. const redis = RedisWrapper.createClient(Settings.redis.web)
  13. function parseArgs() {
  14. const args = minimist(process.argv.slice(2), {
  15. boolean: ['commit', 'ignore-ranges'],
  16. })
  17. const projectIds = args._
  18. if (projectIds.length === 0) {
  19. console.log(`Usage: ${process.argv[1]} [OPTS] PROJECT_ID
  20. Options:
  21. --commit Actually convert oversized docs to binary files
  22. --max-doc-size Size over which docs are converted to binary files
  23. --ignore-ranges Convert docs even if they contain ranges
  24. `)
  25. process.exit(0)
  26. }
  27. const commit = args.commit
  28. const ignoreRanges = args['ignore-ranges']
  29. const maxDocSize = args['max-doc-size']
  30. ? parseInt(args['max-doc-size'], 10)
  31. : 2 * 1024 * 1024
  32. return { projectIds, commit, ignoreRanges, maxDocSize }
  33. }
  34. async function main() {
  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 getDoc(projectId, 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. new ObjectId(projectId),
  69. new 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 getDoc(projectId, docId) {
  82. const lines = await redis.get(`doclines:{${docId}}`)
  83. const ranges = await redis.get(`Ranges:{${docId}}`)
  84. const { path } = await ProjectLocator.promises.findElement({
  85. project_id: projectId,
  86. element_id: docId,
  87. type: 'doc',
  88. })
  89. return {
  90. id: docId,
  91. lines: JSON.parse(lines),
  92. ranges: ranges ? JSON.parse(ranges) : {},
  93. path: path.fileSystem,
  94. }
  95. }
  96. async function sendDocToFilestore(projectId, doc) {
  97. const basename = doc.path.split('/').pop()
  98. const tmpFilePath = await FileWriter.promises.writeLinesToDisk(
  99. projectId,
  100. doc.lines
  101. )
  102. try {
  103. const { fileRef } = await FileStoreHandler.promises.uploadFileFromDisk(
  104. projectId,
  105. { name: basename, rev: doc.version + 1 },
  106. tmpFilePath
  107. )
  108. return fileRef
  109. } finally {
  110. fs.promises.unlink(tmpFilePath)
  111. }
  112. }
  113. async function deleteDocFromMongo(projectId, doc) {
  114. const basename = doc.path.split('/').pop()
  115. const deletedAt = new Date()
  116. await DocstoreManager.promises.deleteDoc(
  117. projectId,
  118. doc.id,
  119. basename,
  120. deletedAt
  121. )
  122. }
  123. async function deleteDocFromRedis(projectId, docId) {
  124. await redis.del(
  125. `Blocking:{${docId}}`,
  126. `doclines:{${docId}}`,
  127. `DocOps:{${docId}}`,
  128. `DocVersion:{${docId}}`,
  129. `DocHash:{${docId}}`,
  130. `ProjectId:{${docId}}`,
  131. `Ranges:{${docId}}`,
  132. `UnflushedTime:{${docId}}`,
  133. `Pathname:{${docId}}`,
  134. `ProjectHistoryId:{${docId}}`,
  135. `PendingUpdates:{${docId}}`,
  136. `lastUpdatedAt:{${docId}}`,
  137. `lastUpdatedBy:{${docId}}`
  138. )
  139. await redis.srem(`DocsIn:{${projectId}}`, projectId)
  140. }
  141. try {
  142. await main()
  143. process.exit(0)
  144. } catch (error) {
  145. console.error(error)
  146. process.exit(1)
  147. }