diff options
author | Henrik Edin <henrik.edin@mongodb.com> | 2021-12-22 16:50:47 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-12-22 17:42:21 +0000 |
commit | af183c9e3f8f498c89440e28c949f4079ee37b39 (patch) | |
tree | 602dea6fc3e7bb08aa013ee10a5230db56ff00fb /jstests/noPassthrough/compound_2dsphere_max_index_keys.js | |
parent | d6f2b64decf02397546427f6574f581e96c52d5b (diff) | |
download | mongo-af183c9e3f8f498c89440e28c949f4079ee37b39.tar.gz |
SERVER-61184 Add limit to number of compound 2dsphere index keys we may generate
Co-authored-by: Henrik Edin<henrik.edin@mongodb.com>
Diffstat (limited to 'jstests/noPassthrough/compound_2dsphere_max_index_keys.js')
-rw-r--r-- | jstests/noPassthrough/compound_2dsphere_max_index_keys.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/jstests/noPassthrough/compound_2dsphere_max_index_keys.js b/jstests/noPassthrough/compound_2dsphere_max_index_keys.js new file mode 100644 index 00000000000..d6eaa272afa --- /dev/null +++ b/jstests/noPassthrough/compound_2dsphere_max_index_keys.js @@ -0,0 +1,85 @@ +// Test that we can limit number of keys being generated by compounded 2dsphere indexes +(function() { +"use strict"; + +// Launch mongod reduced max allowed keys per document. +var runner = MongoRunner.runMongod({setParameter: "indexMaxNumGeneratedKeysPerDocument=200"}); + +const dbName = jsTestName(); +const testDB = runner.getDB(dbName); +var coll = testDB.t; + +const runTest = (indexDefinition) => { + coll.drop(); + + // Create compound 2dsphere index. + assert.commandWorked(coll.createIndex(indexDefinition)); + + // This polygon will generate 11 keys + let polygon = { + "type": "Polygon", + "coordinates": [[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.0, 0.0]]] + }; + + // We can insert this just fine + assert.commandWorked(coll.insert({x: polygon, y: 1})); + + // However, compounding with an array of 20 elements will exceed the number of keys (11*20 > + // 200) and should fail + assert.commandFailed(coll.insert( + {x: polygon, y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]})); + + // Now let's set a failpoint to relax the constraint so we can insert some bad documents. + assert.commandWorked(testDB.adminCommand( + {configureFailPoint: "relaxIndexMaxNumGeneratedKeysPerDocument", mode: "alwaysOn"})); + + assert.commandWorked(coll.insert([ + { + _id: 'problem1', + x: polygon, + y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + }, + { + _id: 'problem2', + x: polygon, + y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + } + ])); + + // And let's disable the failpoint again. At this point, the documents inserted above simulate + // old documents inserted prior to SERVER-61184. + assert.commandWorked(testDB.adminCommand( + {configureFailPoint: "relaxIndexMaxNumGeneratedKeysPerDocument", mode: "off"})); + + // Confirm we cannot continue to insert bad documents. + assert.commandFailed(coll.insert( + {x: polygon, y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]})); + + // We should also get an error when we try to validate. + const validation = assert.commandWorked(coll.validate()); + assert.eq(validation.errors.length, 1); + assert(validation.errors[0].includes('CannotBuildIndexKeys')); + + // We should be able to remove a problem document. + assert.commandWorked(coll.deleteOne({_id: 'problem1'})); + + // We should also be able to update a problem document to fix it. + assert.commandWorked(coll.updateOne({_id: 'problem2'}, {"$set": {y: []}})); + + // But we shouldn't be able to update it to break it again. + try { + coll.updateOne( + {_id: 'problem2'}, + {"$set": {y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]}}); + assert(false); + } catch (res) { + assert(res instanceof WriteError); + } +}; + +// Test both key orderings +runTest({x: "2dsphere", y: 1}); +runTest({y: 1, x: "2dsphere"}); + +MongoRunner.stopMongod(runner); +})(); |