Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade mongodb to 4 #817

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Upgrade: mongodb dependency from 3.6.12 to 4.17.2
- Upgrade: nanoid dependency from 3.3.4 to 3.3.8 (solving vulnerability CVE-2024-55565)

1.34.0 (July 30th, 2024)
Expand Down
45 changes: 18 additions & 27 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var async = require('async'),
myutils = require('./myutils'),
constants = require('./constants'),
alarm = require('./alarm'),
mongodb = require('mongodb'),
database,
orionDb,
delay = config.checkDB.delay,
Expand All @@ -46,16 +47,19 @@ function pingAux(db, component, callback) {
}

function getDbAux(url, component, callback) {
var client = require('mongodb').MongoClient,
var client = mongodb.MongoClient,
checkDbHealthFunc;

client.connect(
url,
{
bufferMaxEntries: config.checkDB.bufferMaxEntries,
domainsEnabled: true,
reconnectTries: config.checkDB.reconnectTries,
reconnectInterval: config.checkDB.reconnectInterval
//useUnifiedTopology: true
// connectTimeoutMS is no longer supported in MongoDB 4.x
// (see https://stackoverflow.com/q/72699235/1485926)
// we keep connectTimeoutMS as configuration parameter, but
// the driver parameters are now socketTimeoutMS and
// serverSelectionTimeoutMS
socketTimeoutMS: config.mongo.connectTimeoutMS,
serverSelectionTimeoutMS: config.mongo.connectTimeoutMS
},
function(err, client) {
if (err) {
Expand All @@ -65,15 +69,6 @@ function getDbAux(url, component, callback) {

const db = client.db();

// This event is emitted only by Server topology (standalone)
// The driver has given up getting a connection, so we will die (restart perseo usually)
// and re-try from scratch.
// The ReplSet does not emit 'reconnectFailed'
db.serverConfig.on('reconnectFailed', function() {
logger.fatal('too many tries to reconnect to database, dying ...');
process.exit(-2);
});

checkDbHealthFunc = function checkDbHealth() {
pingAux(db, component, function(err, result) {
logger.debug('ping (%s) %j', component, err || result);
Expand All @@ -98,21 +93,17 @@ function getOrionDb(callback) {
}

function ensureIndex(collection, fields, callback) {
database.collection(collection, function(err, collection) {
myutils.logErrorIf(err, collection, context);
collection.createIndex(fields, { unique: true }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
var col = database.collection(collection);
col.createIndex(fields, { unique: true }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
}
function ensureIndexTTL(collection, fields, ttl, callback) {
database.collection(collection, function(err, collection) {
myutils.logErrorIf(err, collection);
collection.createIndex(fields, { expireAfterSeconds: ttl }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
var col = database.collection(collection);
col.createIndex(fields, { expireAfterSeconds: ttl }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
}

Expand Down
64 changes: 34 additions & 30 deletions lib/models/entitiesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
*/
'use strict';

var async = require('async'),
appContext = require('../appContext'),
var appContext = require('../appContext'),
config = require('../../config'),
entitiesCollectionName = require('../../config').orionDb.collection,
myutils = require('../myutils'),
Expand Down Expand Up @@ -127,6 +126,10 @@ function findSilentEntitiesByMongo(service, subservice, ruleData, alterFunc, cal
var db,
criterion = {};

var cb = function(err, result) {
logger.debug(context, 'findSilentEntitiesByMongo %s', myutils.firstChars(result));
return callback(err, result);
};
db = orionServiceDb(service);
criterion['attrs.' + ruleData.attribute + '.modDate'] = {
$lt: Date.now() / 1000 - ruleData.reportInterval
Expand All @@ -146,35 +149,36 @@ function findSilentEntitiesByMongo(service, subservice, ruleData, alterFunc, cal
}
logger.debug(context, 'findSilentEntities criterion %j', criterion);

// Variable to store the count of entities
var entityCount = 0;

async.waterfall(
[
db.collection.bind(db, entitiesCollectionName, { strict: false }),
function(col, cb) {
col.find(criterion)
.batchSize(config.orionDb.batchSize)
.each(function(err, one) {
if (err) {
return cb(err, null);
}
if (one === null) {
// Cursor exhausted
return cb(err, 'silent ones count ' + entityCount);
}
logger.debug(context, 'silent entity %j', one._id);
alterFunc(one);
// Increment the count of entities
entityCount++;
});
}
],
function(err, result) {
logger.debug(context, 'findSilentEntities %s', myutils.firstChars(result));
return callback(err, result, entityCount);
myutils.collectionExists(db, entitiesCollectionName, function(exists) {
if (!exists) {
return cb('collection ' + entitiesCollectionName + ' does not exist');
}
);

var col = db.collection(entitiesCollectionName);

var entityCount = 0;
col.find(criterion)
.batchSize(config.orionDb.batchSize)
.forEach(
function(one) {
if (one === null) {
// Cursor exhausted
return cb(null, 'silent ones count ' + entityCount);
}
logger.debug(context, 'silent entity %j', one._id);
alterFunc(one);
// Increment the count of entities
entityCount++;
},
function(err) {
if (err) {
return cb(err, null);
} else {
return cb(null, 'silent ones count ' + entityCount);
}
}
);
});
}

function findSilentEntities(service, subservice, ruleData, alterFunc, callback) {
Expand Down
106 changes: 51 additions & 55 deletions lib/models/executionsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@
*/
'use strict';

var async = require('async'),
appContext = require('../appContext'),
var appContext = require('../appContext'),
logger = require('logops'),
execCollectionName = require('../../config').collections.executions,
myutils = require('../myutils');

module.exports = {
LastTime: function LastTime(task, callback) {
var db = appContext.Db(),
service = task.event.service,
subservice = task.event.subservice,
ruleName = task.event.ruleName,
id = task.event.id,
index = task.action.index;

db.collection(execCollectionName, { strict: true }, function(err, col) {
if (err) {
myutils.logErrorIf(err);
return callback(err, null);
myutils.collectionExists(appContext.Db(), execCollectionName, function(exists) {
if (!exists) {
return callback('collection ' + execCollectionName + ' does not exist');
}
var col = appContext.Db().collection(execCollectionName),
service = task.event.service,
subservice = task.event.subservice,
ruleName = task.event.ruleName,
id = task.event.id,
index = task.action.index;

var cursor = col
.find(
{
Expand All @@ -66,18 +64,18 @@ module.exports = {
});
},
AlreadyDone: function AlreadyDone(task, callback) {
var db = appContext.Db(),
service = task.event.service,
subservice = task.event.subservice,
ruleName = task.event.ruleName,
id = task.event.id,
index = task.action.index,
noticeId = task.event.noticeId;
db.collection(execCollectionName, { strict: true }, function(err, col) {
if (err) {
myutils.logErrorIf(err);
return callback(err, null);
myutils.collectionExists(appContext.Db(), execCollectionName, function(exists) {
if (!exists) {
return callback('collection ' + execCollectionName + ' does not exist');
}
var col = appContext.Db().collection(execCollectionName),
service = task.event.service,
subservice = task.event.subservice,
ruleName = task.event.ruleName,
id = task.event.id,
index = task.action.index,
noticeId = task.event.noticeId;

col.findOne(
{
name: ruleName,
Expand All @@ -98,37 +96,35 @@ module.exports = {
});
},
Update: function Update(task, callback) {
var db = appContext.Db(),
service = task.event.service,
subservice = task.event.subservice,
ruleName = task.event.ruleName,
id = task.event.id,
index = task.action.index,
noticeId = task.event.noticeId;
async.waterfall(
[
db.collection.bind(db, execCollectionName, { strict: true }),
function(col, cb) {
col.update(
{
name: ruleName,
subservice: subservice,
service: service,
id: id,
notice: noticeId,
index: index
},
{ $currentDate: { lastTime: true } },
{ upsert: true },
cb
);
}
],
function(err, result) {
myutils.logErrorIf(err);
logger.info('executionsStore.Update %j', result);
return callback(err, result);
myutils.collectionExists(appContext.Db(), execCollectionName, function(exists) {
if (!exists) {
return callback('collection ' + execCollectionName + ' does not exist');
}
);
var col = appContext.Db().collection(execCollectionName),
service = task.event.service,
subservice = task.event.subservice,
ruleName = task.event.ruleName,
id = task.event.id,
index = task.action.index,
noticeId = task.event.noticeId;

col.update(
{
name: ruleName,
subservice: subservice,
service: service,
id: id,
notice: noticeId,
index: index
},
{ $currentDate: { lastTime: true } },
{ upsert: true },
function(err, result) {
myutils.logErrorIf(err);
logger.info('executionsStore.Update %j', result);
return callback(err, result);
}
);
});
}
};
Loading