summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/apply_ops.cpp
blob: df7f90cb7b675f12c62b5f4232189e5e7d21a87a (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
/**
 *    Copyright (C) 2015 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::kCommand

#include "mongo/platform/basic.h"

#include "mongo/db/catalog/apply_ops.h"

#include "mongo/db/client.h"
#include "mongo/db/commands/dbhash.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/curop.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/matcher/extensions_callback_disallow_extensions.h"
#include "mongo/db/matcher/matcher.h"
#include "mongo/db/op_observer.h"
#include "mongo/db/operation_context_impl.h"
#include "mongo/db/repl/oplog.h"
#include "mongo/db/repl/replication_coordinator_global.h"
#include "mongo/db/service_context.h"
#include "mongo/util/log.h"

namespace mongo {
Status applyOps(OperationContext* txn,
                const std::string& dbName,
                const BSONObj& applyOpCmd,
                BSONObjBuilder* result) {
    // SERVER-4328 todo : is global ok or does this take a long time? i believe multiple
    // ns used so locking individually requires more analysis
    ScopedTransaction scopedXact(txn, MODE_X);
    Lock::GlobalWrite globalWriteLock(txn->lockState());

    bool userInitiatedWritesAndNotPrimary = txn->writesAreReplicated() &&
        !repl::getGlobalReplicationCoordinator()->canAcceptWritesForDatabase(dbName);

    if (userInitiatedWritesAndNotPrimary) {
        return Status(ErrorCodes::NotMaster,
                      str::stream() << "Not primary while applying ops to database " << dbName);
    }

    bool shouldReplicateWrites = txn->writesAreReplicated();
    txn->setReplicatedWrites(false);
    BSONObj ops = applyOpCmd.firstElement().Obj();
    // Preconditions check reads the database state, so needs to be done locked
    if (applyOpCmd["preCondition"].type() == Array) {
        BSONObjIterator i(applyOpCmd["preCondition"].Obj());
        while (i.more()) {
            BSONObj f = i.next().Obj();

            DBDirectClient db(txn);
            BSONObj realres = db.findOne(f["ns"].String(), f["q"].Obj());

            // Apply-ops would never have a $where/$text matcher. Using the "DisallowExtensions"
            // callback ensures that parsing will throw an error if $where or $text are found.
            Matcher m(f["res"].Obj(), ExtensionsCallbackDisallowExtensions());
            if (!m.matches(realres)) {
                result->append("got", realres);
                result->append("whatFailed", f);
                txn->setReplicatedWrites(shouldReplicateWrites);
                return Status(ErrorCodes::BadValue, "pre-condition failed");
            }
        }
    }

    // apply
    int num = 0;
    int errors = 0;

    BSONObjIterator i(ops);
    BSONArrayBuilder ab;
    const bool alwaysUpsert =
        applyOpCmd.hasField("alwaysUpsert") ? applyOpCmd["alwaysUpsert"].trueValue() : true;

    while (i.more()) {
        BSONElement e = i.next();
        const BSONObj& temp = e.Obj();

        // Ignore 'n' operations.
        const char* opType = temp["op"].valuestrsafe();
        if (*opType == 'n')
            continue;

        const std::string ns = temp["ns"].String();

        // Run operations under a nested lock as a hack to prevent yielding.
        //
        // The list of operations is supposed to be applied atomically; yielding
        // would break atomicity by allowing an interruption or a shutdown to occur
        // after only some operations are applied.  We are already locked globally
        // at this point, so taking a DBLock on the namespace creates a nested lock,
        // and yields are disallowed for operations that hold a nested lock.
        //
        // We do not have a wrapping WriteUnitOfWork so it is possible for a journal
        // commit to happen with a subset of ops applied.
        // TODO figure out what to do about this.
        Lock::GlobalWrite globalWriteLockDisallowTempRelease(txn->lockState());

        // Ensures that yielding will not happen (see the comment above).
        DEV {
            Locker::LockSnapshot lockSnapshot;
            invariant(!txn->lockState()->saveLockStateAndUnlock(&lockSnapshot));
        };

        Status status(ErrorCodes::InternalError, "");

        try {
            MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN {
                if (*opType == 'c') {
                    status = repl::applyCommand_inlock(txn, temp);
                    break;
                } else {
                    OldClientContext ctx(txn, ns);

                    status = repl::applyOperation_inlock(txn, ctx.db(), temp, alwaysUpsert);
                    break;
                }
            }
            MONGO_WRITE_CONFLICT_RETRY_LOOP_END(txn, "applyOps", ns);
        } catch (const DBException& ex) {
            ab.append(false);
            result->append("applied", ++num);
            result->append("code", ex.getCode());
            result->append("errmsg", ex.what());
            result->append("results", ab.arr());
            return Status(ErrorCodes::UnknownError, "");
        }

        ab.append(status.isOK());
        if (!status.isOK()) {
            errors++;
        }

        num++;

        WriteUnitOfWork wuow(txn);
        logOpForDbHash(txn, ns.c_str());
        wuow.commit();
    }

    result->append("applied", num);
    result->append("results", ab.arr());
    txn->setReplicatedWrites(shouldReplicateWrites);

    if (txn->writesAreReplicated()) {
        // We want this applied atomically on slaves
        // so we re-wrap without the pre-condition for speed

        std::string tempNS = str::stream() << dbName << ".$cmd";

        // TODO: possibly use mutable BSON to remove preCondition field
        // once it is available
        BSONObjBuilder cmdBuilder;

        for (auto elem : applyOpCmd) {
            auto name = elem.fieldNameStringData();
            if (name == "preCondition")
                continue;
            if (name == "bypassDocumentValidation")
                continue;
            cmdBuilder.append(elem);
        }

        const BSONObj cmdRewritten = cmdBuilder.done();

        // We currently always logOp the command regardless of whether the individial ops
        // succeeded and rely on any failures to also happen on secondaries. This isn't
        // perfect, but it's what the command has always done and is part of its "correct"
        // behavior.
        while (true) {
            try {
                WriteUnitOfWork wunit(txn);
                getGlobalServiceContext()->getOpObserver()->onApplyOps(txn, tempNS, cmdRewritten);
                wunit.commit();
                break;
            } catch (const WriteConflictException& wce) {
                LOG(2) << "WriteConflictException while logging applyOps command, retrying.";
                txn->recoveryUnit()->abandonSnapshot();
                continue;
            }
        }
    }

    if (errors != 0) {
        return Status(ErrorCodes::UnknownError, "");
    }

    return Status::OK();
}

}  // namespace mongo