summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/rollback_fix_up_info.cpp
blob: 2632e0212622e4e62d86c53bd1a34548ebb808ab (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
/**
 *    Copyright (C) 2017 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::kReplication

#include "mongo/platform/basic.h"

#include "mongo/db/repl/rollback_fix_up_info.h"

#include "mongo/base/string_data.h"
#include "mongo/bson/simple_bsonelement_comparator.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/repl/rollback_fix_up_info_descriptions.h"
#include "mongo/db/repl/storage_interface.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/log.h"
#include "mongo/util/uuid.h"

namespace mongo {
namespace repl {

namespace {

const auto kRollbackNamespacePrefix = "local.system.rollback."_sd;

}  // namespace

const NamespaceString RollbackFixUpInfo::kRollbackDocsNamespace(kRollbackNamespacePrefix + "docs");

const NamespaceString RollbackFixUpInfo::kRollbackCollectionUuidNamespace(kRollbackNamespacePrefix +
                                                                          "collectionUuid");

const NamespaceString RollbackFixUpInfo::kRollbackCollectionOptionsNamespace(
    kRollbackNamespacePrefix + "collectionOptions");

const NamespaceString RollbackFixUpInfo::kRollbackIndexNamespace(kRollbackNamespacePrefix +
                                                                 "indexes");

RollbackFixUpInfo::RollbackFixUpInfo(StorageInterface* storageInterface)
    : _storageInterface(storageInterface) {
    invariant(storageInterface);
}

Status RollbackFixUpInfo::processSingleDocumentOplogEntry(OperationContext* opCtx,
                                                          const UUID& collectionUuid,
                                                          const BSONElement& docId,
                                                          SingleDocumentOpType opType,
                                                          const std::string& dbName) {
    SingleDocumentOperationDescription desc(collectionUuid, docId, opType, dbName);
    auto doc = desc.toBSON();
    if (SingleDocumentOpType::kInsert == opType) {
        // If the existing document (that may or may not exist in the "kRollbackDocsNamespace"
        // collection) has a 'delete' op type, this oplog entry will cancel out the previously
        // processed 'delete" oplog entry. We should remove the existing document from the
        // collection and not insert a new document.
        auto deleteResult =
            _storageInterface->deleteById(opCtx, kRollbackDocsNamespace, doc["_id"]);
        if (deleteResult.isOK()) {
            auto existingDoc = deleteResult.getValue();
            if ("delete" == existingDoc["operationType"].String()) {
                return Status::OK();
            }
            // Fall through and replace the 'update' op type in the existing document with 'insert'
            // so that the document will be dropped when we actually do the rollback.
        }
    } else if (SingleDocumentOpType::kUpdate == opType) {
        // If there is an existing document in the "kRollbackDocsNamespace" collection, it must
        // have either a 'delete' or 'update' op type.
        //
        // For a 'delete' entry, we should not replace it with 'update' so that if we process an
        // oplog entry with an 'insert' op type later, we can cancel out the existing entry with the
        // 'delete' op type.
        //
        // For an 'update' entry, there is nothing further to do because this matches the current op
        // type passed to this function.
        auto findResult = _storageInterface->findById(opCtx, kRollbackDocsNamespace, doc["_id"]);
        if (findResult.isOK()) {
            return Status::OK();
        }
        // No existing document. Insert a new document with 'update' op type.
    }
    return _upsertById(opCtx, kRollbackDocsNamespace, doc);
}

Status RollbackFixUpInfo::processCreateCollectionOplogEntry(OperationContext* opCtx,
                                                            const UUID& collectionUuid) {
    CollectionUuidDescription desc(collectionUuid, {});
    auto status = _upsertById(opCtx, kRollbackCollectionUuidNamespace, desc.toBSON());
    if (!status.isOK()) {
        return status;
    }

    // Remove all references to the collection in the create command from the other rollback
    // fix up collections. The documents to be removed will contain 'collectionUuid' as the _id
    // field or nested inside the _id field with the dotted field name '_id.collectionUuid'.

    // Generate key for nested _id field {_id: {collectionUuid: <collection uuid>, ...}, ...}.
    BSONObjBuilder nestedIdFilterBob;
    collectionUuid.appendToBuilder(&nestedIdFilterBob, "_id.collectionUuid");
    auto nestedIdFilter = nestedIdFilterBob.obj();

    // Remove documents from the "kRollbackDocsNamespace" collection with _id's containing
    // 'collectionUuid':
    //     {_id: {collectionUuid: <collectionUuid>, docId: <modified document _id>}, ...}
    status = _storageInterface->deleteByFilter(opCtx, kRollbackDocsNamespace, nestedIdFilter);
    if (!status.isOK()) {
        return status;
    }

    // Generate key for _id field {_id: <collection uuid>, ...}.
    BSONObjBuilder idFilterBob;
    collectionUuid.appendToBuilder(&idFilterBob, "_id");
    auto idFilter = idFilterBob.obj();

    // Remove documents from the "kRollbackCollectionOptionsNamespace" and "kRollbackIndexNamespace"
    // collections with the _id field set to 'collectionUuid'.
    //     {_id: <collectionUuid>, ...}
    status =
        _storageInterface->deleteByFilter(opCtx, kRollbackCollectionOptionsNamespace, idFilter);
    if (!status.isOK()) {
        return status;
    }

    status = _storageInterface->deleteByFilter(opCtx, kRollbackIndexNamespace, nestedIdFilter);
    if (!status.isOK()) {
        return status;
    }

    return Status::OK();
}

Status RollbackFixUpInfo::processDropCollectionOplogEntry(OperationContext* opCtx,
                                                          const UUID& collectionUuid,
                                                          const NamespaceString& nss) {
    CollectionUuidDescription desc(collectionUuid, nss);
    return _upsertById(opCtx, kRollbackCollectionUuidNamespace, desc.toBSON());
}

Status RollbackFixUpInfo::processRenameCollectionOplogEntry(
    OperationContext* opCtx,
    const UUID& sourceCollectionUuid,
    const NamespaceString& sourceNss,
    boost::optional<CollectionUuidAndNss> targetCollectionUuidAndNss) {
    CollectionUuidDescription sourceDesc(sourceCollectionUuid, sourceNss);

    auto status = _upsertById(opCtx, kRollbackCollectionUuidNamespace, sourceDesc.toBSON());
    if (!status.isOK()) {
        return status;
    }

    // If target collection is not dropped during the rename operation, there is nothing further to
    // do.
    if (!targetCollectionUuidAndNss) {
        return Status::OK();
    }

    CollectionUuidDescription targetDesc(targetCollectionUuidAndNss->first,
                                         targetCollectionUuidAndNss->second);
    return _upsertById(opCtx, kRollbackCollectionUuidNamespace, targetDesc.toBSON());
}

Status RollbackFixUpInfo::processCollModOplogEntry(OperationContext* opCtx,
                                                   const UUID& collectionUuid,
                                                   const BSONObj& optionsObj) {
    // If validation is enabled for the collection, the collection options document may contain
    // dollar ($) prefixed field in the "validator" field. Normally, the update operator in the
    // query execution framework disallows such fields (see validateDollarPrefixElement() in
    // exec/update.cpp). To disable this check when upserting the collection options document into
    // the "kRollbackCollectionOptionsNamespace", we have to disable replicated writes in the
    // OperationContext during the update operation.
    UnreplicatedWritesBlock uwb(opCtx);

    CollectionOptionsDescription desc(collectionUuid, optionsObj);
    return _upsertById(opCtx, kRollbackCollectionOptionsNamespace, desc.toBSON());
}

Status RollbackFixUpInfo::processCreateIndexOplogEntry(OperationContext* opCtx,
                                                       const UUID& collectionUuid,
                                                       const std::string& indexName) {
    IndexDescription desc(collectionUuid, indexName, IndexOpType::kCreate, {});

    // If the existing document (that may or may not exist in the "kRollbackIndexNamespace"
    // collection) has a 'drop' op type, this oplog entry will cancel out the previously processed
    // 'dropIndexes" oplog entry. We should remove the existing document from the collection and not
    // insert a new document.
    BSONObjBuilder bob;
    bob.append("_id", desc.makeIdKey());
    auto key = bob.obj();
    auto deleteResult = _storageInterface->deleteById(opCtx, kRollbackIndexNamespace, key["_id"]);
    if (deleteResult.isOK()) {
        auto doc = deleteResult.getValue();
        auto opTypeResult = IndexDescription::parseOpType(doc);
        if (!opTypeResult.isOK()) {
            invariant(ErrorCodes::FailedToParse == opTypeResult.getStatus());
            warning() << "While processing createIndex oplog entry for index " << indexName
                      << " in collection with UUID " << collectionUuid.toString()
                      << ", found existing entry in rollback collection " << kRollbackIndexNamespace
                      << " with unrecognized operation type:" << doc
                      << ". Replacing existing entry.";
            return _upsertIndexDescription(opCtx, desc);
        } else if (IndexOpType::kDrop == opTypeResult.getValue()) {
            return Status::OK();
        }
        // Fall through and replace existing document.
    }

    return _upsertIndexDescription(opCtx, desc);
}

Status RollbackFixUpInfo::processUpdateIndexTTLOplogEntry(OperationContext* opCtx,
                                                          const UUID& collectionUuid,
                                                          const std::string& indexName,
                                                          Seconds expireAfterSeconds) {
    IndexDescription desc(collectionUuid, indexName, expireAfterSeconds);
    return _upsertIndexDescription(opCtx, desc);
}

Status RollbackFixUpInfo::processDropIndexOplogEntry(OperationContext* opCtx,
                                                     const UUID& collectionUuid,
                                                     const std::string& indexName,
                                                     const BSONObj& infoObj) {
    IndexDescription desc(collectionUuid, indexName, IndexOpType::kDrop, infoObj);
    return _upsertIndexDescription(opCtx, desc);
}

Status RollbackFixUpInfo::_upsertById(OperationContext* opCtx,
                                      const NamespaceString& nss,
                                      const BSONObj& update) {
    auto key = update["_id"];
    invariant(!key.eoo());
    return _storageInterface->upsertById(opCtx, nss, key, update);
}

Status RollbackFixUpInfo::_upsertIndexDescription(OperationContext* opCtx,
                                                  const IndexDescription& description) {
    switch (description.getOpType()) {
        case IndexOpType::kCreate:
        case IndexOpType::kDrop:
            return _upsertById(opCtx, kRollbackIndexNamespace, description.toBSON());
        case IndexOpType::kUpdateTTL: {
            // For updateTTL, if there is an existing document in the collection with a "drop" op
            // type, we should update the index info obj in the existing document and leave the op
            // type unchanged. Otherwise, we assume that the existing document has an op type of
            // "updateTTL"; or is not present in the collection. Therefore, it is safe to overwrite
            // any existing data.
            //
            // It's not possible for the existing document to have a "create" op type while
            // processing a collMod (updateTTL) because this implies the follow sequence of
            // of operations in the oplog:
            //    ..., collMod, ..., createIndex, ...
            // (createIndex gets processed before collMod)
            // This is illegal because there's a missing dropIndex oplog entry between the collMod
            // and createIndex oplog entries.
            auto expireAfterSeconds = description.getExpireAfterSeconds();
            invariant(expireAfterSeconds);
            BSONObjBuilder updateBob;
            {
                BSONObjBuilder setOnInsertBob(updateBob.subobjStart("$setOnInsert"));
                setOnInsertBob.append("operationType", description.getOpTypeAsString());
            }
            {
                BSONObjBuilder setBob(updateBob.subobjStart("$set"));
                setBob.append("infoObj.expireAfterSeconds",
                              durationCount<Seconds>(*expireAfterSeconds));
            }
            auto updateDoc = updateBob.obj();

            BSONObjBuilder bob;
            bob.append("_id", description.makeIdKey());
            auto key = bob.obj();
            return _storageInterface->upsertById(
                opCtx, kRollbackIndexNamespace, key.firstElement(), updateDoc);
        }
    }
    MONGO_UNREACHABLE;
}

}  // namespace repl
}  // namespace mongo