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
This commit is contained in:
Thomas 2021-08-04 14:09:06 +01:00 committed by Copybot
parent 99fe2eca51
commit e25d8b1440
2 changed files with 24 additions and 6 deletions

View file

@ -7,13 +7,13 @@ if (process.env.BATCH_LAST_ID) {
BATCH_LAST_ID = ObjectId(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 maxId = maxId || BATCH_LAST_ID
if (maxId) { if (maxId) {
query._id = { $gt: maxId } query._id = { $gt: maxId }
} }
const entries = await collection const entries = await collection
.find(query, { readPreference: ReadPreference.SECONDARY }) .find(query, options)
.project(projection) .project(projection)
.sort({ _id: 1 }) .sort({ _id: 1 })
.limit(BATCH_SIZE) .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() await waitForDb()
const collection = db[collectionName] const collection = db[collectionName]
options = options || {}
options.readPreference = ReadPreference.SECONDARY
projection = projection || { _id: 1 } projection = projection || { _id: 1 }
let nextBatch let nextBatch
let updated = 0 let updated = 0
let maxId let maxId
while ( while (
(nextBatch = await getNextBatch(collection, query, maxId, projection)) (nextBatch = await getNextBatch(
.length collection,
query,
maxId,
projection,
options
)).length
) { ) {
maxId = nextBatch[nextBatch.length - 1]._id maxId = nextBatch[nextBatch.length - 1]._id
updated += nextBatch.length updated += nextBatch.length

View file

@ -79,6 +79,9 @@ async function main() {
_id: 1, _id: 1,
overleaf: 1, overleaf: 1,
} }
const options = {
hint: { _id: 1 },
}
await batchedUpdate( await batchedUpdate(
'projects', 'projects',
{ {
@ -88,7 +91,8 @@ async function main() {
], ],
}, },
processBatch, processBatch,
projection projection,
options
) )
console.log('Final') console.log('Final')
console.log(RESULT) console.log(RESULT)