diff --git a/src/mongo/db/catalog/SConscript b/src/mongo/db/catalog/SConscript index 74fab69837c..3fbf3848a15 100644 --- a/src/mongo/db/catalog/SConscript +++ b/src/mongo/db/catalog/SConscript @@ -314,6 +314,23 @@ env.Benchmark( ], ) +env.Benchmark( + target="collection_bm", + source=[ + "collection_bm.cpp", + ], + LIBDEPS=[ + "$BUILD_DIR/mongo/db/commands/create_command", + "$BUILD_DIR/mongo/db/concurrency/lock_manager", + "$BUILD_DIR/mongo/db/repl/oplog", + "$BUILD_DIR/mongo/db/repl/replmocks", + "$BUILD_DIR/mongo/db/service_context_d_test_fixture", + "$BUILD_DIR/mongo/db/storage/write_unit_of_work", + "catalog_helpers", + "collection_catalog", + ], +) + env.Library( target="catalog_control", source=[ diff --git a/src/mongo/db/catalog/collection.h b/src/mongo/db/catalog/collection.h index 0b054d756ce..f46c305480c 100644 --- a/src/mongo/db/catalog/collection.h +++ b/src/mongo/db/catalog/collection.h @@ -390,6 +390,7 @@ public: * If FCV < 5.2 or if this is not a time-series collection, returns boost::none. */ virtual boost::optional getTimeseriesBucketsMayHaveMixedSchemaData() const = 0; + virtual boost::optional getTimeseriesBucketsMayHaveMixedSchemaData2() const = 0; /** * Sets the 'timeseriesBucketsMayHaveMixedSchemaData' catalog entry flag to 'setting' for this diff --git a/src/mongo/db/catalog/collection_bm.cpp b/src/mongo/db/catalog/collection_bm.cpp new file mode 100644 index 00000000000..982933d62cf --- /dev/null +++ b/src/mongo/db/catalog/collection_bm.cpp @@ -0,0 +1,118 @@ +/** + * 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 + * . + * + * 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 + +#include "mongo/db/catalog/create_collection.h" +#include "mongo/db/commands/create_gen.h" +#include "mongo/db/repl/replication_coordinator_mock.h" +#include "mongo/db/service_context.h" +#include "mongo/db/service_context_d_test_fixture.h" +#include "mongo/util/version/releases.h" + +namespace mongo { + +class CollectionBM : public ServiceContextMongoDTest { +public: + void setUp() override { + logv2::LogManager::global().getGlobalSettings().setMinimumLoggedSeverity( + mongo::logv2::LogComponent::kDefault, mongo::logv2::LogSeverity::Severe()); + serverGlobalParams.mutableFCV.setVersion(multiversion::GenericFCV::kLatest); + + // Set up mongod. + ServiceContextMongoDTest::setUp(); + + auto service = getServiceContext(); + + // Set up ReplicationCoordinator and ensure that we are primary. + auto replCoord = std::make_unique(service); + uassertStatusOK(replCoord->setFollowerMode(repl::MemberState::RS_PRIMARY)); + repl::ReplicationCoordinator::set(service, std::move(replCoord)); + } + void tearDown() override { + // Tear down mongod. + ServiceContextMongoDTest::tearDown(); + } + ServiceContext::UniqueOperationContext makeOpCtx() { + auto opCtx = cc().makeOperationContext(); + repl::createOplog(opCtx.get()); + return opCtx; + } + +private: + virtual void _doTest() override{}; +}; + +void BM_CollectionGetTimeseriesBucketsMayHaveMixedSchemaData(benchmark::State& state) { + CollectionBM cbm; + cbm.setUp(); + + auto opCtx = cbm.makeOpCtx(); + + NamespaceString curNss = NamespaceString::createNamespaceString_forTest("test.curColl"); + auto bucketsColl = + NamespaceString::createNamespaceString_forTest("test.system.buckets.curColl"); + + auto tsOptions = TimeseriesOptions("t"); + CreateCommand cmd = CreateCommand(curNss); + cmd.getCreateCollectionRequest().setTimeseries(std::move(tsOptions)); + uassertStatusOK(createCollection(opCtx.get(), cmd)); + + boost::optional hasMixedSchema = + state.range(1) ? boost::optional(true) : boost::none; + + { + Lock::GlobalLock gLock(opCtx.get(), MODE_X); + WriteUnitOfWork wuow(opCtx.get()); + auto coll = CollectionCatalog::get(opCtx.get()) + ->lookupCollectionByNamespaceForMetadataWrite(opCtx.get(), bucketsColl); + + coll->setTimeseriesBucketsMayHaveMixedSchemaData(opCtx.get(), hasMixedSchema); + } + + auto coll = + CollectionCatalog::get(opCtx.get())->lookupCollectionByNamespace(opCtx.get(), bucketsColl); + + if (state.range(1)) { + + for (auto _ : state) { + benchmark::DoNotOptimize(coll->getTimeseriesBucketsMayHaveMixedSchemaData()); + } + } else { + for (auto _ : state) { + benchmark::DoNotOptimize(coll->getTimeseriesBucketsMayHaveMixedSchemaData2()); + } + } + + cbm.tearDown(); +} + +BENCHMARK(BM_CollectionGetTimeseriesBucketsMayHaveMixedSchemaData)->Ranges({{1, 100'000}, {0, 1}}); + +} // namespace mongo diff --git a/src/mongo/db/catalog/collection_impl.cpp b/src/mongo/db/catalog/collection_impl.cpp index e1ee6ad3e32..7d1584ec751 100644 --- a/src/mongo/db/catalog/collection_impl.cpp +++ b/src/mongo/db/catalog/collection_impl.cpp @@ -845,6 +845,15 @@ boost::optional CollectionImpl::getTimeseriesBucketsMayHaveMixedSchemaData return _metadata->timeseriesBucketsMayHaveMixedSchemaData; } +boost::optional CollectionImpl::getTimeseriesBucketsMayHaveMixedSchemaData2() const { + if (!getTimeseriesOptions()) { + return boost::none; + } + + // Else, fallback to legacy parameter + return _metadata->timeseriesBucketsMayHaveMixedSchemaData; +} + boost::optional CollectionImpl::timeseriesBucketingParametersHaveChanged() const { if (!getTimeseriesOptions()) { return boost::none; diff --git a/src/mongo/db/catalog/collection_impl.h b/src/mongo/db/catalog/collection_impl.h index f6b1d34c922..fbe93173b05 100644 --- a/src/mongo/db/catalog/collection_impl.h +++ b/src/mongo/db/catalog/collection_impl.h @@ -238,6 +238,7 @@ public: bool isTemporary() const final; boost::optional getTimeseriesBucketsMayHaveMixedSchemaData() const final; + boost::optional getTimeseriesBucketsMayHaveMixedSchemaData2() const final; void setTimeseriesBucketsMayHaveMixedSchemaData(OperationContext* opCtx, boost::optional setting) final; diff --git a/src/mongo/db/catalog/collection_mock.h b/src/mongo/db/catalog/collection_mock.h index 7db180d5dab..ab2215acc28 100644 --- a/src/mongo/db/catalog/collection_mock.h +++ b/src/mongo/db/catalog/collection_mock.h @@ -204,6 +204,10 @@ public: MONGO_UNREACHABLE; } + boost::optional getTimeseriesBucketsMayHaveMixedSchemaData2() const override { + MONGO_UNREACHABLE; + } + void setTimeseriesBucketsMayHaveMixedSchemaData(OperationContext* opCtx, boost::optional setting) override { MONGO_UNREACHABLE; diff --git a/src/mongo/db/catalog/virtual_collection_impl.h b/src/mongo/db/catalog/virtual_collection_impl.h index 637feccb3c7..6f43b063868 100644 --- a/src/mongo/db/catalog/virtual_collection_impl.h +++ b/src/mongo/db/catalog/virtual_collection_impl.h @@ -268,6 +268,11 @@ public: return boost::none; } + boost::optional getTimeseriesBucketsMayHaveMixedSchemaData2() const final { + unimplementedTasserted(); + return boost::none; + } + void setTimeseriesBucketsMayHaveMixedSchemaData(OperationContext* opCtx, boost::optional setting) final { unimplementedTasserted();