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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
/**
* Copyright (C) 2013 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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/platform/basic.h"
#include "mongo/db/query/plan_executor.h"
#include "mongo/bson/simple_bsonobj_comparator.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/curop.h"
#include "mongo/db/exec/cached_plan.h"
#include "mongo/db/exec/multi_plan.h"
#include "mongo/db/exec/pipeline_proxy.h"
#include "mongo/db/exec/plan_stage.h"
#include "mongo/db/exec/plan_stats.h"
#include "mongo/db/exec/subplan.h"
#include "mongo/db/exec/working_set.h"
#include "mongo/db/exec/working_set_common.h"
#include "mongo/db/query/plan_yield_policy.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/record_fetcher.h"
#include "mongo/stdx/memory.h"
#include "mongo/util/fail_point_service.h"
#include "mongo/util/stacktrace.h"
namespace mongo {
using std::shared_ptr;
using std::string;
using std::unique_ptr;
using std::vector;
using stdx::make_unique;
namespace {
namespace {
MONGO_FP_DECLARE(planExecutorAlwaysDead);
} // namespace
/**
* Retrieves the first stage of a given type from the plan tree, or NULL
* if no such stage is found.
*/
PlanStage* getStageByType(PlanStage* root, StageType type) {
if (root->stageType() == type) {
return root;
}
const auto& children = root->getChildren();
for (size_t i = 0; i < children.size(); i++) {
PlanStage* result = getStageByType(children[i].get(), type);
if (result) {
return result;
}
}
return NULL;
}
}
// static
StatusWith<unique_ptr<PlanExecutor>> PlanExecutor::make(OperationContext* opCtx,
unique_ptr<WorkingSet> ws,
unique_ptr<PlanStage> rt,
const Collection* collection,
YieldPolicy yieldPolicy) {
return PlanExecutor::make(
opCtx, std::move(ws), std::move(rt), nullptr, nullptr, collection, "", yieldPolicy);
}
// static
StatusWith<unique_ptr<PlanExecutor>> PlanExecutor::make(OperationContext* opCtx,
unique_ptr<WorkingSet> ws,
unique_ptr<PlanStage> rt,
const string& ns,
YieldPolicy yieldPolicy) {
return PlanExecutor::make(
opCtx, std::move(ws), std::move(rt), nullptr, nullptr, nullptr, ns, yieldPolicy);
}
// static
StatusWith<unique_ptr<PlanExecutor>> PlanExecutor::make(OperationContext* opCtx,
unique_ptr<WorkingSet> ws,
unique_ptr<PlanStage> rt,
unique_ptr<CanonicalQuery> cq,
const Collection* collection,
YieldPolicy yieldPolicy) {
return PlanExecutor::make(
opCtx, std::move(ws), std::move(rt), nullptr, std::move(cq), collection, "", yieldPolicy);
}
// static
StatusWith<unique_ptr<PlanExecutor>> PlanExecutor::make(OperationContext* opCtx,
unique_ptr<WorkingSet> ws,
unique_ptr<PlanStage> rt,
unique_ptr<QuerySolution> qs,
unique_ptr<CanonicalQuery> cq,
const Collection* collection,
YieldPolicy yieldPolicy) {
return PlanExecutor::make(opCtx,
std::move(ws),
std::move(rt),
std::move(qs),
std::move(cq),
collection,
"",
yieldPolicy);
}
// static
StatusWith<unique_ptr<PlanExecutor>> PlanExecutor::make(OperationContext* txn,
unique_ptr<WorkingSet> ws,
unique_ptr<PlanStage> rt,
unique_ptr<QuerySolution> qs,
unique_ptr<CanonicalQuery> cq,
const Collection* collection,
const string& ns,
YieldPolicy yieldPolicy) {
unique_ptr<PlanExecutor> exec(new PlanExecutor(
txn, std::move(ws), std::move(rt), std::move(qs), std::move(cq), collection, ns));
// Perform plan selection, if necessary.
Status status = exec->pickBestPlan(yieldPolicy, collection);
if (!status.isOK()) {
return status;
}
return std::move(exec);
}
PlanExecutor::PlanExecutor(OperationContext* opCtx,
unique_ptr<WorkingSet> ws,
unique_ptr<PlanStage> rt,
unique_ptr<QuerySolution> qs,
unique_ptr<CanonicalQuery> cq,
const Collection* collection,
const string& ns)
: _opCtx(opCtx),
_cq(std::move(cq)),
_workingSet(std::move(ws)),
_qs(std::move(qs)),
_root(std::move(rt)),
_ns(ns),
_yieldPolicy(new PlanYieldPolicy(this, YIELD_MANUAL)) {
// We may still need to initialize _ns from either collection or _cq.
if (!_ns.empty()) {
// We already have an _ns set, so there's nothing more to do.
return;
}
if (collection) {
_ns = collection->ns().ns();
} else {
invariant(_cq);
_ns = _cq->getQueryRequest().ns();
}
}
Status PlanExecutor::pickBestPlan(YieldPolicy policy, const Collection* collection) {
invariant(_currentState == kUsable);
// For YIELD_AUTO, this will both set an auto yield policy on the PlanExecutor and
// register it to receive notifications.
this->setYieldPolicy(policy, collection);
// First check if we need to do subplanning.
PlanStage* foundStage = getStageByType(_root.get(), STAGE_SUBPLAN);
if (foundStage) {
SubplanStage* subplan = static_cast<SubplanStage*>(foundStage);
return subplan->pickBestPlan(_yieldPolicy.get());
}
// If we didn't have to do subplanning, we might still have to do regular
// multi plan selection...
foundStage = getStageByType(_root.get(), STAGE_MULTI_PLAN);
if (foundStage) {
MultiPlanStage* mps = static_cast<MultiPlanStage*>(foundStage);
return mps->pickBestPlan(_yieldPolicy.get());
}
// ...or, we might have to run a plan from the cache for a trial period, falling back on
// regular planning if the cached plan performs poorly.
foundStage = getStageByType(_root.get(), STAGE_CACHED_PLAN);
if (foundStage) {
CachedPlanStage* cachedPlan = static_cast<CachedPlanStage*>(foundStage);
return cachedPlan->pickBestPlan(_yieldPolicy.get());
}
// Either we chose a plan, or no plan selection was required. In both cases,
// our work has been successfully completed.
return Status::OK();
}
PlanExecutor::~PlanExecutor() {}
// static
string PlanExecutor::statestr(ExecState s) {
if (PlanExecutor::ADVANCED == s) {
return "ADVANCED";
} else if (PlanExecutor::IS_EOF == s) {
return "IS_EOF";
} else if (PlanExecutor::DEAD == s) {
return "DEAD";
} else {
verify(PlanExecutor::FAILURE == s);
return "FAILURE";
}
}
WorkingSet* PlanExecutor::getWorkingSet() const {
return _workingSet.get();
}
PlanStage* PlanExecutor::getRootStage() const {
return _root.get();
}
CanonicalQuery* PlanExecutor::getCanonicalQuery() const {
return _cq.get();
}
unique_ptr<PlanStageStats> PlanExecutor::getStats() const {
return _root->getStats();
}
BSONObjSet PlanExecutor::getOutputSorts() const {
if (_qs && _qs->root) {
_qs->root->computeProperties();
return _qs->root->getSort();
}
if (_root->stageType() == STAGE_MULTI_PLAN) {
// If we needed a MultiPlanStage, the PlanExecutor does not own the QuerySolution. We
// must go through the MultiPlanStage to access the output sort.
auto multiPlanStage = static_cast<MultiPlanStage*>(_root.get());
if (multiPlanStage->bestSolution()) {
multiPlanStage->bestSolution()->root->computeProperties();
return multiPlanStage->bestSolution()->root->getSort();
}
} else if (_root->stageType() == STAGE_SUBPLAN) {
auto subplanStage = static_cast<SubplanStage*>(_root.get());
if (subplanStage->compositeSolution()) {
subplanStage->compositeSolution()->root->computeProperties();
return subplanStage->compositeSolution()->root->getSort();
}
}
return SimpleBSONObjComparator::kInstance.makeBSONObjSet();
}
OperationContext* PlanExecutor::getOpCtx() const {
return _opCtx;
}
void PlanExecutor::saveState() {
invariant(_currentState == kUsable || _currentState == kSaved);
// The query stages inside this stage tree might buffer record ids (e.g. text, geoNear,
// mergeSort, sort) which are no longer protected by the storage engine's transactional
// boundaries.
WorkingSetCommon::prepareForSnapshotChange(_workingSet.get());
if (!killed()) {
_root->saveState();
}
_currentState = kSaved;
}
bool PlanExecutor::restoreState() {
try {
return restoreStateWithoutRetrying();
} catch (const WriteConflictException& wce) {
if (!_yieldPolicy->allowedToYield())
throw;
// Handles retries by calling restoreStateWithoutRetrying() in a loop.
return _yieldPolicy->yield();
}
}
bool PlanExecutor::restoreStateWithoutRetrying() {
invariant(_currentState == kSaved);
if (!killed()) {
_root->restoreState();
}
_currentState = kUsable;
return !killed();
}
void PlanExecutor::detachFromOperationContext() {
invariant(_currentState == kSaved);
_opCtx = nullptr;
_root->detachFromOperationContext();
_currentState = kDetached;
_everDetachedFromOperationContext = true;
}
void PlanExecutor::reattachToOperationContext(OperationContext* txn) {
invariant(_currentState == kDetached);
// We're reattaching for a getMore now. Reset the yield timer in order to prevent from
// yielding again right away.
_yieldPolicy->resetTimer();
_opCtx = txn;
_root->reattachToOperationContext(txn);
_currentState = kSaved;
}
void PlanExecutor::invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type) {
if (!killed()) {
_root->invalidate(txn, dl, type);
}
}
PlanExecutor::ExecState PlanExecutor::getNext(BSONObj* objOut, RecordId* dlOut) {
Snapshotted<BSONObj> snapshotted;
ExecState state = getNextImpl(objOut ? &snapshotted : NULL, dlOut);
if (objOut) {
*objOut = snapshotted.value();
}
return state;
}
PlanExecutor::ExecState PlanExecutor::getNextSnapshotted(Snapshotted<BSONObj>* objOut,
RecordId* dlOut) {
// Detaching from the OperationContext means that the returned snapshot ids could be invalid.
invariant(!_everDetachedFromOperationContext);
return getNextImpl(objOut, dlOut);
}
PlanExecutor::ExecState PlanExecutor::getNextImpl(Snapshotted<BSONObj>* objOut, RecordId* dlOut) {
MONGO_FAIL_POINT_BLOCK(planExecutorAlwaysDead, customKill) {
const BSONObj& data = customKill.getData();
BSONElement customKillNS = data["namespace"];
if (!customKillNS || _ns == customKillNS.str()) {
deregisterExec();
kill("hit planExecutorAlwaysDead fail point");
}
}
invariant(_currentState == kUsable);
if (killed()) {
if (NULL != objOut) {
Status status(ErrorCodes::OperationFailed,
str::stream() << "Operation aborted because: " << *_killReason);
*objOut = Snapshotted<BSONObj>(SnapshotId(),
WorkingSetCommon::buildMemberStatusObject(status));
}
return PlanExecutor::DEAD;
}
if (!_stash.empty()) {
invariant(objOut && !dlOut);
*objOut = {SnapshotId(), _stash.front()};
_stash.pop();
return PlanExecutor::ADVANCED;
}
// When a stage requests a yield for document fetch, it gives us back a RecordFetcher*
// to use to pull the record into memory. We take ownership of the RecordFetcher here,
// deleting it after we've had a chance to do the fetch. For timing-based yields, we
// just pass a NULL fetcher.
unique_ptr<RecordFetcher> fetcher;
// Incremented on every writeConflict, reset to 0 on any successful call to _root->work.
size_t writeConflictsInARow = 0;
for (;;) {
// These are the conditions which can cause us to yield:
// 1) The yield policy's timer elapsed, or
// 2) some stage requested a yield due to a document fetch, or
// 3) we need to yield and retry due to a WriteConflictException.
// In all cases, the actual yielding happens here.
if (_yieldPolicy->shouldYield()) {
if (!_yieldPolicy->yield(fetcher.get())) {
// A return of false from a yield should only happen if we've been killed during the
// yield.
invariant(killed());
if (NULL != objOut) {
Status status(ErrorCodes::OperationFailed,
str::stream() << "Operation aborted because: " << *_killReason);
*objOut = Snapshotted<BSONObj>(
SnapshotId(), WorkingSetCommon::buildMemberStatusObject(status));
}
return PlanExecutor::DEAD;
}
}
// We're done using the fetcher, so it should be freed. We don't want to
// use the same RecordFetcher twice.
fetcher.reset();
WorkingSetID id = WorkingSet::INVALID_ID;
PlanStage::StageState code = _root->work(&id);
if (code != PlanStage::NEED_YIELD)
writeConflictsInARow = 0;
if (PlanStage::ADVANCED == code) {
WorkingSetMember* member = _workingSet->get(id);
bool hasRequestedData = true;
if (NULL != objOut) {
if (WorkingSetMember::RID_AND_IDX == member->getState()) {
if (1 != member->keyData.size()) {
_workingSet->free(id);
hasRequestedData = false;
} else {
// TODO: currently snapshot ids are only associated with documents, and
// not with index keys.
*objOut = Snapshotted<BSONObj>(SnapshotId(), member->keyData[0].keyData);
}
} else if (member->hasObj()) {
*objOut = member->obj;
} else {
_workingSet->free(id);
hasRequestedData = false;
}
}
if (NULL != dlOut) {
if (member->hasRecordId()) {
*dlOut = member->recordId;
} else {
_workingSet->free(id);
hasRequestedData = false;
}
}
if (hasRequestedData) {
_workingSet->free(id);
return PlanExecutor::ADVANCED;
}
// This result didn't have the data the caller wanted, try again.
} else if (PlanStage::NEED_YIELD == code) {
if (id == WorkingSet::INVALID_ID) {
if (!_yieldPolicy->allowedToYield())
throw WriteConflictException();
CurOp::get(_opCtx)->debug().writeConflicts++;
writeConflictsInARow++;
WriteConflictException::logAndBackoff(writeConflictsInARow, "plan execution", _ns);
} else {
WorkingSetMember* member = _workingSet->get(id);
invariant(member->hasFetcher());
// Transfer ownership of the fetcher. Next time around the loop a yield will
// happen.
fetcher.reset(member->releaseFetcher());
}
// If we're allowed to, we will yield next time through the loop.
if (_yieldPolicy->allowedToYield())
_yieldPolicy->forceYield();
} else if (PlanStage::NEED_TIME == code) {
// Fall through to yield check at end of large conditional.
} else if (PlanStage::IS_EOF == code) {
return PlanExecutor::IS_EOF;
} else {
invariant(PlanStage::DEAD == code || PlanStage::FAILURE == code);
if (NULL != objOut) {
BSONObj statusObj;
WorkingSetCommon::getStatusMemberObject(*_workingSet, id, &statusObj);
*objOut = Snapshotted<BSONObj>(SnapshotId(), statusObj);
}
return (PlanStage::DEAD == code) ? PlanExecutor::DEAD : PlanExecutor::FAILURE;
}
}
}
bool PlanExecutor::isEOF() {
invariant(_currentState == kUsable);
return killed() || (_stash.empty() && _root->isEOF());
}
void PlanExecutor::registerExec(const Collection* collection) {
// There's no need to register a PlanExecutor for which the underlying collection
// doesn't exist.
if (collection) {
_safety.reset(new ScopedExecutorRegistration(this, collection));
}
}
void PlanExecutor::deregisterExec() {
_safety.reset();
}
void PlanExecutor::kill(string reason) {
_killReason = std::move(reason);
}
Status PlanExecutor::executePlan() {
invariant(_currentState == kUsable);
BSONObj obj;
PlanExecutor::ExecState state = PlanExecutor::ADVANCED;
while (PlanExecutor::ADVANCED == state) {
state = this->getNext(&obj, NULL);
}
if (PlanExecutor::DEAD == state || PlanExecutor::FAILURE == state) {
return Status(ErrorCodes::OperationFailed,
str::stream() << "Exec error: " << WorkingSetCommon::toStatusString(obj)
<< ", state: "
<< PlanExecutor::statestr(state));
}
invariant(PlanExecutor::IS_EOF == state);
return Status::OK();
}
const string& PlanExecutor::ns() {
return _ns;
}
void PlanExecutor::setYieldPolicy(YieldPolicy policy,
const Collection* collection,
bool registerExecutor) {
if (!collection) {
// If the collection doesn't exist, then there's no need to yield at all.
invariant(!_yieldPolicy->allowedToYield());
return;
}
_yieldPolicy->setPolicy(policy);
if (PlanExecutor::YIELD_AUTO == policy) {
// Runners that yield automatically generally need to be registered so that
// after yielding, they receive notifications of events like deletions and
// index drops. The only exception is that a few PlanExecutors get registered
// by ClientCursor instead of being registered here. This is unneeded if we only do
// partial "yields" for WriteConflict retrying.
if (registerExecutor) {
this->registerExec(collection);
}
}
}
void PlanExecutor::enqueue(const BSONObj& obj) {
_stash.push(obj.getOwned());
}
//
// ScopedExecutorRegistration
//
// PlanExecutor::ScopedExecutorRegistration
PlanExecutor::ScopedExecutorRegistration::ScopedExecutorRegistration(PlanExecutor* exec,
const Collection* collection)
: _exec(exec), _collection(collection) {
invariant(_collection);
_collection->getCursorManager()->registerExecutor(_exec);
}
PlanExecutor::ScopedExecutorRegistration::~ScopedExecutorRegistration() {
if (_exec->killed()) {
// If the plan executor has been killed, then it's possible that the collection
// no longer exists.
return;
}
_collection->getCursorManager()->deregisterExecutor(_exec);
}
} // namespace mongo
|