summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaria van Keulen <maria@mongodb.com>2017-02-06 18:06:50 -0500
committerMaria van Keulen <maria@mongodb.com>2017-03-30 16:38:12 -0400
commit4741285f93e79256b5fa6e04552ebd6a53e6b29e (patch)
treefb1428e170114105257eab6dbc4371db2ac866d9
parent3105cafb4d712deb5cef0b6b12f137b14d2dee33 (diff)
downloadmongo-4741285f93e79256b5fa6e04552ebd6a53e6b29e.tar.gz
SERVER-27930 Invalidate old index record IDs after renaming collections
(cherry picked from commit 08996a3fa199dd04f428c663e78239c07339e7e5) Conflicts: jstests/core/list_indexes_invalidation.js
-rw-r--r--jstests/core/list_indexes_invalidation.js37
-rw-r--r--src/mongo/db/storage/mmap_v1/mmap_v1_database_catalog_entry.cpp9
2 files changed, 46 insertions, 0 deletions
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 <boost/scoped_ptr.hpp>
#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"
@@ -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);
}