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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/**
* 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 <boost/functional/hash.hpp>
#include "mongo/db/exec/sbe/stages/stages.h"
#include "mongo/db/hasher.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/query/plan_cache.h"
#include "mongo/db/query/plan_cache_key_info.h"
#include "mongo/db/query/sbe_stage_builder.h"
#include "mongo/db/service_context.h"
namespace mongo {
namespace sbe {
/**
* Represents the sharding epoch of the collection which entry will be stored in the plan cache. The
* sharding epoch is not updated on operations like chunk splits and moves but rather on sharding
* and refine shard key operations.
*/
struct PlanCacheKeyShardingEpoch {
bool operator==(const PlanCacheKeyShardingEpoch& other) const {
return epoch == other.epoch && ts == other.ts;
}
OID epoch;
Timestamp ts;
};
/**
* Represents the "key" used in the PlanCache mapping from query shape -> query plan.
*/
class PlanCacheKey {
public:
PlanCacheKey(PlanCacheKeyInfo&& info,
UUID collectionUuid,
size_t collectionVersion,
boost::optional<Timestamp> newestVisibleIndexTimestamp,
boost::optional<PlanCacheKeyShardingEpoch> shardVersion)
: _info{std::move(info)},
_collectionUuid{collectionUuid},
_collectionVersion{collectionVersion},
_newestVisibleIndexTimestamp{newestVisibleIndexTimestamp},
_shardVersion{shardVersion} {}
const UUID& getCollectionUuid() const {
return _collectionUuid;
}
size_t getCollectionVersion() const {
return _collectionVersion;
}
bool operator==(const PlanCacheKey& other) const {
return other._collectionVersion == _collectionVersion &&
other._collectionUuid == _collectionUuid &&
other._newestVisibleIndexTimestamp == _newestVisibleIndexTimestamp &&
other._info == _info && other._shardVersion == _shardVersion;
}
bool operator!=(const PlanCacheKey& other) const {
return !(*this == other);
}
uint32_t queryHash() const {
return _info.queryHash();
}
uint32_t planCacheKeyHash() const {
size_t hash = _info.planCacheKeyHash();
boost::hash_combine(hash, UUID::Hash{}(_collectionUuid));
boost::hash_combine(hash, _collectionVersion);
if (_newestVisibleIndexTimestamp) {
boost::hash_combine(hash, _newestVisibleIndexTimestamp->asULL());
}
if (_shardVersion) {
_shardVersion->epoch.hash_combine(hash);
boost::hash_combine(hash, _shardVersion->ts.asULL());
}
return hash;
}
const std::string& toString() const {
return _info.toString();
}
private:
// Contains the actual encoding of the query shape as well as the index discriminators.
const PlanCacheKeyInfo _info;
const UUID _collectionUuid;
// There is a special collection versioning scheme associated with the SBE plan cache. Whenever
// an action against a collection is made which should invalidate the plan cache entries for the
// collection -- in particular index builds and drops -- the version number is incremented.
// Readers specify the version number that they are reading at so that they only pick up cache
// entries with the right set of indexes.
//
// We also clean up all cache entries for a particular (collectionUuid, versionNumber) pair when
// all readers seeing this version of the collection have drained.
const size_t _collectionVersion;
// The '_collectionVersion' is not currently sufficient in order to ensure that the indexes
// visible to the reader are consistent with the indexes present in the cache entry. The reason
// is that all readers see the latest copy-on-write version of the 'Collection' object, even
// though they are allowed to read at an older timestamp, potentially at a time before an index
// build completed.
//
// To solve this problem, we incorporate the timestamp of the newest index visible to the reader
// into the plan cache key. This ensures that the set of indexes visible to the reader match
// those present in the plan cache entry, preventing a situation where the plan cache entry
// reflects a newer version of the index catalog than the one visible to the reader.
//
// In the future, this could instead be solved with point-in-time catalog lookups.
const boost::optional<Timestamp> _newestVisibleIndexTimestamp;
// Ensures that a cached SBE plan cannot be reused if the collection has since become sharded or
// changed its shard key. The cached plan may no longer be valid after sharding or shard key
// refining since the structure of the plan depends on whether the collection is sharded, and if
// sharded depends on the shard key.
const boost::optional<PlanCacheKeyShardingEpoch> _shardVersion;
};
class PlanCacheKeyHasher {
public:
std::size_t operator()(const PlanCacheKey& k) const {
return k.planCacheKeyHash();
}
};
struct PlanCachePartitioner {
// Determines the partitioning function for use with the 'Partitioned' utility.
std::size_t operator()(const PlanCacheKey& k, const std::size_t nPartitions) const {
return PlanCacheKeyHasher{}(k) % nPartitions;
}
};
/**
* Represents the data cached in the SBE plan cache. This data holds an execution plan and necessary
* auxiliary data for preparing and executing the PlanStage tree.
*/
struct CachedSbePlan {
CachedSbePlan(std::unique_ptr<sbe::PlanStage> root, stage_builder::PlanStageData data)
: root(std::move(root)), planStageData(std::move(data)) {
tassert(5968206, "The RuntimeEnvironment should not be null", planStageData.env);
}
std::unique_ptr<CachedSbePlan> clone() const {
return std::make_unique<CachedSbePlan>(root->clone(), planStageData);
}
uint64_t estimateObjectSizeInBytes() const {
return root->estimateCompileTimeSize();
}
std::unique_ptr<sbe::PlanStage> root;
stage_builder::PlanStageData planStageData;
bool indexFilterApplied = false;
};
using PlanCacheEntry = PlanCacheEntryBase<CachedSbePlan, plan_cache_debug_info::DebugInfoSBE>;
struct BudgetEstimator {
size_t operator()(const std::shared_ptr<const PlanCacheEntry>& entry) {
return entry->estimatedEntrySizeBytes;
}
};
using PlanCache = PlanCacheBase<PlanCacheKey,
CachedSbePlan,
BudgetEstimator,
plan_cache_debug_info::DebugInfoSBE,
PlanCachePartitioner,
PlanCacheKeyHasher>;
/**
* A helper method to get the global SBE plan cache decorated in 'serviceCtx'.
*/
PlanCache& getPlanCache(ServiceContext* serviceCtx);
/**
* A wrapper for the helper above. 'opCtx' cannot be null.
*/
PlanCache& getPlanCache(OperationContext* opCtx);
/**
* Removes cached plan entries with the given collection UUID and collection version number.
*/
void clearPlanCacheEntriesWith(ServiceContext* serviceCtx,
UUID collectionUuid,
size_t collectionVersion);
} // namespace sbe
} // namespace mongo
|