Skip to content

Commit be8a683

Browse files
committed
SERVER-19364 move query stage OperationContext pointer management into the base class
1 parent 84182ff commit be8a683

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+380
-438
lines changed

src/mongo/db/commands/list_collections.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@
4545
#include "mongo/db/query/find_constants.h"
4646
#include "mongo/db/service_context.h"
4747
#include "mongo/db/storage/storage_engine.h"
48+
#include "mongo/stdx/memory.h"
4849

4950
namespace mongo {
5051

51-
using std::unique_ptr;
5252
using std::list;
5353
using std::string;
5454
using std::stringstream;
55+
using std::unique_ptr;
56+
using stdx::make_unique;
5557

5658
class CmdListCollections : public Command {
5759
public:
@@ -99,7 +101,7 @@ class CmdListCollections : public Command {
99101
int,
100102
string& errmsg,
101103
BSONObjBuilder& result) {
102-
std::unique_ptr<MatchExpression> matcher;
104+
unique_ptr<MatchExpression> matcher;
103105
BSONElement filterElt = jsobj["filter"];
104106
if (!filterElt.eoo()) {
105107
if (filterElt.type() != mongo::Object) {
@@ -134,8 +136,8 @@ class CmdListCollections : public Command {
134136
names.sort();
135137
}
136138

137-
std::unique_ptr<WorkingSet> ws(new WorkingSet());
138-
std::unique_ptr<QueuedDataStage> root(new QueuedDataStage(ws.get()));
139+
auto ws = make_unique<WorkingSet>();
140+
auto root = make_unique<QueuedDataStage>(txn, ws.get());
139141

140142
for (std::list<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
141143
const std::string& ns = *i;
@@ -175,7 +177,7 @@ class CmdListCollections : public Command {
175177
if (!statusWithPlanExecutor.isOK()) {
176178
return appendCommandStatus(result, statusWithPlanExecutor.getStatus());
177179
}
178-
std::unique_ptr<PlanExecutor> exec = std::move(statusWithPlanExecutor.getValue());
180+
unique_ptr<PlanExecutor> exec = std::move(statusWithPlanExecutor.getValue());
179181

180182
BSONArrayBuilder firstBatch;
181183

src/mongo/db/commands/list_indexes.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@
4545
#include "mongo/db/query/find_constants.h"
4646
#include "mongo/db/service_context.h"
4747
#include "mongo/db/storage/storage_engine.h"
48+
#include "mongo/stdx/memory.h"
4849

4950
namespace mongo {
5051

5152
using std::string;
5253
using std::stringstream;
54+
using std::unique_ptr;
5355
using std::vector;
56+
using stdx::make_unique;
5457

5558
/**
5659
* Lists the indexes for a given collection.
@@ -143,8 +146,8 @@ class CmdListIndexes : public Command {
143146
}
144147
MONGO_WRITE_CONFLICT_RETRY_LOOP_END(txn, "listIndexes", ns.ns());
145148

146-
std::unique_ptr<WorkingSet> ws(new WorkingSet());
147-
std::unique_ptr<QueuedDataStage> root(new QueuedDataStage(ws.get()));
149+
auto ws = make_unique<WorkingSet>();
150+
auto root = make_unique<QueuedDataStage>(txn, ws.get());
148151

149152
for (size_t i = 0; i < indexNames.size(); i++) {
150153
BSONObj indexSpec;
@@ -173,7 +176,7 @@ class CmdListIndexes : public Command {
173176
if (!statusWithPlanExecutor.isOK()) {
174177
return appendCommandStatus(result, statusWithPlanExecutor.getStatus());
175178
}
176-
std::unique_ptr<PlanExecutor> exec = std::move(statusWithPlanExecutor.getValue());
179+
unique_ptr<PlanExecutor> exec = std::move(statusWithPlanExecutor.getValue());
177180

178181
BSONArrayBuilder firstBatch;
179182

src/mongo/db/commands/pipeline_command.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "mongo/db/query/find_constants.h"
5252
#include "mongo/db/query/get_executor.h"
5353
#include "mongo/db/storage_options.h"
54+
#include "mongo/stdx/memory.h"
5455

5556
namespace mongo {
5657

@@ -60,6 +61,7 @@ using std::shared_ptr;
6061
using std::string;
6162
using std::stringstream;
6263
using std::unique_ptr;
64+
using stdx::make_unique;
6365

6466
/**
6567
* Returns true if we need to keep a ClientCursor saved for this pipeline (for future getMore
@@ -230,9 +232,8 @@ class PipelineCommand : public Command {
230232
// Create the PlanExecutor which returns results from the pipeline. The WorkingSet
231233
// ('ws') and the PipelineProxyStage ('proxy') will be owned by the created
232234
// PlanExecutor.
233-
unique_ptr<WorkingSet> ws(new WorkingSet());
234-
unique_ptr<PipelineProxyStage> proxy(
235-
new PipelineProxyStage(pPipeline, input, ws.get()));
235+
auto ws = make_unique<WorkingSet>();
236+
auto proxy = make_unique<PipelineProxyStage>(txn, pPipeline, input, ws.get());
236237

237238
auto statusWithPlanExecutor = (NULL == collection)
238239
? PlanExecutor::make(

src/mongo/db/exec/and_hash.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,20 @@ const size_t AndHashStage::kLookAheadWorks = 10;
5454
// static
5555
const char* AndHashStage::kStageType = "AND_HASH";
5656

57-
AndHashStage::AndHashStage(WorkingSet* ws, const Collection* collection)
58-
: PlanStage(kStageType),
57+
AndHashStage::AndHashStage(OperationContext* opCtx, WorkingSet* ws, const Collection* collection)
58+
: PlanStage(kStageType, opCtx),
5959
_collection(collection),
6060
_ws(ws),
6161
_hashingChildren(true),
6262
_currentChild(0),
6363
_memUsage(0),
6464
_maxMemUsage(kDefaultMaxMemUsageBytes) {}
6565

66-
AndHashStage::AndHashStage(WorkingSet* ws, const Collection* collection, size_t maxMemUsage)
67-
: PlanStage(kStageType),
66+
AndHashStage::AndHashStage(OperationContext* opCtx,
67+
WorkingSet* ws,
68+
const Collection* collection,
69+
size_t maxMemUsage)
70+
: PlanStage(kStageType, opCtx),
6871
_collection(collection),
6972
_ws(ws),
7073
_hashingChildren(true),

src/mongo/db/exec/and_hash.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ namespace mongo {
5151
*/
5252
class AndHashStage final : public PlanStage {
5353
public:
54-
AndHashStage(WorkingSet* ws, const Collection* collection);
54+
AndHashStage(OperationContext* opCtx, WorkingSet* ws, const Collection* collection);
5555

5656
/**
5757
* For testing only. Allows tests to set memory usage threshold.
5858
*/
59-
AndHashStage(WorkingSet* ws, const Collection* collection, size_t maxMemUsage);
59+
AndHashStage(OperationContext* opCtx,
60+
WorkingSet* ws,
61+
const Collection* collection,
62+
size_t maxMemUsage);
6063

6164
void addChild(PlanStage* child);
6265

src/mongo/db/exec/and_sorted.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ using stdx::make_unique;
4444
// static
4545
const char* AndSortedStage::kStageType = "AND_SORTED";
4646

47-
AndSortedStage::AndSortedStage(WorkingSet* ws, const Collection* collection)
48-
: PlanStage(kStageType),
47+
AndSortedStage::AndSortedStage(OperationContext* opCtx,
48+
WorkingSet* ws,
49+
const Collection* collection)
50+
: PlanStage(kStageType, opCtx),
4951
_collection(collection),
5052
_ws(ws),
5153
_targetNode(numeric_limits<size_t>::max()),

src/mongo/db/exec/and_sorted.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace mongo {
5353
*/
5454
class AndSortedStage final : public PlanStage {
5555
public:
56-
AndSortedStage(WorkingSet* ws, const Collection* collection);
56+
AndSortedStage(OperationContext* opCtx, WorkingSet* ws, const Collection* collection);
5757

5858
void addChild(PlanStage* child);
5959

src/mongo/db/exec/cached_plan.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ CachedPlanStage::CachedPlanStage(OperationContext* txn,
6060
const QueryPlannerParams& params,
6161
size_t decisionWorks,
6262
PlanStage* root)
63-
: PlanStage(kStageType),
64-
_txn(txn),
63+
: PlanStage(kStageType, txn),
6564
_collection(collection),
6665
_ws(ws),
6766
_canonicalQuery(cq),
@@ -228,15 +227,16 @@ Status CachedPlanStage::replan(PlanYieldPolicy* yieldPolicy, bool shouldCache) {
228227

229228
PlanStage* newRoot;
230229
// Only one possible plan. Build the stages from the solution.
231-
verify(StageBuilder::build(_txn, _collection, *solutions[0], _ws, &newRoot));
230+
verify(StageBuilder::build(getOpCtx(), _collection, *solutions[0], _ws, &newRoot));
232231
_children.emplace_back(newRoot);
233232
_replannedQs.reset(solutions.popAndReleaseBack());
234233
return Status::OK();
235234
}
236235

237236
// Many solutions. Create a MultiPlanStage to pick the best, update the cache,
238237
// and so on. The working set will be shared by all candidate plans.
239-
_children.emplace_back(new MultiPlanStage(_txn, _collection, _canonicalQuery, shouldCache));
238+
_children.emplace_back(
239+
new MultiPlanStage(getOpCtx(), _collection, _canonicalQuery, shouldCache));
240240
MultiPlanStage* multiPlanStage = static_cast<MultiPlanStage*>(child().get());
241241

242242
for (size_t ix = 0; ix < solutions.size(); ++ix) {
@@ -245,7 +245,7 @@ Status CachedPlanStage::replan(PlanYieldPolicy* yieldPolicy, bool shouldCache) {
245245
}
246246

247247
PlanStage* nextPlanRoot;
248-
verify(StageBuilder::build(_txn, _collection, *solutions[ix], _ws, &nextPlanRoot));
248+
verify(StageBuilder::build(getOpCtx(), _collection, *solutions[ix], _ws, &nextPlanRoot));
249249

250250
// Takes ownership of 'solutions[ix]' and 'nextPlanRoot'.
251251
multiPlanStage->addPlan(solutions.releaseAt(ix), nextPlanRoot, _ws);
@@ -291,11 +291,6 @@ PlanStage::StageState CachedPlanStage::work(WorkingSetID* out) {
291291
return childStatus;
292292
}
293293

294-
295-
void CachedPlanStage::doReattachToOperationContext(OperationContext* opCtx) {
296-
_txn = opCtx;
297-
}
298-
299294
void CachedPlanStage::doInvalidate(OperationContext* txn,
300295
const RecordId& dl,
301296
InvalidationType type) {

src/mongo/db/exec/cached_plan.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class CachedPlanStage final : public PlanStage {
6565

6666
StageState work(WorkingSetID* out) final;
6767

68-
void doReattachToOperationContext(OperationContext* opCtx) final;
6968
void doInvalidate(OperationContext* txn, const RecordId& dl, InvalidationType type) final;
7069

7170
StageType stageType() const final {
@@ -115,9 +114,6 @@ class CachedPlanStage final : public PlanStage {
115114
*/
116115
Status tryYield(PlanYieldPolicy* yieldPolicy);
117116

118-
// Not owned.
119-
OperationContext* _txn;
120-
121117
// Not owned. Must be non-null.
122118
Collection* _collection;
123119

src/mongo/db/exec/collection_scan.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ CollectionScan::CollectionScan(OperationContext* txn,
5858
const CollectionScanParams& params,
5959
WorkingSet* workingSet,
6060
const MatchExpression* filter)
61-
: PlanStage(kStageType),
62-
_txn(txn),
61+
: PlanStage(kStageType, txn),
6362
_workingSet(workingSet),
6463
_filter(filter),
6564
_params(params),
@@ -98,7 +97,7 @@ PlanStage::StageState CollectionScan::work(WorkingSetID* out) {
9897
try {
9998
if (needToMakeCursor) {
10099
const bool forward = _params.direction == CollectionScanParams::FORWARD;
101-
_cursor = _params.collection->getCursor(_txn, forward);
100+
_cursor = _params.collection->getCursor(getOpCtx(), forward);
102101

103102
if (!_lastSeenId.isNull()) {
104103
invariant(_params.tailable);
@@ -165,7 +164,7 @@ PlanStage::StageState CollectionScan::work(WorkingSetID* out) {
165164
WorkingSetID id = _workingSet->allocate();
166165
WorkingSetMember* member = _workingSet->get(id);
167166
member->loc = record->id;
168-
member->obj = {_txn->recoveryUnit()->getSnapshotId(), record->data.releaseToBson()};
167+
member->obj = {getOpCtx()->recoveryUnit()->getSnapshotId(), record->data.releaseToBson()};
169168
_workingSet->transitionToLocAndObj(id);
170169

171170
return returnIfMatches(member, id, out);
@@ -223,23 +222,21 @@ void CollectionScan::doSaveState() {
223222
void CollectionScan::doRestoreState() {
224223
if (_cursor) {
225224
if (!_cursor->restore()) {
226-
warning() << "Could not restore RecordCursor for CollectionScan: " << _txn->getNS();
225+
warning() << "Could not restore RecordCursor for CollectionScan: "
226+
<< getOpCtx()->getNS();
227227
_isDead = true;
228228
}
229229
}
230230
}
231231

232232
void CollectionScan::doDetachFromOperationContext() {
233-
_txn = NULL;
234233
if (_cursor)
235234
_cursor->detachFromOperationContext();
236235
}
237236

238-
void CollectionScan::doReattachToOperationContext(OperationContext* opCtx) {
239-
invariant(_txn == NULL);
240-
_txn = opCtx;
237+
void CollectionScan::doReattachToOperationContext() {
241238
if (_cursor)
242-
_cursor->reattachToOperationContext(opCtx);
239+
_cursor->reattachToOperationContext(getOpCtx());
243240
}
244241

245242
unique_ptr<PlanStageStats> CollectionScan::getStats() {

0 commit comments

Comments
 (0)