diff options
Diffstat (limited to 'src/mongo/db/sorter/sorter.h')
| -rw-r--r-- | src/mongo/db/sorter/sorter.h | 82 |
1 files changed, 31 insertions, 51 deletions
diff --git a/src/mongo/db/sorter/sorter.h b/src/mongo/db/sorter/sorter.h index 059d7b72f46..12bccee1178 100644 --- a/src/mongo/db/sorter/sorter.h +++ b/src/mongo/db/sorter/sorter.h @@ -42,9 +42,7 @@ #include "mongo/bson/util/builder.h" #include "mongo/db/exec/document_value/document.h" -#include "mongo/db/query/query_shape/serialization_options.h" #include "mongo/db/sorter/sorter_gen.h" -#include "mongo/db/sorter/sorter_stats.h" #include "mongo/platform/atomic_word.h" #include "mongo/util/assert_util.h" #include "mongo/util/bufreader.h" @@ -98,6 +96,14 @@ namespace mongo { /** + * For collecting file usage metrics. + */ +struct SorterFileStats { + AtomicWord<long long> opened; + AtomicWord<long long> closed; +}; + +/** * Runtime options that control the Sorter's behavior */ struct SortOptions { @@ -124,14 +130,6 @@ struct SortOptions { // If set, allows us to observe Sorter file handle usage. SorterFileStats* sorterFileStats; - // If set, allows us to observe aggregate Sorter behaviors. - SorterTracker* sorterTracker; - - // When set, this sorter will own a memory pool that callers should used to allocate memory for - // the keys we are sorting. If enabled, any values returned by memUsageForSorter() will be - // ignored. - bool useMemPool; - // If set to true and sorted data fits into memory, sorted data will be moved into iterator // instead of copying. bool moveSortedDataIntoIterator; @@ -141,8 +139,6 @@ struct SortOptions { maxMemoryUsageBytes(64 * 1024 * 1024), extSortAllowed(false), sorterFileStats(nullptr), - sorterTracker(nullptr), - useMemPool(false), moveSortedDataIntoIterator(false) {} // Fluent API to support expressions like SortOptions().Limit(1000).ExtSortAllowed(true) @@ -177,20 +173,10 @@ struct SortOptions { return *this; } - SortOptions& Tracker(SorterTracker* newSorterTracker) { - sorterTracker = newSorterTracker; - return *this; - } - SortOptions& MoveSortedDataIntoIterator(bool newMoveSortedDataIntoIterator = true) { moveSortedDataIntoIterator = newMoveSortedDataIntoIterator; return *this; } - - SortOptions& UseMemoryPool(bool usePool) { - useMemPool = usePool; - return *this; - } }; /** @@ -255,18 +241,6 @@ protected: SortIteratorInterface() {} // can only be constructed as a base }; -class SorterBase { -public: - SorterBase(SorterTracker* sorterTracker = nullptr) : _stats(sorterTracker) {} - - const SorterStats& stats() const { - return _stats; - } - -protected: - SorterStats _stats; -}; - /** * This is the way to input data to the sorting framework. * @@ -283,7 +257,7 @@ protected: * nextFileName() for example. */ template <typename Key, typename Value> -class Sorter : public SorterBase { +class Sorter { Sorter(const Sorter&) = delete; Sorter& operator=(const Sorter&) = delete; @@ -305,7 +279,10 @@ public: */ class File { public: - File(std::string path, SorterFileStats* stats = nullptr); + File(std::string path, SorterFileStats* stats = nullptr) + : _path(std::move(path)), _stats(stats) { + invariant(!_path.empty()); + } ~File(); @@ -388,6 +365,10 @@ public: virtual ~Sorter() {} + size_t numSpills() const { + return _numSpills; + } + size_t numSorted() const { return _numSorted; } @@ -398,12 +379,9 @@ public: PersistedState persistDataForShutdown(); - SharedBufferFragmentBuilder& memPool() { - invariant(_memPool); - return _memPool.get(); - } - protected: + Sorter() {} // can only be constructed as a base + virtual void spill() = 0; size_t _numSorted = 0; // Keeps track of the number of keys sorted. @@ -413,18 +391,14 @@ protected: std::shared_ptr<File> _file; + std::size_t _numSpills = 0; // Keeps track of the number of spills that have happened. std::vector<std::shared_ptr<Iterator>> _iters; // Data that has already been spilled. - - boost::optional<SharedBufferFragmentBuilder> _memPool; }; template <typename Key, typename Value> -class BoundedSorterInterface : public SorterBase { - +class BoundedSorterInterface { public: - BoundedSorterInterface(const SortOptions& opts) : SorterBase(opts.sorterTracker) {} - virtual ~BoundedSorterInterface() {} // Feed one item of input to the sorter. @@ -462,9 +436,10 @@ public: virtual std::pair<Key, Value> next() = 0; // Serialize the bound for explain output - virtual Document serializeBound(const SerializationOptions& opts) const = 0; + virtual Document serializeBound() const = 0; virtual size_t totalDataSizeBytes() const = 0; + virtual size_t numSpills() const = 0; virtual size_t limit() const = 0; // By default, uassert that the input meets our assumptions of being almost-sorted. @@ -542,14 +517,18 @@ public: std::pair<Key, Value> next(); // Serialize the bound for explain output - Document serializeBound(const SerializationOptions& opts) const { - return {makeBound.serialize(opts)}; + Document serializeBound() const { + return {makeBound.serialize()}; }; size_t totalDataSizeBytes() const { return _totalDataSizeSorted; } + size_t numSpills() const { + return _numSpills; + } + size_t limit() const { return _opts.limit; } @@ -584,6 +563,7 @@ private: std::shared_ptr<typename Sorter<Key, Value>::File> _file; std::shared_ptr<SpillIterator> _spillIter; + std::size_t _numSpills = 0; // Keeps track of the number of spills that have happened. boost::optional<Key> _min; bool _done = false; @@ -634,7 +614,7 @@ private: // be given to the Iterator in done(). std::streamoff _fileStartOffset; - SortOptions _opts; + boost::optional<std::string> _dbName; }; } // namespace mongo |
