From 4741285f93e79256b5fa6e04552ebd6a53e6b29e Mon Sep 17 00:00:00 2001 From: Maria van Keulen Date: Mon, 6 Feb 2017 18:06:50 -0500 Subject: SERVER-27930 Invalidate old index record IDs after renaming collections (cherry picked from commit 08996a3fa199dd04f428c663e78239c07339e7e5) Conflicts: jstests/core/list_indexes_invalidation.js --- jstests/core/list_indexes_invalidation.js | 37 ++++++++++++++++++++++ .../mmap_v1/mmap_v1_database_catalog_entry.cpp | 9 ++++++ 2 files changed, 46 insertions(+) create mode 100644 jstests/core/list_indexes_invalidation.js diff --git a/jstests/core/list_indexes_invalidation.js b/jstests/core/list_indexes_invalidation.js new file mode 100644 index 00000000000..1a7770b75fb --- /dev/null +++ b/jstests/core/list_indexes_invalidation.js @@ -0,0 +1,37 @@ +// SERVER-24963/SERVER-27930 Missing invalidation for system.indexes writes +(function() { + 'use strict'; + var collName = 'system_indexes_invalidations'; + var collNameRenamed = 'renamed_collection'; + var coll = db[collName]; + var collRenamed = db[collNameRenamed]; + + function testIndexInvalidation(isRename) { + coll.drop(); + collRenamed.drop(); + assert.commandWorked(coll.createIndexes([{a: 1}, {b: 1}, {c: 1}])); + + // Get the first two indexes. Use find on 'system.indexes' on MMAPv1, listIndexes otherwise. + var cmd = db.system.indexes.count() ? {find: 'system.indexes'} : {listIndexes: collName}; + Object.extend(cmd, {batchSize: 2}); + var 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. + var cursor = new DBCommandCursor(db.getMongo(), res); + var 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 d7c0c4fe8e9..60c3055f3cc 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 @@ -35,6 +35,8 @@ #include #include +#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" @@ -308,6 +310,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, loc); } -- cgit v1.2.1