diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/catalog/index_key_validate.cpp | 16 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_key_validate.h | 6 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_key_validate_test.cpp | 14 |
3 files changed, 33 insertions, 3 deletions
diff --git a/src/mongo/db/catalog/index_key_validate.cpp b/src/mongo/db/catalog/index_key_validate.cpp index 3fc2aa28304..6e7c5faeb09 100644 --- a/src/mongo/db/catalog/index_key_validate.cpp +++ b/src/mongo/db/catalog/index_key_validate.cpp @@ -269,8 +269,8 @@ BSONObj removeUnknownFields(const NamespaceString& ns, const BSONObj& indexSpec) BSONObj repairIndexSpec(const NamespaceString& ns, const BSONObj& indexSpec, const std::set<StringData>& allowedFieldNames) { - auto fixBoolIndexSpecFn = [&indexSpec, &ns](const BSONElement& indexSpecElem, - BSONObjBuilder* builder) { + auto fixIndexSpecFn = [&indexSpec, &ns](const BSONElement& indexSpecElem, + BSONObjBuilder* builder) { StringData fieldName = indexSpecElem.fieldNameStringData(); if ((IndexDescriptor::kBackgroundFieldName == fieldName || IndexDescriptor::kUniqueFieldName == fieldName || @@ -285,11 +285,21 @@ BSONObj repairIndexSpec(const NamespaceString& ns, "fieldName"_attr = redact(fieldName), "indexSpec"_attr = redact(indexSpec)); builder->appendBool(fieldName, true); + } else if (IndexDescriptor::kExpireAfterSecondsFieldName == fieldName && + !(indexSpecElem.isNumber() && !indexSpecElem.isNaN())) { + LOGV2_WARNING(6835900, + "Fixing expire field from TTL index spec", + "namespace"_attr = redact(ns.toString()), + "fieldName"_attr = redact(fieldName), + "indexSpec"_attr = redact(indexSpec)); + builder->appendNumber(fieldName, + durationCount<Seconds>(kExpireAfterSecondsForInactiveTTLIndex)); } else { builder->append(indexSpecElem); } }; - return buildRepairedIndexSpec(ns, indexSpec, allowedFieldNames, fixBoolIndexSpecFn); + + return buildRepairedIndexSpec(ns, indexSpec, allowedFieldNames, fixIndexSpecFn); } StatusWith<BSONObj> validateIndexSpec(OperationContext* opCtx, const BSONObj& indexSpec) { diff --git a/src/mongo/db/catalog/index_key_validate.h b/src/mongo/db/catalog/index_key_validate.h index f9680ac035b..2ba7174c1b0 100644 --- a/src/mongo/db/catalog/index_key_validate.h +++ b/src/mongo/db/catalog/index_key_validate.h @@ -43,6 +43,12 @@ class StatusWith; namespace index_key_validate { +// TTL indexes with 'expireAfterSeconds' are repaired with this duration, which is chosen to be +// the largest possible value for the 'safeInt' type that can be returned in the listIndexes +// response. +constexpr auto kExpireAfterSecondsForInactiveTTLIndex = + Seconds(std::numeric_limits<int32_t>::max()); + static std::set<StringData> allowedFieldNames = { IndexDescriptor::k2dIndexBitsFieldName, IndexDescriptor::k2dIndexMaxFieldName, diff --git a/src/mongo/db/catalog/index_key_validate_test.cpp b/src/mongo/db/catalog/index_key_validate_test.cpp index 9c4813a2d2e..c60df1b34aa 100644 --- a/src/mongo/db/catalog/index_key_validate_test.cpp +++ b/src/mongo/db/catalog/index_key_validate_test.cpp @@ -426,6 +426,20 @@ TEST(IndexKeyValidateTest, RepairIndexSpecs) { NamespaceString("coll"), fromjson("{key: {a: 1}, name: 'index', sparse: 'true', background: '1', safe: " "true, force: true}")))); + + ASSERT(BSON("key" << BSON("a" << 1) << "name" + << "index" + << "expireAfterSeconds" << std::numeric_limits<int32_t>::max()) + .binaryEqual(index_key_validate::repairIndexSpec( + NamespaceString("coll"), + fromjson("{key: {a: 1}, name: 'index', expireAfterSeconds: NaN}")))); + + ASSERT(BSON("key" << BSON("a" << 1) << "name" + << "index" + << "expireAfterSeconds" << std::numeric_limits<int32_t>::max()) + .binaryEqual(index_key_validate::repairIndexSpec( + NamespaceString("coll"), + fromjson("{key: {a: 1}, name: 'index', expireAfterSeconds: '123'}")))); } TEST(IndexKeyValidateTest, GeoIndexSpecs) { |