Skip to content

Commit 9eecdb4

Browse files
GWlodarekevergreen
authored andcommitted
SERVER-41697 Remove the 'ns' field from any existing index spec catalog entries during upgrade
1 parent 3cdbded commit 9eecdb4

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Tests that the 'ns' field in index specs is removed during metadata changes when in FCV 4.4.
3+
*
4+
* Starting in 4.4, the 'ns' field for index specs is no longer generated. We want to ensure that
5+
* index specs created in prior server versions have their 'ns' field removed when running in FCV
6+
* 4.4.
7+
*/
8+
(function() {
9+
'use strict';
10+
load('jstests/libs/feature_compatibility_version.js');
11+
12+
const dbName = 'test';
13+
const collName = 'coll';
14+
15+
const dbpath = MongoRunner.dataPath + 'remove_ns_field_in_index_spec';
16+
resetDbpath(dbpath);
17+
18+
const mongodOptions42 =
19+
Object.extend({binVersion: 'last-stable'}, {dbpath: dbpath, cleanData: false});
20+
const mongodOptions44 = Object.extend({binVersion: 'latest'}, {dbpath: dbpath, cleanData: false});
21+
22+
/**
23+
* Start up with the 4.2 binary and create a collection. The default '_id' index should have the
24+
* 'ns' field present in its index spec.
25+
*/
26+
let conn = MongoRunner.runMongod(mongodOptions42);
27+
assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(mongodOptions42));
28+
29+
let testDb = conn.getDB(dbName);
30+
assert.commandWorked(testDb.adminCommand({setFeatureCompatibilityVersion: lastStableFCV}));
31+
assert.commandWorked(testDb.createCollection(collName));
32+
33+
let coll = testDb.getCollection(collName);
34+
let indexes = coll.getIndexes();
35+
36+
assert.eq(1, indexes.length);
37+
assert.eq(dbName + '.' + collName, indexes[0].ns);
38+
39+
MongoRunner.stopMongod(conn);
40+
41+
/**
42+
* Restart with the 4.4 binary while in FCV 4.2. The index should not lose its 'ns' field when doing
43+
* a disk modifying metadata change.
44+
*/
45+
let restartOpts44 = Object.extend(mongodOptions44, {restart: true});
46+
conn = MongoRunner.runMongod(restartOpts44);
47+
assert.neq(null, conn, 'mongod was unable to start with version ' + tojson(restartOpts44));
48+
49+
testDb = conn.getDB(dbName);
50+
coll = testDb.getCollection(collName);
51+
52+
// Run a metadata changing operation.
53+
assert.commandWorked(coll.createIndex({x: 1}));
54+
assert.commandWorked(coll.dropIndex({x: 1}));
55+
56+
indexes = coll.getIndexes();
57+
58+
assert.eq(1, indexes.length);
59+
assert.eq(dbName + '.' + collName, indexes[0].ns);
60+
61+
/**
62+
* Set the FCV to 4.4. The index should lose its 'ns' field when doing a disk modifying metadata
63+
* change.
64+
*/
65+
assert.commandWorked(testDb.adminCommand({setFeatureCompatibilityVersion: latestFCV}));
66+
67+
// Run a metadata changing operation.
68+
assert.commandWorked(coll.createIndex({x: 1}));
69+
assert.commandWorked(coll.dropIndex({x: 1}));
70+
71+
indexes = coll.getIndexes();
72+
73+
assert.eq(1, indexes.length);
74+
assert.eq(false, indexes[0].hasOwnProperty('ns'));
75+
76+
MongoRunner.stopMongod(conn);
77+
})();

src/mongo/db/storage/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ env.Library(
426426
],
427427
LIBDEPS_PRIVATE=[
428428
'$BUILD_DIR/mongo/db/catalog/index_timestamp_helper',
429+
'$BUILD_DIR/mongo/db/server_options_core',
429430
],
430431
)
431432

src/mongo/db/storage/durable_catalog_impl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "mongo/db/index/index_descriptor.h"
4343
#include "mongo/db/namespace_string.h"
4444
#include "mongo/db/operation_context.h"
45+
#include "mongo/db/server_options.h"
4546
#include "mongo/db/storage/durable_catalog_feature_tracker.h"
4647
#include "mongo/db/storage/kv/kv_engine.h"
4748
#include "mongo/db/storage/record_store.h"
@@ -576,6 +577,17 @@ void DurableCatalogImpl::putMetaData(OperationContext* opCtx,
576577
BSONObj obj = _findEntry(opCtx, nss, &loc);
577578

578579
{
580+
// Remove the index spec 'ns' field if FCV is set to 4.4.
581+
if (serverGlobalParams.featureCompatibility.isVersionInitialized() &&
582+
serverGlobalParams.featureCompatibility.getVersion() ==
583+
ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo44) {
584+
for (size_t i = 0; i < md.indexes.size(); i++) {
585+
if (md.indexes[i].spec.hasField("ns")) {
586+
md.indexes[i].spec = md.indexes[i].spec.removeField("ns");
587+
}
588+
}
589+
}
590+
579591
// rebuilt doc
580592
BSONObjBuilder b;
581593
b.append("md", md.toBSON());

0 commit comments

Comments
 (0)