Browse Source

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
Thomas 5 years ago
parent
commit
e25d8b1440

+ 19 - 5
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)
   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))
-      .length
+    (nextBatch = await getNextBatch(
+      collection,
+      query,
+      maxId,
+      projection,
+      options
+    )).length
   ) {
   ) {
     maxId = nextBatch[nextBatch.length - 1]._id
     maxId = nextBatch[nextBatch.length - 1]._id
     updated += nextBatch.length
     updated += nextBatch.length

+ 5 - 1
services/web/scripts/history/upgrade_v1_without_conversion_if_no_sl_history.js

@@ -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)