From 1d79792d701ab899165a7c56108633bbeac3c924 Mon Sep 17 00:00:00 2001 From: Yuhong Zhang Date: Wed, 8 Feb 2023 23:53:43 +0000 Subject: SERVER-73602 Add the stub of the TIMESERIES_WRITE query stage --- src/mongo/db/SConscript | 1 + src/mongo/db/exec/plan_stats.h | 20 +++++++ src/mongo/db/exec/plan_stats_visitor.h | 3 + src/mongo/db/exec/timeseries_write.cpp | 53 ++++++++++++++++++ src/mongo/db/exec/timeseries_write.h | 83 ++++++++++++++++++++++++++++ src/mongo/db/query/classic_stage_builder.cpp | 1 + src/mongo/db/query/plan_executor_impl.cpp | 5 ++ src/mongo/db/query/stage_types.cpp | 1 + src/mongo/db/query/stage_types.h | 3 + 9 files changed, 170 insertions(+) create mode 100644 src/mongo/db/exec/timeseries_write.cpp create mode 100644 src/mongo/db/exec/timeseries_write.h diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index 7a5931d98db..d198bd8e5e3 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -1354,6 +1354,7 @@ env.Library( 'exec/subplan.cpp', 'exec/text_match.cpp', 'exec/text_or.cpp', + 'exec/timeseries_write.cpp', 'exec/trial_period_utils.cpp', 'exec/trial_stage.cpp', 'exec/unpack_timeseries_bucket.cpp', diff --git a/src/mongo/db/exec/plan_stats.h b/src/mongo/db/exec/plan_stats.h index 4801769c441..de30eef1a55 100644 --- a/src/mongo/db/exec/plan_stats.h +++ b/src/mongo/db/exec/plan_stats.h @@ -1161,6 +1161,26 @@ struct UnpackTimeseriesBucketStats final : public SpecificStats { size_t nBucketsUnpacked = 0u; }; +struct TimeseriesWriteStats final : public SpecificStats { + std::unique_ptr clone() const final { + return std::make_unique(*this); + } + + uint64_t estimateObjectSizeInBytes() const { + return sizeof(*this); + } + + void acceptVisitor(PlanStatsConstVisitor* visitor) const final { + visitor->visit(this); + } + + void acceptVisitor(PlanStatsMutableVisitor* visitor) final { + visitor->visit(this); + } + + size_t measurementsDeleted = 0u; +}; + struct SampleFromTimeseriesBucketStats final : public SpecificStats { std::unique_ptr clone() const final { return std::make_unique(*this); diff --git a/src/mongo/db/exec/plan_stats_visitor.h b/src/mongo/db/exec/plan_stats_visitor.h index 175bbf1e91b..953eaa85921 100644 --- a/src/mongo/db/exec/plan_stats_visitor.h +++ b/src/mongo/db/exec/plan_stats_visitor.h @@ -80,6 +80,7 @@ struct DocumentSourceLookupStats; struct UnionWithStats; struct DocumentSourceFacetStats; struct UnpackTimeseriesBucketStats; +struct TimeseriesWriteStats; struct SampleFromTimeseriesBucketStats; /** @@ -141,6 +142,7 @@ public: virtual void visit(tree_walker::MaybeConstPtr stats) = 0; virtual void visit(tree_walker::MaybeConstPtr stats) = 0; virtual void visit(tree_walker::MaybeConstPtr stats) = 0; + virtual void visit(tree_walker::MaybeConstPtr stats) = 0; virtual void visit( tree_walker::MaybeConstPtr stats) = 0; }; @@ -196,6 +198,7 @@ struct PlanStatsVisitorBase : public PlanStatsVisitor { void visit(tree_walker::MaybeConstPtr stats) override {} void visit(tree_walker::MaybeConstPtr stats) override {} void visit(tree_walker::MaybeConstPtr stats) override {} + void visit(tree_walker::MaybeConstPtr stats) override {} void visit( tree_walker::MaybeConstPtr stats) override {} }; diff --git a/src/mongo/db/exec/timeseries_write.cpp b/src/mongo/db/exec/timeseries_write.cpp new file mode 100644 index 00000000000..e3d109866ef --- /dev/null +++ b/src/mongo/db/exec/timeseries_write.cpp @@ -0,0 +1,53 @@ +/** + * 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 + * . + * + * 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/exec/timeseries_write.h" + +namespace mongo { + +const char* TimeseriesWriteStage::kStageType = "TS_WRITE"; + +bool TimeseriesWriteStage::isEOF() { + return true; +} + +std::unique_ptr TimeseriesWriteStage::getStats() { + _commonStats.isEOF = isEOF(); + auto ret = std::make_unique(_commonStats, stageType()); + ret->specific = std::make_unique(_specificStats); + for (const auto& child : _children) { + ret->children.emplace_back(child->getStats()); + } + return ret; +} + +PlanStage::StageState TimeseriesWriteStage::doWork(WorkingSetID* out) { + return PlanStage::IS_EOF; +} +} // namespace mongo diff --git a/src/mongo/db/exec/timeseries_write.h b/src/mongo/db/exec/timeseries_write.h new file mode 100644 index 00000000000..f55770c2c1c --- /dev/null +++ b/src/mongo/db/exec/timeseries_write.h @@ -0,0 +1,83 @@ +/** + * 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 + * . + * + * 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 "mongo/db/exec/requires_collection_stage.h" + +namespace mongo { + +class TimeseriesWriteStage final : public RequiresMutableCollectionStage { +public: + static const char* kStageType; + + TimeseriesWriteStage(ExpressionContext* expCtx, + WorkingSet* ws, + std::unique_ptr child, + const CollectionPtr& coll, + std::unique_ptr residualPredicate, + const MatchExpression* originalBucketPredicate); + + StageType stageType() const { + return STAGE_TIMESERIES_WRITE; + } + + bool isEOF() final; + + std::unique_ptr getStats(); + + const SpecificStats* getSpecificStats() const { + return &_specificStats; + } + + PlanStage::StageState doWork(WorkingSetID* id); + +protected: + void doSaveStateRequiresCollection() final {} + + void doRestoreStateRequiresCollection() final {} + +private: + WorkingSet* _ws; + + // + // Main execution machinery data structures. + // + + // Determines the measurements to delete from this bucket, and by inverse, those to keep + // unmodified. + std::unique_ptr _residualPredicate; + + std::vector _unchangedMeasurements; + std::vector _deletedMeasurements; + + TimeseriesWriteStats _specificStats{}; +}; +} // namespace mongo diff --git a/src/mongo/db/query/classic_stage_builder.cpp b/src/mongo/db/query/classic_stage_builder.cpp index a11db550e04..2410384ebcf 100644 --- a/src/mongo/db/query/classic_stage_builder.cpp +++ b/src/mongo/db/query/classic_stage_builder.cpp @@ -421,6 +421,7 @@ std::unique_ptr ClassicStageBuilder::build(const QuerySolutionNode* r case STAGE_TRIAL: case STAGE_UNKNOWN: case STAGE_UNPACK_TIMESERIES_BUCKET: + case STAGE_TIMESERIES_WRITE: case STAGE_SENTINEL: case STAGE_COLUMN_SCAN: case STAGE_UPDATE: { diff --git a/src/mongo/db/query/plan_executor_impl.cpp b/src/mongo/db/query/plan_executor_impl.cpp index 677267fc8b7..cc97c25e7f3 100644 --- a/src/mongo/db/query/plan_executor_impl.cpp +++ b/src/mongo/db/query/plan_executor_impl.cpp @@ -611,6 +611,11 @@ long long PlanExecutorImpl::executeDelete() { const SpecificStats* stats = _root->child()->getSpecificStats(); return static_cast(stats)->docsDeleted; } + case StageType::STAGE_TIMESERIES_WRITE: { + const auto* tsWriteStats = + static_cast(_root->getSpecificStats()); + return tsWriteStats->measurementsDeleted; + } default: { invariant(StageType::STAGE_DELETE == _root->stageType() || StageType::STAGE_BATCHED_DELETE == _root->stageType()); diff --git a/src/mongo/db/query/stage_types.cpp b/src/mongo/db/query/stage_types.cpp index 3de3fa2086e..934fe3107ec 100644 --- a/src/mongo/db/query/stage_types.cpp +++ b/src/mongo/db/query/stage_types.cpp @@ -74,6 +74,7 @@ StringData stageTypeToString(StageType stageType) { {STAGE_SUBPLAN, "SUBPLAN"_sd}, {STAGE_TEXT_OR, "TEXT_OR"_sd}, {STAGE_TEXT_MATCH, "TEXT_MATCH"_sd}, + {STAGE_TIMESERIES_WRITE, "TIMESERIES_WRITE"_sd}, {STAGE_TRIAL, "TRIAL"_sd}, {STAGE_UNKNOWN, "UNKNOWN"_sd}, {STAGE_UNPACK_TIMESERIES_BUCKET, "UNPACK_TIMESERIES_BUCKET"_sd}, diff --git a/src/mongo/db/query/stage_types.h b/src/mongo/db/query/stage_types.h index f5ee3a70d4c..6398573f543 100644 --- a/src/mongo/db/query/stage_types.h +++ b/src/mongo/db/query/stage_types.h @@ -117,6 +117,9 @@ enum StageType { STAGE_TEXT_OR, STAGE_TEXT_MATCH, + // Stage for modifying bucket documents in a time-series bucket collection. + STAGE_TIMESERIES_WRITE, + // Stage for choosing between two alternate plans based on an initial trial period. STAGE_TRIAL, -- cgit v1.2.1