Skip to content

Commit

Permalink
bump mongodb and adapt code
Browse files Browse the repository at this point in the history
  • Loading branch information
benzekrimaha committed Nov 28, 2024
1 parent 353a7e6 commit ba7ade2
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 400 deletions.
27 changes: 15 additions & 12 deletions lib/storage/metadata/mongoclient/LogConsumer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'; // eslint-disable-line

const MongoClient = require('mongodb').MongoClient;
const { MongoClient } = require('mongodb');
const ListRecordStream = require('./ListRecordStream');
const MongoUtils = require('./utils');

Expand All @@ -16,10 +16,11 @@ class LogConsumer {
* @param {string} logger - logger
*/
constructor(mongoConfig, logger) {
const { authCredentials, replicaSetHosts, replicaSet, database } = mongoConfig;
const { authCredentials, replicaSetHosts, replicaSet, database, readPreference } = mongoConfig;
const cred = MongoUtils.credPrefix(authCredentials);
this._mongoUrl = `mongodb://${cred}${replicaSetHosts}/`;
this._replicaSet = replicaSet;
this._readPreference = readPreference;
this._logger = logger;
this._oplogNsRegExp = new RegExp(`^${database}\\.`);
// oplog collection
Expand All @@ -35,27 +36,29 @@ class LogConsumer {
* @return {undefined}
*/
connectMongo(done) {
MongoClient.connect(this._mongoUrl, {
const client = new MongoClient(this._mongoUrl, {
replicaSet: this._replicaSet,
useNewUrlParser: true,
},
(err, client) => {
if (err) {
this._logger.error('Unable to connect to MongoDB',
{ error: err });
return done(err);
}
useUnifiedTopology: true,
readPreference: this._readPreference,
});

client.connect().then(client => {
this._logger.info('connected to mongodb');
// 'local' is the database where MongoDB has oplog.rs
// capped collection
// 'local' is the database where MongoDB has oplog.rs capped collection
const db = client.db('local', {
ignoreUndefined: true,
});
this._coll = db.collection('oplog.rs');
return done();
})
.catch(err => {
this._logger.error('Unable to connect to MongoDB', { error: err });
return done(err);
});
}


/**
* Open a tailable cursor to mongo oplog and retrieve a stream of
* records to read
Expand Down
13 changes: 10 additions & 3 deletions lib/storage/metadata/mongoclient/MongoClientInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const BucketInfo = require('../../../models/BucketInfo').default;
const ObjectMD = require('../../../models/ObjectMD').default;
const jsutil = require('../../../jsutil');

const MongoClient = require('mongodb').MongoClient;
const { MongoClient } = require('mongodb');
const Uuid = require('uuid');
const diskusage = require('diskusage');

Expand Down Expand Up @@ -148,7 +148,9 @@ class MongoClientInterface {
!Number.isNaN(process.env.MONGO_POOL_SIZE)) {
options.poolSize = Number.parseInt(process.env.MONGO_POOL_SIZE, 10);
}
return MongoClient.connect(this.mongoUrl, options)
const client = new MongoClient(this.mongoUrl, options);

return client.connect()
.then(client => {
this.logger.info('connected to mongodb');
this.client = client;
Expand Down Expand Up @@ -477,6 +479,8 @@ class MongoClientInterface {
const m = this.getCollection(METASTORE);
m.findOneAndDelete({
_id: bucketName,
} , {
includeResultMetadata: true
}, {})
.then(result => {
if (result.ok !== 1) {
Expand Down Expand Up @@ -1296,6 +1300,7 @@ class MongoClientInterface {
_id: masterKey,
value: objVal,
}, {
includeResultMetadata: true,
upsert: true,
}).then(result => {
if (result.ok !== 1) {
Expand Down Expand Up @@ -1641,6 +1646,7 @@ class MongoClientInterface {
'value.deleted': true,
},
}, {
includeResultMetadata : true,
upsert: false,
}).then(doc => {
if (!doc.value) {
Expand Down Expand Up @@ -2645,8 +2651,9 @@ class MongoClientInterface {
value: objVal,
},
}, {
includeResultMetadata: true,
upsert: true,
}).then(res => {
},).then(res => {
if (res.ok !== 1) {
log.error('failed to update object', {
method,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"joi": "^17.13.3",
"level": "~5.0.1",
"level-sublevel": "~6.6.5",
"mongodb": "^5.2.0",
"mongodb": "^6.11.0",
"node-forge": "^1.3.1",
"prom-client": "^15.1.3",
"simple-glob": "^0.2.0",
Expand Down
Loading

0 comments on commit ba7ade2

Please sign in to comment.