summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/write_commands/batch_executor.cpp
blob: 8546842016057190c85569d370cddf8120dd984e (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
/**
 *    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/>.
 */

#include "mongo/db/commands/write_commands/batch_executor.h"

#include "mongo/db/commands.h"
#include "mongo/db/instance.h"
#include "mongo/db/introspect.h"
#include "mongo/db/lasterror.h"
#include "mongo/db/ops/delete.h"
#include "mongo/db/ops/update.h"
#include "mongo/db/pagefault.h"
#include "mongo/db/stats/counters.h"
#include "mongo/db/write_concern.h"

namespace mongo {

    WriteBatchExecutor::WriteBatchExecutor(Client* client, OpCounters* opCounters, LastError* le)
        : _client(client)
        , _opCounters(opCounters)
        , _le(le) {}

    bool WriteBatchExecutor::executeBatch(const WriteBatch& writeBatch,
                                          string* errMsg,
                                          BSONObjBuilder* result) {
        Timer commandTimer;

        BSONArrayBuilder resultsArray;
        bool batchSuccess = applyWriteBatch(writeBatch, &resultsArray);
        result->append("resultsBatchSuccess", batchSuccess);
        result->append("results", resultsArray.arr());

        BSONObjBuilder writeConcernResults;
        Timer writeConcernTimer;

        // TODO Define final layout for write commands result object.

        bool writeConcernSuccess = waitForWriteConcern(writeBatch.getWriteConcern(),
                                                       !batchSuccess,
                                                       &writeConcernResults,
                                                       errMsg);
        if (!writeConcernSuccess) {
            return false;
        }

        const char *writeConcernErrField = writeConcernResults.asTempObj().getStringField("err");
        // TODO Should consider changing following existing strange behavior with GLE?
        // - {w:2} specified with batch where any op fails skips replication wait, yields success
        bool writeConcernFulfilled = !writeConcernErrField || strlen(writeConcernErrField) == 0;
        writeConcernResults.append("micros", static_cast<long long>(writeConcernTimer.micros()));
        writeConcernResults.append("ok", writeConcernFulfilled);
        result->append("writeConcernResults", writeConcernResults.obj());

        result->append("micros", static_cast<long long>(commandTimer.micros()));

        return true;
    }

    bool WriteBatchExecutor::applyWriteBatch(const WriteBatch& writeBatch,
                                             BSONArrayBuilder* resultsArray) {
        bool batchSuccess = true;
        for (size_t i = 0; i < writeBatch.getNumWriteItems(); ++i) {
            const WriteBatch::WriteItem& writeItem = writeBatch.getWriteItem(i);

            // All writes in the batch must be of the same type:
            dassert(writeBatch.getWriteType() == writeItem.getWriteType());

            BSONObjBuilder results;
            bool opSuccess = applyWriteItem(writeBatch.getNS(), writeItem, &results);
            resultsArray->append(results.obj());

            batchSuccess &= opSuccess;

            if (!opSuccess && !writeBatch.getContinueOnError()) {
                break;
            }
        }

        return batchSuccess;
    }

    namespace {

        // Translates write item type to wire protocol op code.
        // Helper for WriteBatchExecutor::applyWriteItem().
        int getOpCode(WriteBatch::WriteType writeType) {
            switch (writeType) {
            case WriteBatch::WRITE_INSERT:
                return dbInsert;
            case WriteBatch::WRITE_UPDATE:
                return dbUpdate;
            case WriteBatch::WRITE_DELETE:
                return dbDelete;
            }
            dassert(false);
            return 0;
        }

    } // namespace

    bool WriteBatchExecutor::applyWriteItem(const string& ns,
                                            const WriteBatch::WriteItem& writeItem,
                                            BSONObjBuilder* results) {
        // Clear operation's LastError before starting.
        _le->reset(true);

        uint64_t itemTimeMicros = 0;
        bool opSuccess = true;

        // Each write operation executes in its own PageFaultRetryableSection.  This means that
        // a single batch can throw multiple PageFaultException's, which is not the case for
        // other operations.
        PageFaultRetryableSection s;
        while (true) {
            try {
                // Execute the write item as a child operation of the current operation.
                CurOp childOp(_client, _client->curop());

                // TODO Modify CurOp "wrapped" constructor to take an opcode, so calling .reset()
                // is unneeded
                childOp.reset(_client->getRemote(), getOpCode(writeItem.getWriteType()));

                childOp.ensureStarted();
                OpDebug& opDebug = childOp.debug();
                opDebug.ns = ns;
                {
                    Client::WriteContext ctx(ns);

                    switch(writeItem.getWriteType()) {
                    case WriteBatch::WRITE_INSERT:
                        opSuccess = applyInsert(ns, writeItem, &childOp);
                        break;
                    case WriteBatch::WRITE_UPDATE:
                        opSuccess = applyUpdate(ns, writeItem, &childOp);
                        break;
                    case WriteBatch::WRITE_DELETE:
                        opSuccess = applyDelete(ns, writeItem, &childOp);
                        break;
                    }
                }
                childOp.done();
                itemTimeMicros = childOp.totalTimeMicros();

                opDebug.executionTime = childOp.totalTimeMillis();
                opDebug.recordStats();

                // Log operation if running with at least "-v", or if exceeds slow threshold.
                if (logger::globalLogDomain()->shouldLog(logger::LogSeverity::Debug(1))
                    || opDebug.executionTime > cmdLine.slowMS + childOp.getExpectedLatencyMs()) {

                    MONGO_TLOG(1) << opDebug.report(childOp) << endl;
                }

                // TODO Log operation if logLevel >= 3 and assertion thrown (as assembleResponse()
                // does).

                // Save operation to system.profile if shouldDBProfile().
                if (childOp.shouldDBProfile(opDebug.executionTime)) {
                    profile(*_client, getOpCode(writeItem.getWriteType()), childOp);
                }
                break;
            }
            catch (PageFaultException& e) {
                e.touch();
            }
        }

        // Fill caller's builder with results of operation, using LastError.
        results->append("ok", opSuccess);
        _le->appendSelf(*results, false);
        results->append("micros", static_cast<long long>(itemTimeMicros));

        return opSuccess;
    }

    bool WriteBatchExecutor::applyInsert(const string& ns,
                                         const WriteBatch::WriteItem& writeItem,
                                         CurOp* currentOp) {
        OpDebug& opDebug = currentOp->debug();

        _opCounters->gotInsert();

        opDebug.op = dbInsert;

        BSONObj doc;

        string errMsg;
        bool ret = writeItem.parseInsertItem(&errMsg, &doc);
        verify(ret); // writeItem should have been already validated by WriteBatch::parse().

        try {
            // TODO Should call insertWithObjMod directly instead of checkAndInsert?  Note that
            // checkAndInsert will use mayInterrupt=false, so index builds initiated here won't
            // be interruptible.
            checkAndInsert(ns.c_str(), doc);
            getDur().commitIfNeeded();
            _le->nObjects = 1; // TODO Replace after implementing LastError::recordInsert().
            opDebug.ninserted = 1;
        }
        catch (UserException& e) {
            opDebug.exceptionInfo = e.getInfo();
            return false;
        }

        return true;
    }

    bool WriteBatchExecutor::applyUpdate(const string& ns,
                                         const WriteBatch::WriteItem& writeItem,
                                         CurOp* currentOp) {
        OpDebug& opDebug = currentOp->debug();

        _opCounters->gotUpdate();

        BSONObj queryObj;
        BSONObj updateObj;
        bool multi;
        bool upsert;

        string errMsg;
        bool ret = writeItem.parseUpdateItem(&errMsg, &queryObj, &updateObj, &multi, &upsert);
        verify(ret); // writeItem should have been already validated by WriteBatch::parse().

        currentOp->setQuery(queryObj);
        opDebug.op = dbUpdate;
        opDebug.query = queryObj;

        bool resExisting = false;
        long long resNum = 0;
        OID resUpserted = OID();
        try {

            const NamespaceString requestNs(ns);
            UpdateRequest request(requestNs);

            request.setQuery(queryObj);
            request.setUpdates(updateObj);
            request.setUpsert(upsert);
            request.setMulti(multi);
            request.setUpdateOpLog();

            UpdateResult res = update(request, &opDebug);

            resExisting = res.existing;
            resNum = res.numMatched;
            resUpserted = res.upserted;
        }
        catch (UserException& e) {
            opDebug.exceptionInfo = e.getInfo();
            return false;
        }

        _le->recordUpdate(resExisting, resNum, resUpserted);

        return true;
    }

    bool WriteBatchExecutor::applyDelete(const string& ns,
                                         const WriteBatch::WriteItem& writeItem,
                                         CurOp* currentOp) {
        OpDebug& opDebug = currentOp->debug();

        _opCounters->gotDelete();

        BSONObj queryObj;

        string errMsg;
        bool ret = writeItem.parseDeleteItem(&errMsg, &queryObj);
        verify(ret); // writeItem should have been already validated by WriteBatch::parse().

        currentOp->setQuery(queryObj);
        opDebug.op = dbDelete;
        opDebug.query = queryObj;

        long long n;

        try {
            n = deleteObjects(ns.c_str(),
                              queryObj,
                              /*justOne*/false,
                              /*logOp*/true,
                              /*god*/false);
        }
        catch (UserException& e) {
            opDebug.exceptionInfo = e.getInfo();
            return false;
        }

        _le->recordDelete(n);
        opDebug.ndeleted = n;

        return true;
    }

} // namespace mongo