Skip to content

Commit 7f2f527

Browse files
committed
SERVER-34263 Use WT specific data format version for new unique idx
1 parent 93fd36a commit 7f2f527

15 files changed

+356
-410
lines changed

jstests/noPassthrough/index_version_v2.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
* Additionally, this file tests that index version v=2 is required to create an index with a
55
* collation and that index version v=2 is required to index decimal data on storage engines using
66
* the KeyString format.
7-
*
8-
* Also, index version v=3 is required to prohibit duplicates in unique index at secondary. Enhance
9-
* the tests for index version v=3.
107
*/
118
(function() {
129
"use strict";
@@ -121,17 +118,7 @@
121118

122119
testDB.dropDatabase();
123120

124-
// MongoDB4.0 onwards index version v=3 would be supported. Test that an index created with v=3
125-
// succeeds.
126-
assert.commandWorked(testDB.index_version.createIndex({withV3: 1}, {v: 3}));
127-
128-
//
129-
// Index version v=4
130-
//
131-
132-
testDB.dropDatabase();
133-
134-
// Test that attempting to create an index with v=4 returns an error.
135-
assert.commandFailed(testDB.index_version.createIndex({withV4: 1}, {v: 4}));
121+
// Test that attempting to create an index with v=3 returns an error.
122+
assert.commandFailed(testDB.index_version.createIndex({withV3: 1}, {v: 3}));
136123
MongoRunner.stopMongod(conn);
137124
})();

src/mongo/db/catalog/database_impl.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,9 @@ Collection* DatabaseImpl::createCollection(OperationContext* opCtx,
815815
optionsWithUUID.autoIndexId == CollectionOptions::DEFAULT) {
816816
// createCollection() may be called before the in-memory fCV parameter is
817817
// initialized, so use the unsafe fCV getter here.
818-
const auto featureCompatibilityVersion =
819-
serverGlobalParams.featureCompatibility.getVersionUnsafe();
820818
IndexCatalog* ic = collection->getIndexCatalog();
821819
fullIdIndexSpec = uassertStatusOK(ic->createIndexOnEmptyCollection(
822-
opCtx,
823-
!idIndex.isEmpty() ? idIndex
824-
: ic->getDefaultIdIndexSpec(featureCompatibilityVersion)));
820+
opCtx, !idIndex.isEmpty() ? idIndex : ic->getDefaultIdIndexSpec()));
825821
} else {
826822
// autoIndexId: false is only allowed on unreplicated collections.
827823
uassert(50001,

src/mongo/db/catalog/index_catalog.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ class IndexCatalog {
161161

162162
virtual bool haveIdIndex(OperationContext* opCtx) const = 0;
163163

164-
virtual BSONObj getDefaultIdIndexSpec(ServerGlobalParams::FeatureCompatibility::Version
165-
featureCompatibilityVersion) const = 0;
164+
virtual BSONObj getDefaultIdIndexSpec() const = 0;
166165

167166
virtual IndexDescriptor* findIdIndex(OperationContext* opCtx) const = 0;
168167

@@ -312,9 +311,8 @@ class IndexCatalog {
312311
/**
313312
* Returns the spec for the id index to create by default for this collection.
314313
*/
315-
inline BSONObj getDefaultIdIndexSpec(
316-
const ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion) const {
317-
return this->_impl().getDefaultIdIndexSpec(featureCompatibilityVersion);
314+
inline BSONObj getDefaultIdIndexSpec() const {
315+
return this->_impl().getDefaultIdIndexSpec();
318316
}
319317

320318
inline IndexDescriptor* findIdIndex(OperationContext* const opCtx) const {

src/mongo/db/catalog/index_catalog_impl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,10 @@ Status IndexCatalogImpl::_doesSpecConflictWithExisting(OperationContext* opCtx,
870870
return Status::OK();
871871
}
872872

873-
BSONObj IndexCatalogImpl::getDefaultIdIndexSpec(
874-
ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion) const {
873+
BSONObj IndexCatalogImpl::getDefaultIdIndexSpec() const {
875874
dassert(_idObj["_id"].type() == NumberInt);
876875

877-
const auto indexVersion = IndexDescriptor::getDefaultIndexVersion(featureCompatibilityVersion);
876+
const auto indexVersion = IndexDescriptor::getDefaultIndexVersion();
878877

879878
BSONObjBuilder b;
880879
b.append("v", static_cast<int>(indexVersion));

src/mongo/db/catalog/index_catalog_impl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ class IndexCatalogImpl : public IndexCatalog::Impl {
8484
/**
8585
* Returns the spec for the id index to create by default for this collection.
8686
*/
87-
BSONObj getDefaultIdIndexSpec(ServerGlobalParams::FeatureCompatibility::Version
88-
featureCompatibilityVersion) const override;
87+
BSONObj getDefaultIdIndexSpec() const override;
8988

9089
IndexDescriptor* findIdIndex(OperationContext* opCtx) const override;
9190

src/mongo/db/catalog/index_key_validate.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ Status validateKeyPattern(const BSONObj& key, IndexDescriptor::IndexVersion inde
133133

134134
break;
135135
}
136-
case IndexVersion::kV2:
137-
case IndexVersion::kV2Unique: {
136+
case IndexVersion::kV2: {
138137
if (keyElement.isNumber()) {
139138
double value = keyElement.number();
140139
if (std::isnan(value)) {
@@ -220,7 +219,6 @@ StatusWith<BSONObj> validateIndexSpec(
220219
bool hasNamespaceField = false;
221220
bool hasVersionField = false;
222221
bool hasCollationField = false;
223-
bool isUniqueIndex = false;
224222

225223
auto fieldNamesValidStatus = validateIndexSpecFieldNames(indexSpec);
226224
if (!fieldNamesValidStatus.isOK()) {
@@ -365,13 +363,6 @@ StatusWith<BSONObj> validateIndexSpec(
365363
if (!statusWithMatcher.isOK()) {
366364
return statusWithMatcher.getStatus();
367365
}
368-
} else if (IndexDescriptor::kUniqueFieldName == indexSpecElemFieldName) {
369-
// Note: Here we only consider whether or not "unique" field is specified and that its
370-
// value evaluates to true. "_id" index for instance is unique, but the index spec for
371-
// it doesn't carry a "unique" field. "isUniqueIndex" being false for "_id" indexes is
372-
// on purpose, and in future if we were to make "_id" index specs include
373-
// "unique:true", then we would need to change the logic here to preserve its behavior.
374-
isUniqueIndex = indexSpecElem.trueValue();
375366
} else {
376367
// We can assume field name is valid at this point. Validation of fieldname is handled
377368
// prior to this in validateIndexSpecFieldNames().
@@ -380,8 +371,7 @@ StatusWith<BSONObj> validateIndexSpec(
380371
}
381372

382373
if (!resolvedIndexVersion) {
383-
resolvedIndexVersion = IndexDescriptor::getDefaultIndexVersion(
384-
featureCompatibility.getVersion(), isUniqueIndex);
374+
resolvedIndexVersion = IndexDescriptor::getDefaultIndexVersion();
385375
}
386376

387377
if (!hasKeyPatternField) {

src/mongo/db/catalog/index_spec_validate_test.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfVersionIsUnsupported) {
299299
BSON("key" << BSON("field" << 1) << "name"
300300
<< "indexName"
301301
<< "v"
302-
<< 4
302+
<< 3
303303
<< "collation"
304304
<< BSON("locale"
305305
<< "en")),
@@ -398,25 +398,6 @@ TEST(IndexSpecValidateTest, AcceptsIndexVersionV1) {
398398
sorted(result.getValue()));
399399
}
400400

401-
TEST(IndexSpecValidateTest, AcceptsIndexVersionV2Unique) {
402-
auto result = validateIndexSpec(kDefaultOpCtx,
403-
BSON("key" << BSON("field" << 1) << "name"
404-
<< "indexName"
405-
<< "v"
406-
<< 3),
407-
kTestNamespace,
408-
serverGlobalParams.featureCompatibility);
409-
ASSERT_OK(result.getStatus());
410-
411-
// We don't care about the order of the fields in the resulting index specification.
412-
ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name"
413-
<< "indexName"
414-
<< "ns"
415-
<< kTestNamespace.ns()
416-
<< "v"
417-
<< 3)),
418-
sorted(result.getValue()));
419-
}
420401
TEST(IndexSpecValidateTest, ReturnsAnErrorIfCollationIsNotAnObject) {
421402
ASSERT_EQ(ErrorCodes::TypeMismatch,
422403
validateIndexSpec(kDefaultOpCtx,

src/mongo/db/commands/drop_indexes.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ class CmdReIndex : public ErrmsgCommandDeprecated {
142142
// This is necessary to set up CurOp and update the Top stats.
143143
OldClientContext ctx(opCtx, toReIndexNss.ns());
144144

145-
const auto featureCompatibilityVersion =
146-
serverGlobalParams.featureCompatibility.getVersion();
147-
const auto defaultIndexVersion =
148-
IndexDescriptor::getDefaultIndexVersion(featureCompatibilityVersion);
145+
const auto defaultIndexVersion = IndexDescriptor::getDefaultIndexVersion();
149146

150147
vector<BSONObj> all;
151148
{

src/mongo/db/index/btree_key_generator.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ std::unique_ptr<BtreeKeyGenerator> BtreeKeyGenerator::make(IndexVersion indexVer
7777
return stdx::make_unique<BtreeKeyGeneratorV0>(fieldNames, fixed, isSparse);
7878
case IndexVersion::kV1:
7979
case IndexVersion::kV2:
80-
case IndexVersion::kV2Unique:
8180
return stdx::make_unique<BtreeKeyGeneratorV1>(fieldNames, fixed, isSparse, collator);
8281
}
8382
return nullptr;

src/mongo/db/index/index_descriptor.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ bool IndexDescriptor::isIndexVersionSupported(IndexVersion indexVersion) {
101101
case IndexVersion::kV0:
102102
case IndexVersion::kV1:
103103
case IndexVersion::kV2:
104-
case IndexVersion::kV2Unique:
105104
return true;
106105
}
107106
return false;
@@ -120,7 +119,6 @@ Status IndexDescriptor::isIndexVersionAllowedForCreation(
120119
break;
121120
case IndexVersion::kV1:
122121
case IndexVersion::kV2:
123-
case IndexVersion::kV2Unique:
124122
return Status::OK();
125123
}
126124
return {ErrorCodes::CannotCreateIndex,
@@ -129,21 +127,7 @@ Status IndexDescriptor::isIndexVersionAllowedForCreation(
129127
<< static_cast<int>(indexVersion)};
130128
}
131129

132-
IndexVersion IndexDescriptor::getDefaultIndexVersion(
133-
ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion,
134-
bool isUniqueIndex) {
135-
// The gating variable would allow creation of V2 format unique index when set to true.
136-
// Note: Here "isUniqueIndex" only considers whether or not "unique" field is specified
137-
// and that its value evaluates to true. "_id" index for instance is unique, but the
138-
// index spec for it doesn't carry a "unique" field. "isUniqueIndex" being false for
139-
// "_id" indexes is on purpose, and in future if we were to make "_id" index specs
140-
// include "unique:true", then we would need to change the logic here to preserve its
141-
// behavior.
142-
const bool useV2UniqueIndexFormat = false;
143-
if (useV2UniqueIndexFormat && isUniqueIndex) {
144-
return IndexVersion::kV2Unique;
145-
}
146-
130+
IndexVersion IndexDescriptor::getDefaultIndexVersion() {
147131
return IndexVersion::kV2;
148132
}
149133

0 commit comments

Comments
 (0)