Просмотр исходного кода

Merge pull request #26545 from overleaf/jpa-fix-resync

[history-v1] use mongo projectId for initiating resync

GitOrigin-RevId: f93f2358695782fb222d23ba3720d98724b9a291
Eric Mc Sween 1 год назад
Родитель
Сommit
fd9fd9f0e7

+ 23 - 0
services/history-v1/storage/lib/chunk_store/mongo.js

@@ -286,6 +286,27 @@ async function updateProjectRecord(
   )
 }
 
+/**
+ * @param {number} historyId
+ * @return {Promise<string>}
+ */
+async function lookupMongoProjectIdFromHistoryId(historyId) {
+  const project = await mongodb.projects.findOne(
+    // string for Object ids, number for postgres ids
+    { 'overleaf.history.id': historyId },
+    { projection: { _id: 1 } }
+  )
+  if (!project) {
+    // should not happen: We flush before allowing a project to be soft-deleted.
+    throw new OError('mongo project not found by history id', { historyId })
+  }
+  return project._id.toString()
+}
+
+async function resolveHistoryIdToMongoProjectId(projectId) {
+  return projectId
+}
+
 /**
  * Record that a chunk was replaced by a new one.
  *
@@ -533,4 +554,6 @@ module.exports = {
   deleteProjectChunks,
   getOldChunksBatch,
   deleteOldChunks,
+  lookupMongoProjectIdFromHistoryId,
+  resolveHistoryIdToMongoProjectId,
 }

+ 9 - 1
services/history-v1/storage/lib/chunk_store/postgres.js

@@ -5,7 +5,10 @@ const assert = require('../assert')
 const knex = require('../knex')
 const knexReadOnly = require('../knex_read_only')
 const { ChunkVersionConflictError } = require('./errors')
-const { updateProjectRecord } = require('./mongo')
+const {
+  updateProjectRecord,
+  lookupMongoProjectIdFromHistoryId,
+} = require('./mongo')
 
 const DUPLICATE_KEY_ERROR_CODE = '23505'
 
@@ -472,6 +475,10 @@ async function generateProjectId() {
   return record.doc_id.toString()
 }
 
+async function resolveHistoryIdToMongoProjectId(projectId) {
+  return await lookupMongoProjectIdFromHistoryId(parseInt(projectId, 10))
+}
+
 module.exports = {
   getLatestChunk,
   getFirstChunkBeforeTimestamp,
@@ -488,4 +495,5 @@ module.exports = {
   getOldChunksBatch,
   deleteOldChunks,
   generateProjectId,
+  resolveHistoryIdToMongoProjectId,
 }

+ 4 - 1
services/history-v1/storage/lib/persist_buffer.js

@@ -186,7 +186,10 @@ async function persistBuffer(projectId, limits) {
         'content hash validation failed while persisting a history resync, skipping additional resync'
       )
     } else {
-      await resyncProject(projectId)
+      const backend = chunkStore.getBackend(projectId)
+      const mongoProjectId =
+        await backend.resolveHistoryIdToMongoProjectId(projectId)
+      await resyncProject(mongoProjectId)
     }
   }