Merge pull request #144 from overleaf/jpa-mongodb-native

[misc] migrate to the native mongo driver
This commit is contained in:
Jakob Ackermann 2020-09-30 10:09:49 +02:00 committed by GitHub
commit 16da44e56a
9 changed files with 191 additions and 641 deletions

View file

@ -17,14 +17,14 @@ const DispatchManager = require('./app/js/DispatchManager')
const DeleteQueueManager = require('./app/js/DeleteQueueManager')
const Errors = require('./app/js/Errors')
const HttpController = require('./app/js/HttpController')
const mongojs = require('./app/js/mongojs')
const mongodb = require('./app/js/mongodb')
const async = require('async')
const Path = require('path')
const bodyParser = require('body-parser')
Metrics.mongodb.monitor(
Path.resolve(__dirname, '/node_modules/mongojs/node_modules/mongodb'),
Path.resolve(__dirname, '/node_modules/mongodb'),
logger
)
Metrics.event_loop.monitor(logger, 100)
@ -158,7 +158,7 @@ app.get('/health_check', (req, res, next) => {
})
},
(cb) => {
mongojs.healthCheck((error) => {
mongodb.healthCheck((error) => {
if (error) {
logger.err({ err: error }, 'failed mongo health check')
}
@ -219,13 +219,27 @@ const host = Settings.internal.documentupdater.host || 'localhost'
if (!module.parent) {
// Called directly
app.listen(port, host, () => {
logger.info(`Document-updater starting up, listening on ${host}:${port}`)
mongodb
.waitForDb()
.then(() => {
app.listen(port, host, function (err) {
if (err) {
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
process.exit(1)
}
logger.info(
`Document-updater starting up, listening on ${host}:${port}`
)
if (Settings.continuousBackgroundFlush) {
logger.info('Starting continuous background flush')
DeleteQueueManager.startBackgroundFlush()
}
})
})
.catch((err) => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1)
})
}
module.exports = app

View file

@ -12,7 +12,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let SnapshotManager
const { db, ObjectId } = require('./mongojs')
const { db, ObjectId } = require('./mongodb')
module.exports = SnapshotManager = {
recordSnapshot(
@ -30,7 +30,7 @@ module.exports = SnapshotManager = {
} catch (error) {
return callback(error)
}
return db.docSnapshots.insert(
db.docSnapshots.insertOne(
{
project_id,
doc_id,

View file

@ -0,0 +1,37 @@
const Settings = require('settings-sharelatex')
const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(
Settings.mongo.url,
Settings.mongo.options
)
async function healthCheck() {
const internalDb = (await clientPromise).db()
const res = await internalDb.command({ ping: 1 })
if (!res.ok) {
throw new Error('failed mongo ping')
}
}
let setupDbPromise
async function waitForDb() {
if (!setupDbPromise) {
setupDbPromise = setupDb()
}
await setupDbPromise
}
const db = {}
async function setupDb() {
const internalDb = (await clientPromise).db()
db.docSnapshots = internalDb.collection('docSnapshots')
}
module.exports = {
db,
ObjectId,
healthCheck: require('util').callbackify(healthCheck),
waitForDb
}

View file

@ -1,27 +0,0 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const Settings = require('settings-sharelatex')
const mongojs = require('mongojs')
const db = mongojs(Settings.mongo.url, ['docSnapshots'])
module.exports = {
db,
ObjectId: mongojs.ObjectId,
healthCheck(callback) {
return db.runCommand({ ping: 1 }, function (err, res) {
if (err != null) {
return callback(err)
}
if (!res.ok) {
return callback(new Error('failed mongo ping'))
}
return callback()
})
}
}

View file

@ -174,6 +174,10 @@ module.exports = {
dispatcherCount: process.env.DISPATCHER_COUNT,
mongo: {
options: {
useUnifiedTopology:
(process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true'
},
url:
process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || '127.0.0.1'}/sharelatex`

File diff suppressed because it is too large Load diff

View file

@ -25,7 +25,7 @@
"lodash": "^4.17.13",
"logger-sharelatex": "^2.2.0",
"metrics-sharelatex": "^2.6.2",
"mongojs": "^3.1.0",
"mongodb": "^3.6.0",
"redis-sharelatex": "^1.0.13",
"request": "^2.88.2",
"requestretry": "^4.1.0",

View file

@ -17,7 +17,7 @@ chai.should()
const { expect } = chai
const async = require('async')
const { db, ObjectId } = require('../../../app/js/mongojs')
const { db, ObjectId } = require('../../../app/js/mongodb')
const MockWebApi = require('./helpers/MockWebApi')
const DocUpdaterClient = require('./helpers/DocUpdaterClient')
const DocUpdaterApp = require('./helpers/DocUpdaterApp')
@ -668,12 +668,12 @@ describe('Ranges', function () {
if (error != null) {
return done(error)
}
return db.docSnapshots.find(
{
db.docSnapshots
.find({
project_id: ObjectId(this.project_id),
doc_id: ObjectId(this.doc_id)
},
(error, docSnapshots) => {
})
.toArray((error, docSnapshots) => {
if (error != null) {
return done(error)
}
@ -686,8 +686,7 @@ describe('Ranges', function () {
tid: this.tid
})
return done()
}
)
})
}
)
})

View file

@ -12,6 +12,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const app = require('../../../../app')
const { waitForDb } = require('../../../../app/js/mongodb')
require('logger-sharelatex').logger.level('fatal')
module.exports = {
@ -26,9 +27,10 @@ module.exports = {
return callback()
} else if (this.initing) {
return this.callbacks.push(callback)
} else {
}
this.initing = true
this.callbacks.push(callback)
waitForDb().then(() => {
return app.listen(3003, 'localhost', (error) => {
if (error != null) {
throw error
@ -42,6 +44,6 @@ module.exports = {
return result
})()
})
}
})
}
}