summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/rollback_fix_up_info_descriptions.cpp
blob: 5dc300b0293b1417ecf7f6d0793109a539be8fe8 (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) 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::kReplicationRollback

#include "mongo/platform/basic.h"

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

#include "mongo/bson/util/bson_extract.h"
#include "mongo/db/jsobj.h"

namespace mongo {
namespace repl {

namespace {

/**
 * Appends single document op type to builder as string element under the field name "op".
 */
void appendOpTypeToBuilder(RollbackFixUpInfo::SingleDocumentOpType opType,
                           BSONObjBuilder* builder) {
    auto fieldName = "operationType"_sd;
    switch (opType) {
        case RollbackFixUpInfo::SingleDocumentOpType::kDelete:
            builder->append(fieldName, "delete");
            break;
        case RollbackFixUpInfo::SingleDocumentOpType::kInsert:
            builder->append(fieldName, "insert");
            break;
        case RollbackFixUpInfo::SingleDocumentOpType::kUpdate:
            builder->append(fieldName, "update");
            break;
    }
}

/**
 * Returns string representation of RollbackFixUpInfo::IndexOpType.
 */
std::string toString(RollbackFixUpInfo::IndexOpType opType) {
    switch (opType) {
        case RollbackFixUpInfo::IndexOpType::kCreate:
            return "create";
        case RollbackFixUpInfo::IndexOpType::kDrop:
            return "drop";
        case RollbackFixUpInfo::IndexOpType::kUpdateTTL:
            return "updateTTL";
    }
    MONGO_UNREACHABLE;
}
/**
 * Appends index op type to builder as string element under the field name "op".
 */
void appendOpTypeToBuilder(RollbackFixUpInfo::IndexOpType opType, BSONObjBuilder* builder) {
    builder->append("operationType", toString(opType));
}

}  // namespace

RollbackFixUpInfo::SingleDocumentOperationDescription::SingleDocumentOperationDescription(
    const UUID& collectionUuid,
    const BSONElement& docId,
    RollbackFixUpInfo::SingleDocumentOpType opType,
    const std::string& dbName)
    : _collectionUuid(collectionUuid),
      _wrappedDocId(docId.wrap("documentId")),
      _opType(opType),
      _dbName(dbName) {}

BSONObj RollbackFixUpInfo::SingleDocumentOperationDescription::toBSON() const {
    // For non-insert operations, we will use the collection UUID and document id to query the sync
    // source (using the source collection's default collation) for the most recent copy of the
    // deleted/updated document. This is necessary to complete the rollback for a deleted/updated
    // document.
    // Insert operations are rolled back by deleting the the document from the local collection.
    BSONObjBuilder bob;
    {
        BSONObjBuilder idBob(bob.subobjStart("_id"));
        _collectionUuid.appendToBuilder(&idBob, "collectionUuid");
        idBob.append(_wrappedDocId.firstElement());
    }

    // This matches the "op" field in the oplog entry.
    appendOpTypeToBuilder(_opType, &bob);

    // The database name is used in the find command request when fetching the document from the
    // sync source.
    bob.append("db", _dbName);

    // This will be replaced by the most recent copy of the affected document from the sync
    // source. If the document is not found on the sync source, this will remain null.
    bob.appendNull("documentToRestore");

    return bob.obj();
}

RollbackFixUpInfo::CollectionUuidDescription::CollectionUuidDescription(const UUID& collectionUuid,
                                                                        const NamespaceString& nss)
    : _collectionUuid(collectionUuid), _nss(nss) {}

BSONObj RollbackFixUpInfo::CollectionUuidDescription::toBSON() const {
    BSONObjBuilder bob;
    _collectionUuid.appendToBuilder(&bob, "_id");
    bob.append("ns", _nss.ns());
    return bob.obj();
}

RollbackFixUpInfo::CollectionOptionsDescription::CollectionOptionsDescription(
    const UUID& collectionUuid, const BSONObj& optionsObj)
    : _collectionUuid(collectionUuid), _optionsObj(optionsObj) {}

BSONObj RollbackFixUpInfo::CollectionOptionsDescription::toBSON() const {
    BSONObjBuilder bob;
    _collectionUuid.appendToBuilder(&bob, "_id");
    bob.append("options", _optionsObj);
    return bob.obj();
}

RollbackFixUpInfo::IndexDescription::IndexDescription(const UUID& collectionUuid,
                                                      const std::string& indexName,
                                                      RollbackFixUpInfo::IndexOpType opType,
                                                      const BSONObj& infoObj)
    : _collectionUuid(collectionUuid), _indexName(indexName), _opType(opType), _infoObj(infoObj) {
    invariant(RollbackFixUpInfo::IndexOpType::kUpdateTTL != _opType);
}

RollbackFixUpInfo::IndexDescription::IndexDescription(const UUID& collectionUuid,
                                                      const std::string& indexName,
                                                      Seconds expireAfterSeconds)
    : _collectionUuid(collectionUuid),
      _indexName(indexName),
      _opType(RollbackFixUpInfo::IndexOpType::kUpdateTTL),
      _expireAfterSeconds(expireAfterSeconds) {
    BSONObjBuilder bob;
    bob.append("expireAfterSeconds", durationCount<Seconds>(*_expireAfterSeconds));
    _infoObj = bob.obj();
}

RollbackFixUpInfo::IndexOpType RollbackFixUpInfo::IndexDescription::getOpType() const {
    return _opType;
}

std::string RollbackFixUpInfo::IndexDescription::getOpTypeAsString() const {
    return toString(_opType);
}

boost::optional<Seconds> RollbackFixUpInfo::IndexDescription::getExpireAfterSeconds() const {
    return _expireAfterSeconds;
}

// static
StatusWith<RollbackFixUpInfo::IndexOpType> RollbackFixUpInfo::IndexDescription::parseOpType(
    const BSONObj& doc) {
    std::string opTypeStr;
    auto status = bsonExtractStringField(doc, "operationType"_sd, &opTypeStr);
    if (!status.isOK()) {
        return status;
    }
    if ("create" == opTypeStr) {
        return RollbackFixUpInfo::IndexOpType::kCreate;
    } else if ("drop" == opTypeStr) {
        return RollbackFixUpInfo::IndexOpType::kDrop;
    } else if ("updateTTL" == opTypeStr) {
        return RollbackFixUpInfo::IndexOpType::kUpdateTTL;
    }
    return Status(ErrorCodes::FailedToParse,
                  str::stream() << "Unrecognized RollbackFixUpInfo::IndexOpType: " << opTypeStr);
}

BSONObj RollbackFixUpInfo::IndexDescription::toBSON() const {
    BSONObjBuilder bob;
    bob.append("_id", makeIdKey());
    appendOpTypeToBuilder(_opType, &bob);
    bob.append("infoObj", _infoObj);

    return bob.obj();
}

BSONObj RollbackFixUpInfo::IndexDescription::makeIdKey() const {
    BSONObjBuilder idBob;
    _collectionUuid.appendToBuilder(&idBob, "collectionUuid");
    idBob.append("indexName", _indexName);
    return idBob.obj();
}

}  // namespace repl

std::ostream& operator<<(std::ostream& os, const repl::RollbackFixUpInfo::IndexOpType& opType) {
    return os << repl::toString(opType);
}

}  // namespace mongo