From e25d8b1440286d639b21c0c8d45aa048e71df6ca Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 4 Aug 2021 14:09:06 +0100 Subject: [PATCH] Merge pull request #4381 from overleaf/tm-history-upgrade-batched-update-hints Extend batchedUpdate to accept find() options, and use a hint in upgrade script query to suggest mongo uses the _id index GitOrigin-RevId: 7115f84d8be0c78ccb443051e911c931bf4aa0de --- services/web/scripts/helpers/batchedUpdate.js | 24 +++++++++++++++---- ..._v1_without_conversion_if_no_sl_history.js | 6 ++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/services/web/scripts/helpers/batchedUpdate.js b/services/web/scripts/helpers/batchedUpdate.js index 058cc3908f..1e3e3ca82f 100644 --- a/services/web/scripts/helpers/batchedUpdate.js +++ b/services/web/scripts/helpers/batchedUpdate.js @@ -7,13 +7,13 @@ if (process.env.BATCH_LAST_ID) { BATCH_LAST_ID = ObjectId(process.env.BATCH_LAST_ID) } -async function getNextBatch(collection, query, maxId, projection) { +async function getNextBatch(collection, query, maxId, projection, options) { maxId = maxId || BATCH_LAST_ID if (maxId) { query._id = { $gt: maxId } } const entries = await collection - .find(query, { readPreference: ReadPreference.SECONDARY }) + .find(query, options) .project(projection) .sort({ _id: 1 }) .limit(BATCH_SIZE) @@ -28,17 +28,31 @@ async function performUpdate(collection, nextBatch, update) { ) } -async function batchedUpdate(collectionName, query, update, projection) { +async function batchedUpdate( + collectionName, + query, + update, + projection, + options +) { await waitForDb() const collection = db[collectionName] + options = options || {} + options.readPreference = ReadPreference.SECONDARY + projection = projection || { _id: 1 } let nextBatch let updated = 0 let maxId while ( - (nextBatch = await getNextBatch(collection, query, maxId, projection)) - .length + (nextBatch = await getNextBatch( + collection, + query, + maxId, + projection, + options + )).length ) { maxId = nextBatch[nextBatch.length - 1]._id updated += nextBatch.length diff --git a/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js b/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js index 65250aa4fc..24a9b68b62 100644 --- a/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js +++ b/services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js @@ -79,6 +79,9 @@ async function main() { _id: 1, overleaf: 1, } + const options = { + hint: { _id: 1 }, + } await batchedUpdate( 'projects', { @@ -88,7 +91,8 @@ async function main() { ], }, processBatch, - projection + projection, + options ) console.log('Final') console.log(RESULT)