scan.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. const logger = require('@overleaf/logger')
  2. const { JobNotFoundError, JobNotReadyError } = require('./chunk_store/errors')
  3. const BATCH_SIZE = 1000 // Default batch size for SCAN
  4. /**
  5. * Asynchronously scans a Redis instance or cluster for keys matching a pattern.
  6. *
  7. * This function handles both standalone Redis instances and Redis clusters.
  8. * For clusters, it iterates over all master nodes. It yields keys in batches
  9. * as they are found by the SCAN command.
  10. *
  11. * @param {object} redisClient - The Redis client instance (from @overleaf/redis-wrapper).
  12. * @param {string} pattern - The pattern to match keys against (e.g., 'user:*').
  13. * @param {number} [count=BATCH_SIZE] - Optional hint for Redis SCAN count per iteration.
  14. * @yields {string[]} A batch of matching keys.
  15. */
  16. async function* scanRedisCluster(redisClient, pattern, count = BATCH_SIZE) {
  17. const nodes = redisClient.nodes ? redisClient.nodes('master') : [redisClient]
  18. for (const node of nodes) {
  19. let cursor = '0'
  20. do {
  21. // redisClient from @overleaf/redis-wrapper uses ioredis style commands
  22. const [nextCursor, keys] = await node.scan(
  23. cursor,
  24. 'MATCH',
  25. pattern,
  26. 'COUNT',
  27. count
  28. )
  29. cursor = nextCursor
  30. if (keys.length > 0) {
  31. yield keys
  32. }
  33. } while (cursor !== '0')
  34. }
  35. }
  36. /**
  37. * Extracts the content within the first pair of curly braces {} from a string.
  38. * This is used to extract a user ID or project ID from a Redis key.
  39. *
  40. * @param {string} key - The input string containing content within curly braces.
  41. * @returns {string | null} The extracted content (the key ID) if found, otherwise null.
  42. */
  43. function extractKeyId(key) {
  44. const match = key.match(/\{(.*?)\}/)
  45. if (match && match[1]) {
  46. return match[1]
  47. }
  48. return null
  49. }
  50. /**
  51. * Fetches timestamps for a list of project IDs based on a given key name.
  52. *
  53. * @param {string[]} projectIds - Array of project identifiers.
  54. * @param {object} rclient - The Redis client instance.
  55. * @param {string} keyName - The base name for the Redis keys storing the timestamps (e.g., "expire-time", "persist-time").
  56. * @param {number} currentTime - The current time (timestamp in milliseconds) to compare against.
  57. * @returns {Promise<Array<{projectId: string, timestampValue: string}>>}
  58. * A promise that resolves to an array of objects, each containing a projectId and
  59. * its corresponding timestampValue, for due projects only.
  60. */
  61. async function fetchOverdueProjects(projectIds, rclient, keyName, currentTime) {
  62. if (!projectIds || projectIds.length === 0) {
  63. return []
  64. }
  65. const timestampKeys = projectIds.map(id => `${keyName}:{${id}}`)
  66. const timestamps = await rclient.mget(timestampKeys)
  67. const dueProjects = []
  68. for (let i = 0; i < projectIds.length; i++) {
  69. const projectId = projectIds[i]
  70. const timestampValue = timestamps[i]
  71. if (timestampValue !== null) {
  72. const timestamp = parseInt(timestampValue, 10)
  73. if (!isNaN(timestamp) && currentTime > timestamp) {
  74. dueProjects.push({ projectId, timestampValue })
  75. }
  76. }
  77. }
  78. return dueProjects
  79. }
  80. /**
  81. * Scans Redis for keys matching a pattern derived from keyName, identifies items that are "due" based on a timestamp,
  82. * and performs a specified action on them.
  83. *
  84. * @param {object} rclient - The Redis client instance.
  85. * @param {string} taskName - A descriptive name for the task (used in logging).
  86. * @param {string} keyName - The base name for the Redis keys (e.g., "expire-time", "persist-time").
  87. * The function will derive the key prefix as `${keyName}:` and scan pattern as `${keyName}:{*}`.
  88. * @param {function(string): Promise<void>} actionFn - An async function that takes a projectId and performs an action.
  89. * @param {boolean} DRY_RUN - If true, logs actions that would be taken without performing them.
  90. * @returns {Promise<{scannedKeyCount: number, processedKeyCount: number}>} Counts of scanned and processed keys.
  91. */
  92. async function scanAndProcessDueItems(
  93. rclient,
  94. taskName,
  95. keyName,
  96. actionFn,
  97. DRY_RUN
  98. ) {
  99. let scannedKeyCount = 0
  100. let processedKeyCount = 0
  101. const START_TIME = Date.now()
  102. const logContext = { taskName, dryRun: DRY_RUN }
  103. const scanPattern = `${keyName}:{*}`
  104. if (DRY_RUN) {
  105. logger.info(logContext, `Starting ${taskName} scan in DRY RUN mode`)
  106. } else {
  107. logger.info(logContext, `Starting ${taskName} scan`)
  108. }
  109. for await (const keysBatch of scanRedisCluster(rclient, scanPattern)) {
  110. scannedKeyCount += keysBatch.length
  111. const projectIds = keysBatch.map(extractKeyId).filter(id => id != null)
  112. if (projectIds.length === 0) {
  113. continue
  114. }
  115. const currentTime = Date.now()
  116. const overdueProjects = await fetchOverdueProjects(
  117. projectIds,
  118. rclient,
  119. keyName,
  120. currentTime
  121. )
  122. for (const project of overdueProjects) {
  123. const { projectId } = project
  124. if (DRY_RUN) {
  125. logger.info(
  126. { ...logContext, projectId },
  127. `[Dry Run] Would perform ${taskName} for project`
  128. )
  129. } else {
  130. try {
  131. await actionFn(projectId)
  132. logger.debug(
  133. { ...logContext, projectId },
  134. `Successfully performed ${taskName} for project`
  135. )
  136. } catch (err) {
  137. if (err instanceof JobNotReadyError) {
  138. // the project has been touched since the job was created
  139. logger.info(
  140. { ...logContext, projectId },
  141. `Job not ready for ${taskName} for project`
  142. )
  143. } else if (err instanceof JobNotFoundError) {
  144. // the project has been expired already by another worker
  145. logger.info(
  146. { ...logContext, projectId },
  147. `Job not found for ${taskName} for project`
  148. )
  149. } else {
  150. logger.error(
  151. { ...logContext, projectId, err },
  152. `Error performing ${taskName} for project`
  153. )
  154. }
  155. continue
  156. }
  157. }
  158. processedKeyCount++
  159. if (processedKeyCount % 1000 === 0 && processedKeyCount > 0) {
  160. logger.info(
  161. { ...logContext, scannedKeyCount, processedKeyCount },
  162. `${taskName} scan progress`
  163. )
  164. }
  165. }
  166. }
  167. logger.info(
  168. {
  169. ...logContext,
  170. scannedKeyCount,
  171. processedKeyCount,
  172. elapsedTimeInSeconds: Math.floor((Date.now() - START_TIME) / 1000),
  173. },
  174. `${taskName} scan complete`
  175. )
  176. return { scannedKeyCount, processedKeyCount }
  177. }
  178. module.exports = {
  179. scanRedisCluster,
  180. extractKeyId,
  181. scanAndProcessDueItems,
  182. }