From fd9fd9f0e7dd84379959fed736009832f0a64f4c Mon Sep 17 00:00:00 2001 From: Eric Mc Sween <5454374+emcsween@users.noreply.github.com> Date: Wed, 18 Jun 2025 12:22:25 -0400 Subject: [PATCH] Merge pull request #26545 from overleaf/jpa-fix-resync [history-v1] use mongo projectId for initiating resync GitOrigin-RevId: f93f2358695782fb222d23ba3720d98724b9a291 --- .../storage/lib/chunk_store/mongo.js | 23 +++++++++++++++++++ .../storage/lib/chunk_store/postgres.js | 10 +++++++- .../history-v1/storage/lib/persist_buffer.js | 5 +++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/services/history-v1/storage/lib/chunk_store/mongo.js b/services/history-v1/storage/lib/chunk_store/mongo.js index 26c1bc48ec..49020c6be4 100644 --- a/services/history-v1/storage/lib/chunk_store/mongo.js +++ b/services/history-v1/storage/lib/chunk_store/mongo.js @@ -286,6 +286,27 @@ async function updateProjectRecord( ) } +/** + * @param {number} historyId + * @return {Promise} + */ +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, } diff --git a/services/history-v1/storage/lib/chunk_store/postgres.js b/services/history-v1/storage/lib/chunk_store/postgres.js index bfb5c6954a..8906db38e1 100644 --- a/services/history-v1/storage/lib/chunk_store/postgres.js +++ b/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, } diff --git a/services/history-v1/storage/lib/persist_buffer.js b/services/history-v1/storage/lib/persist_buffer.js index 578142135c..d562388f87 100644 --- a/services/history-v1/storage/lib/persist_buffer.js +++ b/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) } }