summaryrefslogtreecommitdiff
path: root/src/mongo/db/ops/update_executor.cpp
blob: bd95b0620248040a570a209a506ccaa716568fd4 (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
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
/**
 *    Copyright (C) 2014 MongoDB 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.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kWrite

#include "mongo/platform/basic.h"

#include "mongo/db/ops/update_executor.h"

#include "mongo/db/catalog/database.h"
#include "mongo/db/concurrency/d_concurrency.h"
#include "mongo/db/exec/update.h"
#include "mongo/db/ops/update.h"
#include "mongo/db/ops/update_driver.h"
#include "mongo/db/ops/update_lifecycle.h"
#include "mongo/db/ops/update_request.h"
#include "mongo/db/query/canonical_query.h"
#include "mongo/db/query/get_executor.h"
#include "mongo/db/query/query_planner_common.h"
#include "mongo/db/repl/oplog.h"
#include "mongo/db/repl/repl_coordinator_global.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/fail_point_service.h"
#include "mongo/util/log.h"

namespace mongo {

    namespace {

        // TODO: Make this a function on NamespaceString, or make it cleaner.
        inline void validateUpdate(const char* ns ,
                                   const BSONObj& updateobj,
                                   const BSONObj& patternOrig) {
            uassert(10155 , "cannot update reserved $ collection", strchr(ns, '$') == 0);
            if (strstr(ns, ".system.")) {
                /* dm: it's very important that system.indexes is never updated as IndexDetails
                   has pointers into it */
                uassert(10156,
                         str::stream() << "cannot update system collection: "
                         << ns << " q: " << patternOrig << " u: " << updateobj,
                         legalClientSystemNS(ns , true));
            }
        }

    } // namespace

    UpdateExecutor::UpdateExecutor(OperationContext* txn, const UpdateRequest* request,
                                   OpDebug* opDebug) :
        _txn(txn),
        _request(request),
        _opDebug(opDebug),
        _driver(UpdateDriver::Options()),
        _canonicalQuery(),
        _isQueryParsed(false),
        _isUpdateParsed(false) {
    }

    UpdateExecutor::~UpdateExecutor() {}

    Status UpdateExecutor::prepare() {
        // We parse the update portion before the query portion because the dispostion of the update
        // may determine whether or not we need to produce a CanonicalQuery at all.  For example, if
        // the update involves the positional-dollar operator, we must have a CanonicalQuery even if
        // it isn't required for query execution.
        Status status = parseUpdate();
        if (!status.isOK())
            return status;
        status = parseQuery();
        if (!status.isOK())
            return status;
        return Status::OK();
    }

    PlanExecutor* UpdateExecutor::getPlanExecutor() {
        return _exec.get();
    }

    MONGO_FP_DECLARE(implicitCollectionCreationDelay);

    Status UpdateExecutor::prepareInLock(Database* db) {
        // If we have a non-NULL PlanExecutor, then we've already done the in-lock preparation.
        if (_exec.get()) {
            return Status::OK();
        }

        const NamespaceString& nsString = _request->getNamespaceString();
        UpdateLifecycle* lifecycle = _request->getLifecycle();

        validateUpdate(nsString.ns().c_str(), _request->getUpdates(), _request->getQuery());

        // The batch executor is responsible for creating a database if this update is being
        // performed against a non-existent database. However, it is possible for either the
        // database or collection to be NULL for an explain. In this case, the explain is
        // a no-op which returns a trivial EOF plan.
        Collection* collection = NULL;
        if (db) {
            collection = db->getCollection(_txn, nsString.ns());
        }
        else {
            invariant(_request->isExplain());
        }

        // The update stage does not create its own collection.  As such, if the update is
        // an upsert, create the collection that the update stage inserts into beforehand.
        // We can only create the collection if this is not an explain, as explains should not
        // alter the state of the database.
        if (!collection && _request->isUpsert() && !_request->isExplain()) {
            // We have to have an exclsive lock on the db to be allowed to create the collection.
            // Callers should either get an X or create the collection.
            const Locker* locker = _txn->lockState();
            invariant( locker->isW() ||
                       locker->isLockHeldForMode( ResourceId( RESOURCE_DATABASE, nsString.db() ),
                                                  MODE_X ) );

            Lock::DBLock lk(_txn->lockState(), nsString.db(), MODE_X);

            WriteUnitOfWork wuow(_txn);
            invariant(db->createCollection(_txn, nsString.ns()));

            if (!_request->isFromReplication()) {
                repl::logOp(_txn,
                            "c",
                            (db->name() + ".$cmd").c_str(),
                            BSON("create" << (nsString.coll())));
            }
            wuow.commit();
            collection = db->getCollection(_txn, nsString.ns());
            invariant(collection);
        }

        // TODO: This seems a bit circuitious.
        _opDebug->updateobj = _request->getUpdates();

        // If this is a user-issued update, then we want to return an error: you cannot perform
        // writes on a secondary. If this is an update to a secondary from the replication system,
        // however, then we make an exception and let the write proceed. In this case,
        // shouldCallLogOp() will be false.
        if (_request->shouldCallLogOp() &&
            !repl::getGlobalReplicationCoordinator()->canAcceptWritesForDatabase(nsString.db())) {
            return Status(ErrorCodes::NotMaster,
                          str::stream() << "Not primary while performing update on "
                                        << nsString.ns());
        }

        if (lifecycle) {
            lifecycle->setCollection(collection);
            _driver.refreshIndexKeys(lifecycle->getIndexKeys(_txn));
        }

        // If yielding is allowed for this plan, then set an auto yield policy. Otherwise set
        // a manual yield policy.
        const bool canYield = !_request->isGod() &&
            PlanExecutor::YIELD_AUTO == _request->getYieldPolicy() && (
            _canonicalQuery.get() ?
            !QueryPlannerCommon::hasNode(_canonicalQuery->root(), MatchExpression::ATOMIC) :
            !LiteParsedQuery::isQueryIsolated(_request->getQuery()));

        PlanExecutor::YieldPolicy policy = canYield ? PlanExecutor::YIELD_AUTO :
                                                      PlanExecutor::YIELD_MANUAL;

        PlanExecutor* rawExec = NULL;
        Status getExecStatus = Status::OK();
        if (_canonicalQuery.get()) {
            // This is the regular path for when we have a CanonicalQuery.
            getExecStatus = getExecutorUpdate(_txn,
                                              collection,
                                              _canonicalQuery.release(),
                                              _request,
                                              &_driver,
                                              _opDebug,
                                              policy,
                                              &rawExec);
        }
        else {
            // This is the idhack fast-path for getting a PlanExecutor without doing the work
            // to create a CanonicalQuery.
            getExecStatus = getExecutorUpdate(_txn,
                                              collection,
                                              nsString.ns(),
                                              _request,
                                              &_driver,
                                              _opDebug,
                                              policy,
                                              &rawExec);
        }

        if (!getExecStatus.isOK()) {
            return getExecStatus;
        }

        invariant(rawExec);
        _exec.reset(rawExec);

        return Status::OK();
    }

    UpdateResult UpdateExecutor::execute(Database* db) {
        uassertStatusOK(prepare());

        LOG(3) << "processing update : " << *_request;

        // If we've already done the in-lock preparation, this is a no-op.
        Status status = prepareInLock(db);
        uassert(17243,
                "could not get executor " + _request->getQuery().toString()
                                         + "; " + causedBy(status),
                status.isOK());

        // Run the plan (don't need to collect results because UpdateStage always returns
        // NEED_TIME).
        uassertStatusOK(_exec->executePlan());

        // Get stats from the root stage.
        invariant(_exec->getRootStage()->stageType() == STAGE_UPDATE);
        UpdateStage* updateStage = static_cast<UpdateStage*>(_exec->getRootStage());
        const UpdateStats* updateStats =
            static_cast<const UpdateStats*>(updateStage->getSpecificStats());

        // Use stats from the root stage to fill out opDebug.
        _opDebug->nMatched = updateStats->nMatched;
        _opDebug->nModified = updateStats->nModified;
        _opDebug->upsert = updateStats->inserted;
        _opDebug->fastmodinsert = updateStats->fastmodinsert;
        _opDebug->fastmod = updateStats->fastmod;

        // Historically, 'opDebug' considers 'nMatched' and 'nModified' to be 1 (rather than 0) if
        // there is an upsert that inserts a document. The UpdateStage does not participate in this
        // madness in order to have saner stats reporting for explain. This means that we have to
        // set these values "manually" in the case of an insert.
        if (updateStats->inserted) {
            _opDebug->nMatched = 1;
            _opDebug->nModified = 1;
        }

        // Get summary information about the plan.
        PlanSummaryStats stats;
        Explain::getSummaryStats(_exec.get(), &stats);
        _opDebug->nscanned = stats.totalKeysExamined;
        _opDebug->nscannedObjects = stats.totalDocsExamined;

        return UpdateResult(updateStats->nMatched > 0 /* Did we update at least one obj? */,
                            !_driver.isDocReplacement() /* $mod or obj replacement */,
                            _opDebug->nModified /* number of modified docs, no no-ops */,
                            _opDebug->nMatched /* # of docs matched/updated, even no-ops */,
                            updateStats->objInserted);
    }

    Status UpdateExecutor::parseQuery() {
        if (_isQueryParsed)
            return Status::OK();

        dassert(!_canonicalQuery.get());
        dassert(_isUpdateParsed);

        if (!_driver.needMatchDetails() && CanonicalQuery::isSimpleIdQuery(_request->getQuery())) {
            _isQueryParsed = true;
            return Status::OK();
        }

        CanonicalQuery* cqRaw;
        const WhereCallbackReal whereCallback(_txn, _request->getNamespaceString().db());

        Status status = CanonicalQuery::canonicalize(_request->getNamespaceString().ns(),
                                                     _request->getQuery(),
                                                     _request->isExplain(),
                                                     &cqRaw,
                                                     whereCallback);
        if (status.isOK()) {
            cqRaw->setIsForWrite( true );
            _canonicalQuery.reset(cqRaw);
            _isQueryParsed = true;
        }

        return status;
    }

    Status UpdateExecutor::parseUpdate() {
        if (_isUpdateParsed)
            return Status::OK();

        const NamespaceString& ns(_request->getNamespaceString());

        // Should the modifiers validate their embedded docs via okForStorage
        // Only user updates should be checked. Any system or replication stuff should pass through.
        // Config db docs shouldn't get checked for valid field names since the shard key can have
        // a dot (".") in it.
        const bool shouldValidate = !(_request->isFromReplication() ||
                                      ns.isConfigDB() ||
                                      _request->isFromMigration());

        _driver.setLogOp(true);
        _driver.setModOptions(ModifierInterface::Options(_request->isFromReplication(),
                                                         shouldValidate));
        Status status = _driver.parse(_request->getUpdates(), _request->isMulti());
        if (status.isOK())
            _isUpdateParsed = true;
        return status;
    }

}  // namespace mongo