diff options
author | Ruoxin Xu <ruoxin.xu@mongodb.com> | 2022-11-14 12:29:57 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-11-14 13:01:10 +0000 |
commit | 2c485054578b7172439da7103b4d9a35881e087d (patch) | |
tree | 31a2c93c619335d9a91595c6c9dfbe46f0f5c851 | |
parent | 1854c66a7dc76d891912356cf1646ccd4b146e8c (diff) | |
download | mongo-2c485054578b7172439da7103b4d9a35881e087d.tar.gz |
SERVER-71300 Reset cost coefficients to default value if "internalCostModelCoefficients" is empty
-rw-r--r-- | jstests/cqf/cost_model_override.js | 20 | ||||
-rw-r--r-- | src/mongo/db/query/cost_model/cost_model_manager.cpp | 11 |
2 files changed, 22 insertions, 9 deletions
diff --git a/jstests/cqf/cost_model_override.js b/jstests/cqf/cost_model_override.js index cdf424250ad..5f03cbeeca2 100644 --- a/jstests/cqf/cost_model_override.js +++ b/jstests/cqf/cost_model_override.js @@ -21,12 +21,7 @@ assert.commandWorked(coll.insert(Array.from({length: nDocuments}, (_, i) => { }))); function executeAndGetScanCost(scanIncrementalCost) { - try { - assert.commandWorked(db.adminCommand({ - 'setParameter': 1, - 'internalCostModelCoefficients': `{"scanIncrementalCost": ${scanIncrementalCost}}` - })); - + const getScanCost = function() { const explain = coll.explain("executionStats").aggregate([]); assert.eq(nDocuments, explain.executionStats.nReturned); @@ -34,9 +29,22 @@ function executeAndGetScanCost(scanIncrementalCost) { assertValueOnPath("PhysicalScan", scanNode, "nodeType"); return scanNode.properties.cost; + }; + const initCost = getScanCost(); + try { + assert.commandWorked(db.adminCommand({ + 'setParameter': 1, + 'internalCostModelCoefficients': `{"scanIncrementalCost": ${scanIncrementalCost}}` + })); + + return getScanCost(); } finally { + // Empty "internalCostModelCoefficients" should reset the cost model to default. assert.commandWorked( db.adminCommand({'setParameter': 1, 'internalCostModelCoefficients': ''})); + const resetCost = getScanCost(); + + assert.close(initCost, resetCost, 8 /*decimal places*/); } } diff --git a/src/mongo/db/query/cost_model/cost_model_manager.cpp b/src/mongo/db/query/cost_model/cost_model_manager.cpp index caa493fffb0..6c97b39124a 100644 --- a/src/mongo/db/query/cost_model/cost_model_manager.cpp +++ b/src/mongo/db/query/cost_model/cost_model_manager.cpp @@ -106,9 +106,14 @@ CostModelCoefficients CostModelManager::getDefaultCoefficients() { } void CostModelManager::updateCostModelCoefficients(const BSONObj& overrides) { - auto coefsObj = _coefficients.toBSON(); - auto newCoefs = CostModelCoefficients::parse(IDLParserContext{"CostModelCoefficients"}, - coefsObj.addFields(overrides)); + CostModelCoefficients newCoefs; + if (overrides.isEmpty()) { + initializeCoefficients(newCoefs); + } else { + auto coefsObj = _coefficients.toBSON(); + newCoefs = CostModelCoefficients::parse(IDLParserContext{"CostModelCoefficients"}, + coefsObj.addFields(overrides)); + } stdx::unique_lock wLock(_mutex); _coefficients = std::move(newCoefs); |