expire_redis_chunks.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const logger = require('@overleaf/logger')
  2. const commandLineArgs = require('command-line-args') // Add this line
  3. const redis = require('../lib/redis')
  4. const { scanRedisCluster, extractKeyId } = require('../lib/scan')
  5. const { expireCurrentChunk } = require('../lib/chunk_store/redis')
  6. const rclient = redis.rclientHistory
  7. const EXPIRE_TIME_KEY_PATTERN = `expire-time:{*}`
  8. const optionDefinitions = [{ name: 'dry-run', alias: 'd', type: Boolean }]
  9. const options = commandLineArgs(optionDefinitions)
  10. const DRY_RUN = options['dry-run'] || false
  11. logger.initialize('expire-redis-chunks')
  12. function isExpiredKey(expireTimestamp, currentTime) {
  13. const expireTime = parseInt(expireTimestamp, 10)
  14. if (isNaN(expireTime)) {
  15. return false
  16. }
  17. logger.debug(
  18. {
  19. expireTime,
  20. currentTime,
  21. expireIn: expireTime - currentTime,
  22. expired: currentTime > expireTime,
  23. },
  24. 'Checking if key is expired'
  25. )
  26. return currentTime > expireTime
  27. }
  28. async function processKeysBatch(keysBatch, rclient) {
  29. let clearedKeyCount = 0
  30. if (keysBatch.length === 0) {
  31. return 0
  32. }
  33. // For efficiency, we use MGET to fetch all the timestamps in a single request
  34. const expireTimestamps = await rclient.mget(keysBatch)
  35. const currentTime = Date.now()
  36. for (let i = 0; i < keysBatch.length; i++) {
  37. const key = keysBatch[i]
  38. // For each key, do a quick check to see if the key is expired before calling
  39. // the LUA script to expire the chunk atomically.
  40. if (isExpiredKey(expireTimestamps[i], currentTime)) {
  41. const projectId = extractKeyId(key)
  42. if (DRY_RUN) {
  43. logger.info({ projectId }, '[Dry Run] Would expire chunk for project')
  44. } else {
  45. await expireCurrentChunk(projectId)
  46. }
  47. clearedKeyCount++
  48. }
  49. }
  50. return clearedKeyCount
  51. }
  52. async function expireRedisChunks() {
  53. let scannedKeyCount = 0
  54. let clearedKeyCount = 0
  55. const START_TIME = Date.now()
  56. if (DRY_RUN) {
  57. // Use global DRY_RUN
  58. logger.info({}, 'starting expireRedisChunks scan in DRY RUN mode')
  59. } else {
  60. logger.info({}, 'starting expireRedisChunks scan')
  61. }
  62. for await (const keysBatch of scanRedisCluster(
  63. rclient,
  64. EXPIRE_TIME_KEY_PATTERN
  65. )) {
  66. scannedKeyCount += keysBatch.length
  67. clearedKeyCount += await processKeysBatch(keysBatch, rclient)
  68. if (scannedKeyCount % 1000 === 0) {
  69. logger.info(
  70. { scannedKeyCount, clearedKeyCount },
  71. 'expireRedisChunks scan progress'
  72. )
  73. }
  74. }
  75. logger.info(
  76. {
  77. scannedKeyCount,
  78. clearedKeyCount,
  79. elapsedTimeInSeconds: Math.floor((Date.now() - START_TIME) / 1000),
  80. dryRun: DRY_RUN,
  81. },
  82. 'expireRedisChunks scan complete'
  83. )
  84. await redis.disconnect()
  85. }
  86. expireRedisChunks().catch(err => {
  87. logger.fatal({ err }, 'unhandled error in expireRedisChunks')
  88. process.exit(1)
  89. })