From 0de648eecdab6c134753ed66c674497cdbb5447a Mon Sep 17 00:00:00 2001 From: Jessica Lawshe <5312836+lawshe@users.noreply.github.com> Date: Thu, 27 Apr 2023 08:56:31 -0500 Subject: [PATCH] Merge pull request #12774 from overleaf/ds-em-mongo-secondary-db-fix Bug fix - Mongo DB secondary preference GitOrigin-RevId: d9adc0a7e1ee5085b34c5c1ddfdc715967607fc3 --- .../src/Features/InactiveData/InactiveProjectManager.js | 3 ++- services/web/app/src/Features/Project/ProjectDeleter.js | 3 ++- services/web/app/src/infrastructure/mongodb.js | 6 +++--- .../unit/src/InactiveData/InactiveProjectManagerTests.js | 6 +++++- services/web/test/unit/src/Project/ProjectDeleterTests.js | 8 ++++++-- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/services/web/app/src/Features/InactiveData/InactiveProjectManager.js b/services/web/app/src/Features/InactiveData/InactiveProjectManager.js index e460f931dd..529c438a49 100644 --- a/services/web/app/src/Features/InactiveData/InactiveProjectManager.js +++ b/services/web/app/src/Features/InactiveData/InactiveProjectManager.js @@ -18,6 +18,7 @@ const ProjectGetter = require('../Project/ProjectGetter') const ProjectUpdateHandler = require('../Project/ProjectUpdateHandler') const { Project } = require('../../models/Project') const { ObjectId } = require('mongodb') +const { READ_PREFERENCE_SECONDARY } = require('../../infrastructure/mongodb') const MILISECONDS_IN_DAY = 86400000 module.exports = InactiveProjectManager = { @@ -71,7 +72,7 @@ module.exports = InactiveProjectManager = { .select('_id') .sort({ _id: 1 }) .limit(limit) - .read('secondary') + .read(READ_PREFERENCE_SECONDARY) .exec(function (err, projects) { if (err != null) { logger.err({ err }, 'could not get projects for deactivating') diff --git a/services/web/app/src/Features/Project/ProjectDeleter.js b/services/web/app/src/Features/Project/ProjectDeleter.js index 3716a11980..af22c1a0f3 100644 --- a/services/web/app/src/Features/Project/ProjectDeleter.js +++ b/services/web/app/src/Features/Project/ProjectDeleter.js @@ -20,6 +20,7 @@ const TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender') const ChatApiHandler = require('../Chat/ChatApiHandler') const moment = require('moment') const { promiseMapWithLimit } = require('../../util/promises') +const { READ_PREFERENCE_SECONDARY } = require('../../infrastructure/mongodb') const EXPIRE_PROJECTS_AFTER_DAYS = 90 const PROJECT_EXPIRATION_BATCH_SIZE = 10000 @@ -94,7 +95,7 @@ async function expireDeletedProjectsAfterDuration() { { 'deleterData.deletedProjectId': 1 } ) .limit(PROJECT_EXPIRATION_BATCH_SIZE) - .read('secondary') + .read(READ_PREFERENCE_SECONDARY) const projectIds = _.shuffle( deletedProjects.map( deletedProject => deletedProject.deleterData.deletedProjectId diff --git a/services/web/app/src/infrastructure/mongodb.js b/services/web/app/src/infrastructure/mongodb.js index 7aa44ae76a..1a9bacbb3f 100644 --- a/services/web/app/src/infrastructure/mongodb.js +++ b/services/web/app/src/infrastructure/mongodb.js @@ -12,10 +12,10 @@ if ( ) } -const READ_PREFERENCE_PRIMARY = ReadPreference.primary +const READ_PREFERENCE_PRIMARY = ReadPreference.primary.mode const READ_PREFERENCE_SECONDARY = Settings.mongo.hasSecondaries - ? ReadPreference.secondary - : ReadPreference.secondaryPreferred + ? ReadPreference.secondary.mode + : ReadPreference.secondaryPreferred.mode let setupDbPromise async function waitForDb() { diff --git a/services/web/test/unit/src/InactiveData/InactiveProjectManagerTests.js b/services/web/test/unit/src/InactiveData/InactiveProjectManagerTests.js index 72def5dbd3..3c8f97cc7f 100644 --- a/services/web/test/unit/src/InactiveData/InactiveProjectManagerTests.js +++ b/services/web/test/unit/src/InactiveData/InactiveProjectManagerTests.js @@ -20,7 +20,7 @@ const modulePath = path.join( '../../../../app/src/Features/InactiveData/InactiveProjectManager' ) const { expect } = require('chai') -const { ObjectId } = require('mongodb') +const { ObjectId, ReadPreference } = require('mongodb') describe('InactiveProjectManager', function () { beforeEach(function () { @@ -42,6 +42,10 @@ describe('InactiveProjectManager', function () { '../Project/ProjectUpdateHandler': this.ProjectUpdateHandler, '../Project/ProjectGetter': this.ProjectGetter, '../../models/Project': {}, + '../../infrastructure/mongodb': { + ObjectId, + READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode, + }, }, }) return (this.project_id = '1234') diff --git a/services/web/test/unit/src/Project/ProjectDeleterTests.js b/services/web/test/unit/src/Project/ProjectDeleterTests.js index 51e8f04f6b..a2af8f5acf 100644 --- a/services/web/test/unit/src/Project/ProjectDeleterTests.js +++ b/services/web/test/unit/src/Project/ProjectDeleterTests.js @@ -6,7 +6,7 @@ const tk = require('timekeeper') const moment = require('moment') const { Project } = require('../helpers/models/Project') const { DeletedProject } = require('../helpers/models/DeletedProject') -const { ObjectId } = require('mongodb') +const { ObjectId, ReadPreference } = require('mongodb') const Errors = require('../../../../app/src/Features/Errors/Errors') describe('ProjectDeleter', function () { @@ -160,7 +160,11 @@ describe('ProjectDeleter', function () { '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter, '../Docstore/DocstoreManager': this.DocstoreManager, './ProjectDetailsHandler': this.ProjectDetailsHandler, - '../../infrastructure/mongodb': { db: this.db, ObjectId }, + '../../infrastructure/mongodb': { + db: this.db, + ObjectId, + READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode, + }, '../History/HistoryManager': this.HistoryManager, '../../models/ProjectAuditLogEntry': { ProjectAuditLogEntry: this.ProjectAuditLogEntry,