diff options
author | Gregory Wlodarek <gregory.wlodarek@mongodb.com> | 2021-07-22 14:06:33 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-07-23 22:32:22 +0000 |
commit | 484192f1d6c598c926a4d74af6faccc2228bed80 (patch) | |
tree | 1352b339635a5dc79c0c79ded0a30c9c35ba02de /src | |
parent | daefcb2220ec0ce848900a93036f35b3b4c3d2d8 (diff) | |
download | mongo-484192f1d6c598c926a4d74af6faccc2228bed80.tar.gz |
SERVER-58057 Support ascending and descending indexes on time-series measurements
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/storage/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/db/timeseries/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp | 75 |
3 files changed, 55 insertions, 23 deletions
diff --git a/src/mongo/db/storage/SConscript b/src/mongo/db/storage/SConscript index 1805672b5cd..01563a09d48 100644 --- a/src/mongo/db/storage/SConscript +++ b/src/mongo/db/storage/SConscript @@ -165,9 +165,9 @@ env.Library( ], LIBDEPS=[ '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/idl/feature_flag', ], LIBDEPS_PRIVATE=[ - '$BUILD_DIR/mongo/idl/feature_flag', '$BUILD_DIR/mongo/idl/server_parameter', ], ) diff --git a/src/mongo/db/timeseries/SConscript b/src/mongo/db/timeseries/SConscript index 88c60d82eb9..7051550a57e 100644 --- a/src/mongo/db/timeseries/SConscript +++ b/src/mongo/db/timeseries/SConscript @@ -38,6 +38,7 @@ env.Library( 'timeseries_index_schema_conversion_functions.cpp', ], LIBDEPS_PRIVATE=[ + '$BUILD_DIR/mongo/db/storage/storage_options', 'timeseries_idl', ], ) diff --git a/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp b/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp index dccdbbf3893..75130c5c852 100644 --- a/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp +++ b/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp @@ -33,6 +33,7 @@ #include "mongo/db/timeseries/timeseries_index_schema_conversion_functions.h" +#include "mongo/db/storage/storage_parameters_gen.h" #include "mongo/db/timeseries/timeseries_constants.h" #include "mongo/db/timeseries/timeseries_gen.h" #include "mongo/logv2/log.h" @@ -83,35 +84,65 @@ StatusWith<BSONObj> createBucketsSpecFromTimeseriesSpec(const TimeseriesOptions& continue; } - if (!metaField) { - return {ErrorCodes::BadValue, - str::stream() << "Invalid index spec for time-series collection: " - << redact(timeseriesIndexSpecBSON) - << ". Indexes are only allowed on the '" << timeField - << "' field, no other data fields are supported: " << elem}; - } + if (metaField) { + if (elem.fieldNameStringData() == *metaField) { + // The time-series 'metaField' field name always maps to a field named + // timeseries::kBucketMetaFieldName on the underlying buckets collection. + builder.appendAs(elem, timeseries::kBucketMetaFieldName); + continue; + } - if (elem.fieldNameStringData() == *metaField) { - // The time-series 'metaField' field name always maps to a field named - // timeseries::kBucketMetaFieldName on the underlying buckets collection. - builder.appendAs(elem, timeseries::kBucketMetaFieldName); - continue; + // Time-series indexes on sub-documents of the 'metaField' are allowed. + if (elem.fieldNameStringData().startsWith(*metaField + ".")) { + builder.appendAs(elem, + str::stream() + << timeseries::kBucketMetaFieldName << "." + << elem.fieldNameStringData().substr(metaField->size() + 1)); + continue; + } } - // Lastly, time-series indexes on sub-documents of the 'metaField' are allowed. - if (elem.fieldNameStringData().startsWith(*metaField + ".")) { - builder.appendAs(elem, - str::stream() - << timeseries::kBucketMetaFieldName << "." - << elem.fieldNameStringData().substr(metaField->size() + 1)); - continue; + if (!feature_flags::gTimeseriesMetricIndexes.isEnabledAndIgnoreFCV()) { + auto reason = str::stream(); + reason << "Invalid index spec for time-series collection: " + << redact(timeseriesIndexSpecBSON) << ". "; + reason << "Indexes are only supported on the '" << timeField << "' "; + if (metaField) { + reason << "and '" << *metaField << "' fields. "; + } else { + reason << "field. "; + } + reason << "Attempted to create an index on the field '" << elem.fieldName() << "'."; + return {ErrorCodes::BadValue, reason}; } - return {ErrorCodes::BadValue, + // Indexes on measurement fields are only supported when the 'gTimeseriesMetricIndexes' + // feature flag is enabled. + if (!elem.isNumber()) { + return { + ErrorCodes::BadValue, str::stream() << "Invalid index spec for time-series collection: " << redact(timeseriesIndexSpecBSON) - << ". Indexes are only supported on the '" << *metaField << "' and '" - << timeField << "' fields: " << elem}; + << ". Indexes on measurement fields must be ascending or descending " + "(numbers only): " + << elem}; + } + + if (elem.number() >= 0) { + // For ascending key patterns, the { control.max.elem: 1, control.min.elem: 1 } + // compound index is created. + builder.appendAs( + elem, str::stream() << timeseries::kControlMaxFieldNamePrefix << elem.fieldName()); + builder.appendAs( + elem, str::stream() << timeseries::kControlMinFieldNamePrefix << elem.fieldName()); + } else if (elem.number() < 0) { + // For descending key patterns, the { control.min.elem: -1, control.max.elem: -1 } + // compound index is created. + builder.appendAs( + elem, str::stream() << timeseries::kControlMinFieldNamePrefix << elem.fieldName()); + builder.appendAs( + elem, str::stream() << timeseries::kControlMaxFieldNamePrefix << elem.fieldName()); + } } return builder.obj(); |