diff options
author | Nikita Lapkov <nikita.lapkov@mongodb.com> | 2021-06-10 17:02:39 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-16 11:20:22 +0000 |
commit | 0acd557cb0f788ff82ab3a2e4c437d881c6c5c0a (patch) | |
tree | a200123f72432014020958fe24c9f9d0c8e9a387 /src/mongo/db/timeseries | |
parent | 36edc888b6415b7dc37c13bc799a18bcaa4d8236 (diff) | |
download | mongo-0acd557cb0f788ff82ab3a2e4c437d881c6c5c0a.tar.gz |
SERVER-58175 Round time values before routing inserts to sharded timeseries collections
(cherry picked from commit d644971af8d2315cadf23b2c7addd7fe05ea3c2d)
Diffstat (limited to 'src/mongo/db/timeseries')
-rw-r--r-- | src/mongo/db/timeseries/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/db/timeseries/bucket_catalog.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/timeseries/timeseries_options.cpp | 66 | ||||
-rw-r--r-- | src/mongo/db/timeseries/timeseries_options.h | 10 | ||||
-rw-r--r-- | src/mongo/db/timeseries/timeseries_options_test.cpp | 63 |
5 files changed, 111 insertions, 40 deletions
diff --git a/src/mongo/db/timeseries/SConscript b/src/mongo/db/timeseries/SConscript index 866be00d415..8a75cd7fbb9 100644 --- a/src/mongo/db/timeseries/SConscript +++ b/src/mongo/db/timeseries/SConscript @@ -76,11 +76,13 @@ env.CppUnitTest( 'minmax_test.cpp', 'timeseries_index_schema_conversion_functions_test.cpp', 'timeseries_update_delete_util_test.cpp', + 'timeseries_options_test.cpp' ], LIBDEPS=[ '$BUILD_DIR/mongo/db/catalog/catalog_test_fixture', 'bucket_catalog', 'timeseries_index_schema_conversion_functions', + 'timeseries_options', 'timeseries_update_delete_util', ], ) diff --git a/src/mongo/db/timeseries/bucket_catalog.cpp b/src/mongo/db/timeseries/bucket_catalog.cpp index e65da2ce66f..33087511caa 100644 --- a/src/mongo/db/timeseries/bucket_catalog.cpp +++ b/src/mongo/db/timeseries/bucket_catalog.cpp @@ -150,13 +150,6 @@ OperationId getOpId(OperationContext* opCtx, MONGO_UNREACHABLE; } -long long roundTimestampDown(const Date_t& time, const TimeseriesOptions& options) { - int roundingSeconds = - timeseries::getBucketRoundingSecondsFromGranularity(options.getGranularity()); - long long seconds = durationCount<Seconds>(time.toDurationSinceEpoch()); - return (seconds - (seconds % roundingSeconds)); -} - BSONObj buildControlMinTimestampDoc(StringData timeField, long long roundedSeconds) { BSONObjBuilder builder; builder.append(timeField, Date_t::fromMillisSinceEpoch(1000 * roundedSeconds)); @@ -654,7 +647,8 @@ const std::shared_ptr<BucketCatalog::ExecutionStats> BucketCatalog::_getExecutio void BucketCatalog::_setIdTimestamp(Bucket* bucket, const Date_t& time, const TimeseriesOptions& options) { - auto const roundedSeconds = roundTimestampDown(time, options); + auto roundedTime = timeseries::roundTimestampToGranularity(time, options.getGranularity()); + auto const roundedSeconds = durationCount<Seconds>(roundedTime.toDurationSinceEpoch()); bucket->_id.setTimestamp(roundedSeconds); // Make sure we set the control.min time field to match the rounded _id timestamp. diff --git a/src/mongo/db/timeseries/timeseries_options.cpp b/src/mongo/db/timeseries/timeseries_options.cpp index 81a3fc15cb4..e8b1df78f91 100644 --- a/src/mongo/db/timeseries/timeseries_options.cpp +++ b/src/mongo/db/timeseries/timeseries_options.cpp @@ -103,33 +103,6 @@ int getMaxSpanSecondsFromGranularity(BucketGranularityEnum granularity) { MONGO_UNREACHABLE; } -int getBucketRoundingSecondsFromGranularity(BucketGranularityEnum granularity) { - switch (granularity) { - case BucketGranularityEnum::Seconds: - // Round down to nearest minute. - return 60; - case BucketGranularityEnum::Minutes: - // Round down to nearest hour. - return 60 * 60; - case BucketGranularityEnum::Hours: - // Round down to hearest day. - return 60 * 60 * 24; - } - MONGO_UNREACHABLE; -} -bool optionsAreEqual(const TimeseriesOptions& option1, const TimeseriesOptions& option2) { - const auto option1BucketSpan = option1.getBucketMaxSpanSeconds() - ? *option1.getBucketMaxSpanSeconds() - : getMaxSpanSecondsFromGranularity(option1.getGranularity()); - const auto option2BucketSpan = option2.getBucketMaxSpanSeconds() - ? *option2.getBucketMaxSpanSeconds() - : getMaxSpanSecondsFromGranularity(option2.getGranularity()); - return option1.getTimeField() == option1.getTimeField() && - option1.getMetaField() == option2.getMetaField() && - option1.getGranularity() == option2.getGranularity() && - option1BucketSpan == option2BucketSpan; -} - StatusWith<std::pair<TimeseriesOptions, bool>> applyTimeseriesOptionsModifications( const TimeseriesOptions& currentOptions, const BSONObj& mod) { TimeseriesOptions newOptions = currentOptions; @@ -171,5 +144,44 @@ BSONObj generateViewPipeline(const TimeseriesOptions& options, bool asArray) { << *options.getBucketMaxSpanSeconds() << "exclude" << BSONArray()))); } +bool optionsAreEqual(const TimeseriesOptions& option1, const TimeseriesOptions& option2) { + const auto option1BucketSpan = option1.getBucketMaxSpanSeconds() + ? *option1.getBucketMaxSpanSeconds() + : getMaxSpanSecondsFromGranularity(option1.getGranularity()); + const auto option2BucketSpan = option2.getBucketMaxSpanSeconds() + ? *option2.getBucketMaxSpanSeconds() + : getMaxSpanSecondsFromGranularity(option2.getGranularity()); + return option1.getTimeField() == option1.getTimeField() && + option1.getMetaField() == option2.getMetaField() && + option1.getGranularity() == option2.getGranularity() && + option1BucketSpan == option2BucketSpan; +} + +namespace { +/** + * Returns the number of seconds used to round down the bucket ID and control.min timestamp. + */ +int getBucketRoundingSecondsFromGranularity(BucketGranularityEnum granularity) { + switch (granularity) { + case BucketGranularityEnum::Seconds: + // Round down to nearest minute. + return 60; + case BucketGranularityEnum::Minutes: + // Round down to nearest hour. + return 60 * 60; + case BucketGranularityEnum::Hours: + // Round down to nearest day. + return 60 * 60 * 24; + } + MONGO_UNREACHABLE; +} +} // namespace + +Date_t roundTimestampToGranularity(const Date_t& time, BucketGranularityEnum granularity) { + int roundingSeconds = getBucketRoundingSecondsFromGranularity(granularity); + long long timeSeconds = durationCount<Seconds>(time.toDurationSinceEpoch()); + long long roundedTimeSeconds = (timeSeconds - (timeSeconds % roundingSeconds)); + return Date_t::fromDurationSinceEpoch(Seconds{roundedTimeSeconds}); +} } // namespace timeseries } // namespace mongo diff --git a/src/mongo/db/timeseries/timeseries_options.h b/src/mongo/db/timeseries/timeseries_options.h index feaf7e3cd33..87bf4387473 100644 --- a/src/mongo/db/timeseries/timeseries_options.h +++ b/src/mongo/db/timeseries/timeseries_options.h @@ -53,16 +53,16 @@ boost::optional<TimeseriesOptions> getTimeseriesOptions(OperationContext* opCtx, */ int getMaxSpanSecondsFromGranularity(BucketGranularityEnum granularity); -/** - * Returns the number of seconds used to round down the bucket ID and control.min timestamp. - */ -int getBucketRoundingSecondsFromGranularity(BucketGranularityEnum granularity); - StatusWith<std::pair<TimeseriesOptions, bool>> applyTimeseriesOptionsModifications( const TimeseriesOptions& current, const BSONObj& mod); BSONObj generateViewPipeline(const TimeseriesOptions& options, bool asArray); bool optionsAreEqual(const TimeseriesOptions& option1, const TimeseriesOptions& option2); + +/** + * Rounds down timestamp to the specified granularity. + */ +Date_t roundTimestampToGranularity(const Date_t& time, BucketGranularityEnum granularity); } // namespace timeseries } // namespace mongo diff --git a/src/mongo/db/timeseries/timeseries_options_test.cpp b/src/mongo/db/timeseries/timeseries_options_test.cpp new file mode 100644 index 00000000000..e3106407cea --- /dev/null +++ b/src/mongo/db/timeseries/timeseries_options_test.cpp @@ -0,0 +1,63 @@ +/** + * Copyright (C) 2021-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include "mongo/db/timeseries/timeseries_options.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/time_support.h" + + +namespace mongo { + +TEST(TimeseriesOptionsTest, RoundTimestampToGranularity) { + std::vector<std::tuple<BucketGranularityEnum, std::string, std::string>> testCases{ + {BucketGranularityEnum::Seconds, "2021-01-01T00:00:15.555Z", "2021-01-01T00:00:00.000Z"}, + {BucketGranularityEnum::Seconds, "2021-01-01T00:00:30.555Z", "2021-01-01T00:00:00.000Z"}, + {BucketGranularityEnum::Seconds, "2021-01-01T00:00:45.555Z", "2021-01-01T00:00:00.000Z"}, + + {BucketGranularityEnum::Minutes, "2021-01-01T00:15:00.000Z", "2021-01-01T00:00:00.000Z"}, + {BucketGranularityEnum::Minutes, "2021-01-01T00:30:00.000Z", "2021-01-01T00:00:00.000Z"}, + {BucketGranularityEnum::Minutes, "2021-01-01T00:45:00.000Z", "2021-01-01T00:00:00.000Z"}, + + {BucketGranularityEnum::Hours, "2021-01-01T06:00:00.000Z", "2021-01-01T00:00:00.000Z"}, + {BucketGranularityEnum::Hours, "2021-01-01T12:00:00.000Z", "2021-01-01T00:00:00.000Z"}, + {BucketGranularityEnum::Hours, "2021-01-01T18:00:00.000Z", "2021-01-01T00:00:00.000Z"}, + }; + + for (const auto& [granularity, input, expectedOutput] : testCases) { + auto inputDate = dateFromISOString(input); + ASSERT_OK(inputDate); + auto roundedDate = + timeseries::roundTimestampToGranularity(inputDate.getValue(), granularity); + ASSERT_EQ(dateToISOStringUTC(roundedDate), expectedOutput); + } +} + +} // namespace mongo |