diff options
| author | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-18 17:02:53 -0300 |
|---|---|---|
| committer | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-18 17:02:53 -0300 |
| commit | 959575a5ca598bf5f37fb5cebe7ed1d80d3d71f7 (patch) | |
| tree | acc8d60aedb12b70048e676e8a7349deb0010db8 /src/mongo/db/query/util | |
| parent | 76588293975fc059cf076779e4283e6ffaf8afff (diff) | |
New upstream version 6.0.20upstream
Diffstat (limited to 'src/mongo/db/query/util')
| -rw-r--r-- | src/mongo/db/query/util/deferred.h | 118 | ||||
| -rw-r--r-- | src/mongo/db/query/util/deferred_test.cpp | 98 | ||||
| -rw-r--r-- | src/mongo/db/query/util/memory_util.cpp | 128 | ||||
| -rw-r--r-- | src/mongo/db/query/util/memory_util.h | 66 | ||||
| -rw-r--r-- | src/mongo/db/query/util/memory_util_test.cpp | 73 |
5 files changed, 483 insertions, 0 deletions
diff --git a/src/mongo/db/query/util/deferred.h b/src/mongo/db/query/util/deferred.h new file mode 100644 index 00000000000..a2609bb6b49 --- /dev/null +++ b/src/mongo/db/query/util/deferred.h @@ -0,0 +1,118 @@ +/** + * Copyright (C) 2023-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. + */ + +#pragma once + +#include <functional> + +namespace mongo { + +/** + * A template class that provides a way to defer the initialization of an object until its value is + * actually required. This is also commonly referred to as lazy initialization. + * + * Dangers: + * - This implementation is currently not thread safe, and it shouldn't be used in multi-threaded + * fashion. + * - Be careful about using this for lazy initialization of data members and capturing the 'this' + * variable. Code like this will result in buggy/unsafe move constructors, which would have a + * dangling reference to the moved-from type: + * + * class MyType { + * int x; + * // !!! Dangling 'this' when moved !!! + * Deferred<int> xSquared{[this]() { return this->x * this-> x; }; + * }; + * Instead, it is better to do something like this: + * class MyType { + * int xSquared() const { + * return *_xSquared.get(_x); + * } + * + * int _x; + * Deferred<int, int> _xSquared{[](int x) { return x * x; }; + * }; + * - As a similar danger, the value is only computed once. if you initialize it with arguments like + * the above 'xSquared()' implementation, then be cogniscent that the value will never change. If + * '_x' changes, '_xSquared' will not. + * + * A Deferred class can be constructed with either an initial value (eager initialization) or a + * function that will generate the value when needed. + */ +template <typename T, typename... Args> +class Deferred { +public: + /** + * Instantiates a Deffered<T> with the given data - no callbacks or lazy initialization. + */ + Deferred(T data) : _data(data) {} + + /** + * Stores a function to compute a T later. Please note the warnings described in this class + * comment. + */ + Deferred(std::function<T(Args&&...)> initializer) : _initializer(std::move(initializer)) {} + + /** + * Returns a pointer to the managed object. Initializes the object if it hasn't done so already. + */ + T& get(Args&&... args) const { + if (_initializer) { + _data = _initializer(std::forward<Args>(args)...); + _initializer = nullptr; + } + return _data; + } + + /** + * Dereferences the pointer to the managed object. Note this is only a valid shortcut if there + * are no arguments to '_initializer'. + */ + T* operator->() const { + return &get(); + } + + /** + * Returns a referenced to the managed object. Initializes the object if it hasn't done so + * already. Note this is only a valid shortcut if there are no arguments to '_initializer'. + */ + const T& operator*() const { + return get(); + } + + bool isInitialized() const { + return _initializer ? false : true; + } + +private: + mutable T _data; + mutable std::function<T(Args&&...)> _initializer; +}; + +} // namespace mongo diff --git a/src/mongo/db/query/util/deferred_test.cpp b/src/mongo/db/query/util/deferred_test.cpp new file mode 100644 index 00000000000..de256394787 --- /dev/null +++ b/src/mongo/db/query/util/deferred_test.cpp @@ -0,0 +1,98 @@ +/** + * Copyright (C) 2023-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/db/query/util/deferred.h" + +#include "mongo/unittest/unittest.h" + +namespace mongo { +using std::string; +using namespace std::string_literals; + + +TEST(DeferredTest, EagerInitialization) { + Deferred<string> eager{"someString"}; + ASSERT_TRUE(eager.isInitialized()); + ASSERT_EQ(eager.get(), "someString"s); + ASSERT_EQ(*eager, "someString"s); +} + +TEST(DeferredTest, DeferredInitialization) { + size_t initializationCount = 0; + Deferred<string> deferred{[&]() { + initializationCount++; + return "someString"s; + }}; + ASSERT_FALSE(deferred.isInitialized()); + + // Ensure the deferred object wasn't initialized on creation. + ASSERT_EQ(initializationCount, 0); + + // Ensure that the deferred object is initialized on pointer dereferences. + ASSERT_FALSE(deferred->empty()); + ASSERT_TRUE(deferred.isInitialized()); + + ASSERT_EQ(initializationCount, 1); + + // Ensure that the content of the deferred object is equal to its raw counterpart, while also + // verifing that it is initialized at most once. + ASSERT_EQ(deferred.get(), "someString"s); + ASSERT_EQ(initializationCount, 1); +} + +TEST(DeferredTest, DeferredInitializationWithOneArgument) { + size_t initializationCount = 0; + Deferred<string, const string&> deferred{[&](const string& input) { + initializationCount++; + return "{" + input + "}"; + }}; + + // Ensure the deferred object wasn't initialized on creation. + ASSERT_EQ(initializationCount, 0); + + // Ensure that the content of the deferred object is equal to its raw counterpart, while also + // verifing that it is initialized at most once. + ASSERT_EQ(deferred.get("more curlies"), "{more curlies}"s); + ASSERT_EQ(initializationCount, 1); + + // Note that the value is cached, so it's not really valid to call it with a different argument. + ASSERT_EQ(deferred.get("merganser"), "{more curlies}"s); + ASSERT_EQ(initializationCount, 1); +} + +TEST(DeferredTest, DeferredInitializationWithTwoArgs) { + Deferred<string, const string&, const string&> deferred{ + [&](const auto& input, const auto& prefix) { return prefix + input; }}; + + ASSERT_EQ(deferred.get("cowbell", "more "), "more cowbell"s); + ASSERT_EQ(deferred.get("cowbell", "more "), "more cowbell"s); + ASSERT_EQ(deferred.get("cowbell", "less?"), "more cowbell"s); + ASSERT_EQ(deferred.get("tests", "better"), "more cowbell"s); +} +} // namespace mongo diff --git a/src/mongo/db/query/util/memory_util.cpp b/src/mongo/db/query/util/memory_util.cpp new file mode 100644 index 00000000000..8a206deb2b7 --- /dev/null +++ b/src/mongo/db/query/util/memory_util.cpp @@ -0,0 +1,128 @@ +/** + * 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. + */ + +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery + +#include "mongo/db/query/util/memory_util.h" + +#include <cstddef> +#include <pcrecpp.h> + +#include "mongo/logv2/log.h" +#include "mongo/util/processinfo.h" + + +namespace mongo::memory_util { + +StatusWith<MemoryUnits> parseUnitString(const std::string& strUnit) { + if (strUnit.empty()) { + return Status(ErrorCodes::Error{6007010}, "Unit value cannot be empty"); + } + + if (strUnit[0] == '%') { + return MemoryUnits::kPercent; + } else if (strUnit[0] == 'M' || strUnit[0] == 'm') { + return MemoryUnits::kMB; + } else if (strUnit[0] == 'G' || strUnit[0] == 'g') { + return MemoryUnits::kGB; + } + + return Status(ErrorCodes::Error{6007011}, "Incorrect unit value"); +} + +StatusWith<MemorySize> MemorySize::parse(const std::string& str) { + pcrecpp::RE_Options opt; + opt.set_caseless(true); + // Looks for a floating point number with followed by a unit suffix (MB, GB, %). + pcrecpp::RE re("\\s*(\\d+\\.?\\d*)\\s*(MB|GB|%)\\s*", opt); + + double size{}; + std::string strUnit{}; + if (!re.FullMatch(str, &size, &strUnit)) { + return {ErrorCodes::Error{6007012}, "Unable to parse memory size string"}; + } + + auto statusWithUnit = parseUnitString(strUnit); + if (!statusWithUnit.isOK()) { + return statusWithUnit.getStatus(); + } + return MemorySize{size, statusWithUnit.getValue()}; +} + +size_t convertToSizeInBytes(const MemorySize& memSize) { + constexpr size_t kBytesInMB = 1024 * 1024; + constexpr size_t kMBytesInGB = 1024; + + double sizeInMB = memSize.size; + + switch (memSize.units) { + case MemoryUnits::kPercent: + sizeInMB *= ProcessInfo::getMemSizeMB() / 100.0; + break; + case MemoryUnits::kMB: + break; + case MemoryUnits::kGB: + sizeInMB *= kMBytesInGB; + break; + } + + return static_cast<size_t>(sizeInMB * kBytesInMB); +} + +size_t getRequestedMemSizeInBytes(const MemorySize& memSize) { + size_t planCacheSize = convertToSizeInBytes(memSize); + uassert(5968001, + "Cache size must be at least 1KB * number of cores", + planCacheSize >= 1024 * ProcessInfo::getNumCores()); + return planCacheSize; +} + +/** + * Sets upper limit on a storage structure's size. Either that structure's maximumSize or to + * percentage of the total system's memory (both known at call site), whichever is smaller. + */ +size_t capMemorySize(size_t requestedSizeBytes, + size_t maximumSizeGB, + double percentTotalSystemMemory) { + constexpr size_t kBytesInGB = 1024 * 1024 * 1024; + // Express maximum size in bytes. + const size_t maximumSizeBytes = maximumSizeGB * kBytesInGB; + const memory_util::MemorySize limitToProcessSize{percentTotalSystemMemory, + memory_util::MemoryUnits::kPercent}; + const size_t limitToProcessSizeInBytes = convertToSizeInBytes(limitToProcessSize); + + // The size will be capped by the minimum of the two values defined above. + const size_t upperLimit = std::min(maximumSizeBytes, limitToProcessSizeInBytes); + + if (requestedSizeBytes > upperLimit) { + requestedSizeBytes = upperLimit; + } + return requestedSizeBytes; +} +} // namespace mongo::memory_util diff --git a/src/mongo/db/query/util/memory_util.h b/src/mongo/db/query/util/memory_util.h new file mode 100644 index 00000000000..345780b4c84 --- /dev/null +++ b/src/mongo/db/query/util/memory_util.h @@ -0,0 +1,66 @@ +/** + * 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. + */ + +#pragma once + +#include <string> + +#include "mongo/base/error_codes.h" +#include "mongo/base/status_with.h" + +namespace mongo::memory_util { + +/** + * Defines units of memory. + */ +enum class MemoryUnits { + kPercent, + kMB, + kGB, +}; + +/** + * Represents parsed memory size parameter. + */ +struct MemorySize { + static StatusWith<MemorySize> parse(const std::string& str); + + const double size; + const MemoryUnits units; +}; + +StatusWith<MemoryUnits> parseUnitString(const std::string& strUnit); +size_t convertToSizeInBytes(const MemorySize& memSize); +size_t capMemorySize(size_t requestedSizeBytes, + size_t maximumSizeGB, + double percentTotalSystemMemory); +size_t getRequestedMemSizeInBytes(const MemorySize& memSize); + + +} // namespace mongo::memory_util diff --git a/src/mongo/db/query/util/memory_util_test.cpp b/src/mongo/db/query/util/memory_util_test.cpp new file mode 100644 index 00000000000..78f7b3098d6 --- /dev/null +++ b/src/mongo/db/query/util/memory_util_test.cpp @@ -0,0 +1,73 @@ +/** + * 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/db/query/util/memory_util.h" + +#include "mongo/unittest/unittest.h" + +namespace mongo::memory_util { + +bool operator==(const MemorySize& lhs, const MemorySize& rhs) { + constexpr double kEpsilon = 1e-10; + return std::abs(lhs.size - rhs.size) < kEpsilon && lhs.units == rhs.units; +} + +TEST(MemorySizeTest, ParseUnitStringPercent) { + ASSERT_TRUE(MemoryUnits::kPercent == parseUnitString("%")); +} + +TEST(MemorySizeTest, ParseUnitStringMB) { + ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("MB")); + ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("mb")); + ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("mB")); + ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("Mb")); +} + +TEST(MemorySizeTest, ParseUnitStringGB) { + ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("GB")); + ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("gb")); + ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("gB")); + ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("Gb")); +} + +TEST(MemorySizeTest, ParseUnitStringIncorrectValue) { + ASSERT_NOT_OK(parseUnitString("").getStatus()); + ASSERT_NOT_OK(parseUnitString(" ").getStatus()); + ASSERT_NOT_OK(parseUnitString("KB").getStatus()); +} + +TEST(MemorySizeTest, ParseMemorySize) { + ASSERT_TRUE((MemorySize{10.0, MemoryUnits::kPercent}) == MemorySize::parse("10%")); + ASSERT_TRUE((MemorySize{300.0, MemoryUnits::kMB}) == MemorySize::parse("300MB")); + ASSERT_TRUE((MemorySize{4.0, MemoryUnits::kGB}) == MemorySize::parse("4GB")); + ASSERT_TRUE((MemorySize{5.1, MemoryUnits::kPercent}) == MemorySize::parse(" 5.1%")); + ASSERT_TRUE((MemorySize{11.1, MemoryUnits::kMB}) == MemorySize::parse("11.1 mb")); + ASSERT_TRUE((MemorySize{12.1, MemoryUnits::kGB}) == MemorySize::parse(" 12.1 Gb ")); +} +} // namespace mongo::memory_util |
