summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/sbe_cached_solution_planner.cpp
blob: abc3027ce354e6776c0888e5a97a3cc8a8f05504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 *    Copyright (C) 2020-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/platform/basic.h"

#include "mongo/db/query/sbe_cached_solution_planner.h"

#include "mongo/db/query/collection_query_info.h"
#include "mongo/db/query/explain.h"
#include "mongo/db/query/query_planner.h"
#include "mongo/db/query/sbe_multi_planner.h"
#include "mongo/db/query/stage_builder_util.h"
#include "mongo/logv2/log.h"

namespace mongo::sbe {
plan_ranker::CandidatePlan CachedSolutionPlanner::plan(
    std::vector<std::unique_ptr<QuerySolution>> solutions,
    std::vector<std::pair<std::unique_ptr<PlanStage>, stage_builder::PlanStageData>> roots) {
    invariant(solutions.size() == 1);
    invariant(solutions.size() == roots.size());

    auto candidate = [&]() {
        auto candidates = collectExecutionStats(std::move(solutions), std::move(roots));
        invariant(candidates.size() == 1);
        return std::move(candidates[0]);
    }();

    if (candidate.failed) {
        // On failure, fall back to replanning the whole query. We neither evict the existing cache
        // entry, nor cache the result of replanning.
        LOGV2_DEBUG(2057901,
                    1,
                    "Execution of cached plan failed, falling back to replan",
                    "query"_attr = redact(_cq.toStringShort()),
                    "planSummary"_attr = Explain::getPlanSummary(candidate.root.get()));
        return replan(false);
    }

    auto stats{candidate.root->getStats()};
    auto numReads{calculateNumberOfReads(stats.get())};
    // If the cached plan hit EOF quickly enough, or still as efficient as before, then no need to
    // replan. Finalize the cached plan and return it.
    if (stats->common.isEOF || numReads <= _decisionReads) {
        return finalizeExecutionPlan(std::move(stats), std::move(candidate));
    }

    // If we're here, the trial period took more than 'maxReadsBeforeReplan' physical reads. This
    // plan may not be efficient any longer, so we replan from scratch.
    LOGV2_DEBUG(
        2058001,
        1,
        "Evicting cache entry for a query and replanning it since the number of required works "
        "mismatch the number of cached works",
        "maxReadsBeforeReplan"_attr = numReads,
        "decisionReads"_attr = _decisionReads,
        "query"_attr = redact(_cq.toStringShort()),
        "planSummary"_attr = Explain::getPlanSummary(candidate.root.get()));
    return replan(true);
}

plan_ranker::CandidatePlan CachedSolutionPlanner::finalizeExecutionPlan(
    std::unique_ptr<PlanStageStats> stats, plan_ranker::CandidatePlan candidate) const {
    // If the winning stage has exited early, clear the results queue and reopen the plan stage
    // tree, as we cannot resume such execution tree from where the trial run has stopped, and, as
    // a result, we cannot stash the results returned so far in the plan executor.
    if (!stats->common.isEOF && candidate.exitedEarly) {
        candidate.root->close();
        candidate.root->open(true);
        // Clear the results queue.
        candidate.results = decltype(candidate.results){};
    }

    return candidate;
}

plan_ranker::CandidatePlan CachedSolutionPlanner::replan(bool shouldCache) const {
    if (shouldCache) {
        // Deactivate the current cache entry.
        auto cache = CollectionQueryInfo::get(_collection).getPlanCache();
        cache->deactivate(_cq);
    }

    // Use the query planning module to plan the whole query.
    auto solutions = uassertStatusOK(QueryPlanner::plan(_cq, _queryParams));
    if (solutions.size() == 1) {
        // Only one possible plan. Build the stages from the solution.
        auto&& [root, data] = stage_builder::buildSlotBasedExecutableTree(
            _opCtx, _collection, _cq, *solutions[0], _yieldPolicy, true);
        prepareExecutionPlan(root.get(), &data);
        LOGV2_DEBUG(
            2058101,
            1,
            "Replanning of query resulted in a single query solution, which will not be cached. ",
            "query"_attr = redact(_cq.toStringShort()),
            "planSummary"_attr = Explain::getPlanSummary(root.get()),
            "shouldCache"_attr = (shouldCache ? "yes" : "no"));
        return {std::move(solutions[0]), std::move(root), std::move(data)};
    }

    // Many solutions. Build a plan stage tree for each solution and create a multi planner to pick
    // the best, update the cache, and so on.
    std::vector<std::pair<std::unique_ptr<PlanStage>, stage_builder::PlanStageData>> roots;
    for (auto&& solution : solutions) {
        if (solution->cacheData.get()) {
            solution->cacheData->indexFilterApplied = _queryParams.indexFiltersApplied;
        }

        roots.push_back(stage_builder::buildSlotBasedExecutableTree(
            _opCtx, _collection, _cq, *solution, _yieldPolicy, true));
    }

    const auto cachingMode =
        shouldCache ? PlanCachingMode::AlwaysCache : PlanCachingMode::NeverCache;
    MultiPlanner multiPlanner{_opCtx, _collection, _cq, cachingMode, _yieldPolicy};
    auto plan = multiPlanner.plan(std::move(solutions), std::move(roots));
    LOGV2_DEBUG(2058201,
                1,
                "Query plan after replanning and its cache status",
                "query"_attr = redact(_cq.toStringShort()),
                "planSummary"_attr = Explain::getPlanSummary(plan.root.get()),
                "shouldCache"_attr = (shouldCache ? "yes" : "no"));
    return plan;
}
}  // namespace mongo::sbe