summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/change_stream_expired_pre_image_remover.cpp
blob: 75fea08ded7b13287f622658e4a6ba4b57f52044 (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) 2021-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    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 Server Side 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_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery

#include "mongo/platform/basic.h"

#include "change_stream_expired_pre_image_remover.h"

#include "mongo/db/client.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/pipeline/change_stream_preimage_gen.h"
#include "mongo/db/query/internal_plans.h"
#include "mongo/db/record_id_helpers.h"
#include "mongo/db/repl/oplog_entry_gen.h"
#include "mongo/db/repl/storage_interface.h"
#include "mongo/db/service_context.h"
#include "mongo/logv2/log.h"

namespace mongo {

namespace {
/**
 * Scans the 'config.system.preimages' collection and deletes the expired pre-images from it.
 *
 * Pre-images are ordered by collection UUID, ie. if UUID of collection A is ordered before UUID of
 * collection B, then pre-images of collection A will be stored before pre-images of collection B.
 *
 * While scanning the collection for expired pre-images, each pre-image timestamp is compared
 * against the 'earliestOplogEntryTimestamp' value. Any pre-image that has a timestamp greater than
 * the 'earliestOplogEntryTimestamp' value is not considered for deletion and the cursor seeks to
 * the next UUID in the collection.
 *
 * Seek to the next UUID is done by setting the values of 'Timestamp' and 'ApplyOpsIndex' fields to
 * max, ie. (currentPreImage.nsUUID, Timestamp::max(), ApplyOpsIndex::max()).
 *
 *                               +-------------------------+
 *                               | config.system.preimages |
 *                               +------------+------------+
 *                                            |
 *             +--------------------+---------+---------+-----------------------+
 *             |                    |                   |                       |
 * +-----------+-------+ +----------+--------+ +--------+----------+ +----------+--------+
 * |  collA.preImageA  | |  collA.preImageB  | |  collB.preImageC  | |  collB.preImageD  |
 * +-----------+-------+ +----------+--------+ +---------+---------+ +----------+--------+
 * |   timestamp: 1    | |   timestamp: 10   | |   timestamp: 5    | |   timestamp: 9    |
 * |   applyIndex: 0   | |   applyIndex: 0   | |   applyIndex: 0   | |   applyIndex: 1   |
 * +-------------------+ +-------------------+ +-------------------+ +-------------------+
 */
class ChangeStreamExpiredPreImageIterator {
public:
    // Iterator over the expired pre-images.
    struct Iterator {
        using iterator_category = std::forward_iterator_tag;
        using difference_type = void;
        using value_type = BSONObj;
        using pointer = const BSONObj*;
        using reference = const BSONObj&;

        Iterator(OperationContext* opCtx,
                 const CollectionPtr* preImagesCollPtr,
                 Timestamp earliestOplogEntryTimestamp,
                 boost::optional<ChangeStreamPreImageId> minPreImageId = boost::none)
            : _opCtx(opCtx),
              _preImagesCollPtr(preImagesCollPtr),
              _earliestOplogEntryTimestamp(earliestOplogEntryTimestamp) {
            setupPlanExecutor(minPreImageId);
            advance();
        }

        reference operator*() const {
            return _currentPreImageObj;
        }

        pointer operator->() const {
            return &_currentPreImageObj;
        }

        Iterator& operator++() {
            advance();
            return *this;
        }

        // Both iterators are equal if they are both at the same pre-image.
        friend bool operator==(const Iterator& a, const Iterator& b) {
            return a._currentPreImageObj.woCompare(b._currentPreImageObj) == 0;
        };

        friend bool operator!=(const Iterator& a, const Iterator& b) {
            return !(a == b);
        };

        void saveState() {
            _planExecutor->saveState();
        }

        void restoreState() {
            _planExecutor->restoreState(_preImagesCollPtr);
        }

    private:
        // Scans the pre-images collection and gets the next expired pre-image or sets
        // 'currentPreImage' to BSONObj() in case there are no more expired pre-images left.
        void advance() {
            const auto getNextPreImage = [&]() -> boost::optional<ChangeStreamPreImage> {
                if (_planExecutor->getNext(&_currentPreImageObj, nullptr) == PlanExecutor::IS_EOF) {
                    _currentPreImageObj = BSONObj();
                    return boost::none;
                }

                return {ChangeStreamPreImage::parse(IDLParserErrorContext("pre-image"),
                                                    _currentPreImageObj)};
            };

            // If current collection has no more expired pre-images, fetch the first pre-image from
            // the next collection that has pre-images enabled.
            boost::optional<ChangeStreamPreImage> preImage;
            while ((preImage = getNextPreImage()) && !isExpiredPreImage(*preImage)) {
                // Set the maximum values for timestamp and apply ops fields such that we jump to
                // the next collection that has the pre-images enabled.
                preImage->getId().setTs(Timestamp::max());
                preImage->getId().setApplyOpsIndex(std::numeric_limits<int64_t>::max());
                setupPlanExecutor(preImage->getId());
            }
        }

        // Pre-image is expired if its timestamp is smaller than the 'earliestOplogEntryTimestamp'
        bool isExpiredPreImage(const ChangeStreamPreImage& preImage) const {
            return preImage.getId().getTs() < _earliestOplogEntryTimestamp;
        }

        // Set up the new collection scan to start from the 'minPreImageId'.
        void setupPlanExecutor(boost::optional<ChangeStreamPreImageId> minPreImageId) {
            const auto minRecordId =
                (minPreImageId ? boost::optional<RecordId>(record_id_helpers::keyForElem(
                                     BSON("_id" << minPreImageId->toBSON()).firstElement()))
                               : boost::none);
            _planExecutor =
                InternalPlanner::collectionScan(_opCtx,
                                                _preImagesCollPtr,
                                                PlanYieldPolicy::YieldPolicy::YIELD_AUTO,
                                                InternalPlanner::Direction::FORWARD,
                                                boost::none,
                                                minRecordId);
        }

        OperationContext* _opCtx;
        const CollectionPtr* _preImagesCollPtr;
        BSONObj _currentPreImageObj;
        Timestamp _earliestOplogEntryTimestamp;
        std::unique_ptr<PlanExecutor, PlanExecutor::Deleter> _planExecutor;
    };

    ChangeStreamExpiredPreImageIterator(OperationContext* opCtx,
                                        const CollectionPtr* preImagesCollPtr,
                                        Timestamp earliestOplogEntryTimestamp)
        : _opCtx(opCtx),
          _preImagesCollPtr(preImagesCollPtr),
          _earliestOplogEntryTimestamp(earliestOplogEntryTimestamp) {}

    Iterator begin() const {
        return Iterator(_opCtx, _preImagesCollPtr, _earliestOplogEntryTimestamp);
    }

    Iterator end() const {
        // For end iterator the collection scan 'minRecordId' has to be maximum pre-image id.
        const auto maxUUID = UUID::parse("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF").getValue();
        ChangeStreamPreImageId maxPreImageId(
            maxUUID, Timestamp::max(), std::numeric_limits<int64_t>::max());
        return Iterator(_opCtx, _preImagesCollPtr, _earliestOplogEntryTimestamp, maxPreImageId);
    }

private:
    OperationContext* _opCtx;
    const CollectionPtr* _preImagesCollPtr;
    Timestamp _earliestOplogEntryTimestamp;
};

void deleteExpiredChangeStreamPreImages(Client* client) {
    const auto startTime = Date_t::now();
    auto opCtx = client->makeOperationContext();

    // Acquire intent-exclusive lock on the pre-images collection. Early exit if the collection
    // doesn't exist.
    AutoGetCollection autoColl(
        opCtx.get(), NamespaceString::kChangeStreamPreImagesNamespace, MODE_IX);
    const auto& preImagesColl = autoColl.getCollection();
    if (!preImagesColl) {
        return;
    }

    // Do not run the job on secondaries.
    const auto isPrimary = repl::ReplicationCoordinator::get(opCtx.get())
                               ->canAcceptWritesForDatabase(opCtx.get(), NamespaceString::kAdminDb);
    if (!isPrimary) {
        return;
    }

    // Get the timestamp of the ealiest oplog entry.
    const auto currentEarliestOplogEntryTs =
        repl::StorageInterface::get(client->getServiceContext())
            ->getEarliestOplogTimestamp(opCtx.get());

    // Iterate over all expired pre-images and remove them.
    size_t numberOfRemovals = 0;
    ChangeStreamExpiredPreImageIterator expiredPreImages(
        opCtx.get(), &preImagesColl, currentEarliestOplogEntryTs);
    // TODO SERVER-58693: consider adopting a recordID range-based deletion policy instead of
    // iterating.
    for (auto it = expiredPreImages.begin(); it != expiredPreImages.end(); ++it) {
        it.saveState();

        writeConflictRetry(
            opCtx.get(),
            "ChangeStreamExpiredPreImagesRemover",
            NamespaceString::kChangeStreamPreImagesNamespace.ns(),
            [&] {
                WriteUnitOfWork wuow(opCtx.get());
                const auto recordId =
                    record_id_helpers::keyForElem(it->getField(ChangeStreamPreImage::kIdFieldName));
                preImagesColl->deleteDocument(
                    opCtx.get(), kUninitializedStmtId, recordId, &CurOp::get(*opCtx)->debug());
                wuow.commit();
                numberOfRemovals++;
            });

        it.restoreState();
    }

    LOGV2(5869104,
          "Periodic expired pre-images removal job finished executing",
          "numberOfRemovals"_attr = numberOfRemovals,
          "jobDuration"_attr = (Date_t::now() - startTime).toString());
}
}  // namespace

PeriodicChangeStreamExpiredPreImagesRemover& PeriodicChangeStreamExpiredPreImagesRemover::get(
    ServiceContext* serviceContext) {
    auto& jobContainer = _serviceDecoration(serviceContext);
    jobContainer._init(serviceContext);
    return jobContainer;
}

PeriodicJobAnchor& PeriodicChangeStreamExpiredPreImagesRemover::operator*() const noexcept {
    stdx::lock_guard lk(_mutex);
    return *_anchor;
}

PeriodicJobAnchor* PeriodicChangeStreamExpiredPreImagesRemover::operator->() const noexcept {
    stdx::lock_guard lk(_mutex);
    return _anchor.get();
}

void PeriodicChangeStreamExpiredPreImagesRemover::_init(ServiceContext* serviceContext) {
    stdx::lock_guard lk(_mutex);
    if (_anchor) {
        return;
    }

    auto periodicRunner = serviceContext->getPeriodicRunner();
    invariant(periodicRunner);

    PeriodicRunner::PeriodicJob job(
        "ChangeStreamExpiredPreImagesRemover",
        [](Client* client) {
            try {
                deleteExpiredChangeStreamPreImages(client);
            } catch (const ExceptionForCat<ErrorCategory::Interruption>&) {
                LOGV2_WARNING(5869105, "Periodic expired pre-images removal job was interrupted");
            } catch (const DBException& exception) {
                LOGV2_ERROR(5869106,
                            "Periodic expired pre-images removal job failed",
                            "reason"_attr = exception.reason());
            }
        },
        Seconds(gExpiredChangeStreamPreImageRemovalJobSleepSecs.load()));

    _anchor = std::make_shared<PeriodicJobAnchor>(periodicRunner->makeJob(std::move(job)));
}

}  // namespace mongo