diff options
author | Maria van Keulen <maria@mongodb.com> | 2017-02-06 18:06:50 -0500 |
---|---|---|
committer | Maria van Keulen <maria@mongodb.com> | 2017-03-06 13:33:48 -0500 |
commit | 07b613ffce164020fee19a4a4465db1c7fa680a8 (patch) | |
tree | 34480849e287c2c72830893e9ac6b1dc6331bed0 | |
parent | 65bc8f25d7e76e23e7380f81ee033537ba9e8866 (diff) | |
download | mongo-07b613ffce164020fee19a4a4465db1c7fa680a8.tar.gz |
SERVER-27930 Invalidate old index record IDs after renaming collections
(cherry picked from commit 763edc9dfef746ffa1c72bcc18778047fb0efc97)
-rw-r--r-- | jstests/core/list_indexes_invalidation.js | 45 | ||||
-rw-r--r-- | src/mongo/db/storage/mmap_v1/mmap_v1_database_catalog_entry.cpp | 9 |
2 files changed, 39 insertions, 15 deletions
diff --git a/jstests/core/list_indexes_invalidation.js b/jstests/core/list_indexes_invalidation.js index 0557cb0e17b..b8cbe5eb134 100644 --- a/jstests/core/list_indexes_invalidation.js +++ b/jstests/core/list_indexes_invalidation.js @@ -1,22 +1,37 @@ -// SERVER-24963 Missing invalidation for system.indexes write +// SERVER-24963/SERVER-27930 Missing invalidation for system.indexes writes (function() { 'use strict'; let collName = 'system_indexes_invalidations'; + let collNameRenamed = 'renamed_collection'; let coll = db[collName]; - coll.drop(); - assert.commandWorked(coll.createIndexes([{a: 1}, {b: 1}, {c: 1}])); + let collRenamed = db[collNameRenamed]; - // Get the first two indexes. Use find on 'system.indexes' on MMAPv1, listIndexes otherwise. - let cmd = db.system.indexes.count() ? {find: 'system.indexes'} : {listIndexes: collName}; - Object.extend(cmd, {batchSize: 2}); - let res = db.runCommand(cmd); - assert.commandWorked(res, 'could not run ' + tojson(cmd)); - printjson(res); + function testIndexInvalidation(isRename) { + coll.drop(); + collRenamed.drop(); + assert.commandWorked(coll.createIndexes([{a: 1}, {b: 1}, {c: 1}])); - // Ensure the cursor has data, drop the collection, and exhaust the cursor. - let cursor = new DBCommandCursor(db.getMongo(), res); - let errMsg = 'expected more data from command ' + tojson(cmd) + ', with result ' + tojson(res); - assert(cursor.hasNext(), errMsg); - assert(coll.drop()); - assert.gt(cursor.itcount(), 0, errMsg); + // Get the first two indexes. Use find on 'system.indexes' on MMAPv1, listIndexes otherwise. + let cmd = db.system.indexes.count() ? {find: 'system.indexes'} : {listIndexes: collName}; + Object.extend(cmd, {batchSize: 2}); + let res = db.runCommand(cmd); + assert.commandWorked(res, 'could not run ' + tojson(cmd)); + printjson(res); + + // Ensure the cursor has data, rename or drop the collection, and exhaust the cursor. + let cursor = new DBCommandCursor(db.getMongo(), res); + let errMsg = + 'expected more data from command ' + tojson(cmd) + ', with result ' + tojson(res); + assert(cursor.hasNext(), errMsg); + if (isRename) { + assert.commandWorked(coll.renameCollection(collNameRenamed)); + } else { + assert(coll.drop()); + } + assert.gt(cursor.itcount(), 0, errMsg); + } + + // Test that we invalidate indexes for both collection drops and renames. + testIndexInvalidation(false); + testIndexInvalidation(true); }()); diff --git a/src/mongo/db/storage/mmap_v1/mmap_v1_database_catalog_entry.cpp b/src/mongo/db/storage/mmap_v1/mmap_v1_database_catalog_entry.cpp index 3d2a0f3a290..e8e964a0183 100644 --- a/src/mongo/db/storage/mmap_v1/mmap_v1_database_catalog_entry.cpp +++ b/src/mongo/db/storage/mmap_v1/mmap_v1_database_catalog_entry.cpp @@ -34,6 +34,8 @@ #include <utility> +#include "mongo/db/catalog/database.h" +#include "mongo/db/catalog/database_holder.h" #include "mongo/db/catalog/index_catalog_entry.h" #include "mongo/db/index/2d_access_method.h" #include "mongo/db/index/btree_access_method.h" @@ -307,6 +309,13 @@ Status MMAPV1DatabaseCatalogEntry::renameCollection(OperationContext* txn, if (!s.isOK()) return s; } + // Invalidate index record for the old collection. + StringData dbName(nsToDatabaseSubstring(_collections.begin()->first)); + invariant(txn->lockState()->isDbLockedForMode(dbName, MODE_X)); + Database* db = dbHolder().get(txn, dbName); + Collection* systemIndexes = db->getCollection(db->getSystemIndexesName()); + systemIndexes->getCursorManager()->invalidateDocument( + txn, record->id, INVALIDATION_DELETION); systemIndexRecordStore->deleteRecord(txn, record->id); } |