0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-27 23:27:11 +01:00
mongodb/jstests/noPassthrough/server_stats_api_versions_limit.js
2023-08-06 20:48:04 +00:00

31 lines
1.1 KiB
JavaScript

// Ensure that the api versions metric does not grow indefinitely and cause the server to crash.
// Currently it is bounded by 'KMaxNumOfOutputAppNames = 1000' app names.
const KMaxNumOfOutputAppNames = 1000;
const conn = MongoRunner.runMongod({verbose: 0});
const uri = "mongodb://" + conn.host + "/test";
let addAppName = function(suffix) {
try {
// The client.application.name cannot exceed 128 bytes.
const appName = "a".repeat(123) + "_" + suffix;
const newConn = new Mongo(uri + "?appName=" + appName);
const db = newConn.getDB("test");
db.runCommand({ping: 1});
newConn.close();
} catch (e) {
// The connection may fail to be established due to the presence of too many open
// connections.
}
};
const db = new Mongo(uri + "?appName=ServerStatus").getDB("test");
let getNumberofAppNamesReturned = function() {
return Object.keys(db.serverStatus().metrics.apiVersions).length;
};
for (var i = 0; i < 2000; i++) {
addAppName(i);
assert(getNumberofAppNamesReturned() <= KMaxNumOfOutputAppNames);
}
MongoRunner.stopMongod(conn);