summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/dbhelper_tests.cpp
blob: bdfd223e8d37da954b8a2196685496081dd6260f (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
/**
 *    Copyright (C) 2018-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.
 */

#include "mongo/platform/basic.h"

#include "mongo/client/dbclient_cursor.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/catalog/database_holder.h"
#include "mongo/db/client.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/dbhelpers.h"
#include "mongo/db/global_settings.h"
#include "mongo/db/op_observer_impl.h"
#include "mongo/db/op_observer_registry.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/range_arithmetic.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/repl/replication_coordinator_mock.h"
#include "mongo/db/write_concern_options.h"
#include "mongo/dbtests/dbtests.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"

namespace mongo {

namespace {

using std::set;
using std::unique_ptr;

/**
 * Unit tests related to DBHelpers
 */

static const char* const ns = "unittests.removetests";

// TODO: Normalize with test framework
/** Simple test for Helpers::RemoveRange. */
class RemoveRange {
public:
    RemoveRange() : _min(4), _max(8) {}

    void run() {}

private:
    BSONArray expected() const {
        BSONArrayBuilder bab;
        for (int i = 0; i < _min; ++i) {
            bab << BSON("_id" << i);
        }
        for (int i = _max; i < 10; ++i) {
            bab << BSON("_id" << i);
        }
        return bab.arr();
    }

    BSONArray docs(OperationContext* opCtx) const {
        DBDirectClient client(opCtx);
        FindCommandRequest findRequest{NamespaceString{ns}};
        findRequest.setHint(BSON("_id" << 1));
        std::unique_ptr<DBClientCursor> cursor = client.find(std::move(findRequest));
        BSONArrayBuilder bab;
        while (cursor->more()) {
            bab << cursor->next();
        }
        return bab.arr();
    }
    int _min;
    int _max;
};

class FindAndNoopUpdateTest {
public:
    void run() {
        auto serviceContext = getGlobalServiceContext();

        repl::ReplSettings replSettings;
        replSettings.setOplogSizeBytes(10 * 1024 * 1024);
        replSettings.setReplSetString("rs");
        setGlobalReplSettings(replSettings);
        auto coordinatorMock = new repl::ReplicationCoordinatorMock(serviceContext, replSettings);
        _coordinatorMock = coordinatorMock;
        coordinatorMock->alwaysAllowWrites(true);
        repl::ReplicationCoordinator::set(
            serviceContext, std::unique_ptr<repl::ReplicationCoordinator>(coordinatorMock));

        NamespaceString nss("test.findandnoopupdate");

        auto client1 = serviceContext->makeClient("client1");
        auto opCtx1 = client1->makeOperationContext();

        auto client2 = serviceContext->makeClient("client2");
        auto opCtx2 = client2->makeOperationContext();

        auto registry = std::make_unique<OpObserverRegistry>();
        registry->addObserver(std::make_unique<OpObserverImpl>());
        opCtx1.get()->getServiceContext()->setOpObserver(std::move(registry));
        repl::createOplog(opCtx1.get());

        Lock::DBLock dbLk1(opCtx1.get(), nss.dbName(), LockMode::MODE_IX);
        Lock::CollectionLock collLk1(opCtx1.get(), nss, LockMode::MODE_IX);

        Lock::DBLock dbLk2(opCtx2.get(), nss.dbName(), LockMode::MODE_IX);
        Lock::CollectionLock collLk2(opCtx2.get(), nss, LockMode::MODE_IX);

        Database* db =
            DatabaseHolder::get(opCtx1.get())->openDb(opCtx1.get(), nss.dbName(), nullptr);

        // Create the collection and insert one doc
        BSONObj doc = BSON("_id" << 1 << "x" << 2);
        BSONObj idQuery = BSON("_id" << 1);

        CollectionPtr collection1;
        {
            WriteUnitOfWork wuow(opCtx1.get());
            collection1 = db->createCollection(opCtx1.get(), nss, CollectionOptions(), true);
            ASSERT_TRUE(collection1 != nullptr);
            ASSERT_TRUE(collection1
                            ->insertDocument(
                                opCtx1.get(), InsertStatement(doc), nullptr /* opDebug */, false)
                            .isOK());
            wuow.commit();
        }

        BSONObj result;
        Helpers::findById(opCtx1.get(), nss.ns(), idQuery, result, nullptr, nullptr);
        ASSERT_BSONOBJ_EQ(result, doc);

        // Assert that the same doc still exists after findByIdAndNoopUpdate
        {
            WriteUnitOfWork wuow(opCtx1.get());
            BSONObj res;
            auto lastApplied = repl::ReplicationCoordinator::get(opCtx1->getServiceContext())
                                   ->getMyLastAppliedOpTime()
                                   .getTimestamp();
            ASSERT_OK(opCtx1->recoveryUnit()->setTimestamp(lastApplied + 1));
            auto foundDoc = Helpers::findByIdAndNoopUpdate(opCtx1.get(), collection1, idQuery, res);
            wuow.commit();
            ASSERT_TRUE(foundDoc);
            ASSERT_BSONOBJ_EQ(res, doc);
        }

        // Assert that findByIdAndNoopUpdate did not generate an oplog entry.
        BSONObj oplogEntry;
        Helpers::getLast(opCtx1.get(), NamespaceString::kRsOplogNamespace, oplogEntry);
        ASSERT_BSONOBJ_NE(oplogEntry, BSONObj());
        ASSERT_TRUE(oplogEntry.getStringField("op") == "i"_sd);

        // Run two concurrent storage transactions. Run findByIdAndNoopUpdate in one, and then
        // attempt to delete all docs in the collection in the other. Assert that the delete op
        // throws a WCE.
        assertWriteAttemptAfterFindAndNoopUpdateThrowsWCE(
            opCtx1.get(), opCtx2.get(), nss, db, doc, idQuery);

        // Run two concurrent storage transactions. Run a delete op to remove all documents in the
        // collection in one, and then attempt to run findByIdAndNoopUpdate in the second. Assert
        // that findByIdAndNoopUpdate throws WCE.
        assertFindAndNoopUpdateAfterWriteThrowsWCE(
            opCtx1.get(), opCtx2.get(), nss, db, doc, idQuery);
    }

private:
    void assertWriteAttemptAfterFindAndNoopUpdateThrowsWCE(OperationContext* opCtx1,
                                                           OperationContext* opCtx2,
                                                           const NamespaceString& nss,
                                                           Database* db,
                                                           const BSONObj& doc,
                                                           const BSONObj& idQuery) {
        {
            WriteUnitOfWork wuow1(opCtx1);

            WriteUnitOfWork wuow2(opCtx2);
            auto collection2 =
                CollectionCatalog::get(opCtx2)->lookupCollectionByNamespace(opCtx2, nss);
            ASSERT(collection2 != nullptr);
            auto lastApplied = repl::ReplicationCoordinator::get(opCtx2->getServiceContext())
                                   ->getMyLastAppliedOpTime()
                                   .getTimestamp();
            ASSERT_OK(opCtx2->recoveryUnit()->setTimestamp(lastApplied + 1));
            BSONObj res;
            ASSERT_TRUE(Helpers::findByIdAndNoopUpdate(opCtx2, collection2, idQuery, res));

            ASSERT_THROWS(Helpers::emptyCollection(opCtx1, nss), WriteConflictException);

            wuow2.commit();
        }

        // Assert that the doc still exists in the collection.
        BSONObj res1;
        Helpers::findById(opCtx1, nss.ns(), idQuery, res1, nullptr, nullptr);
        ASSERT_BSONOBJ_EQ(res1, doc);

        BSONObj res2;
        Helpers::findById(opCtx2, nss.ns(), idQuery, res2, nullptr, nullptr);
        ASSERT_BSONOBJ_EQ(res2, doc);

        // Assert that findByIdAndNoopUpdate did not generate an oplog entry.
        BSONObj oplogEntry;
        Helpers::getLast(opCtx2, NamespaceString::kRsOplogNamespace, oplogEntry);
        ASSERT_BSONOBJ_NE(oplogEntry, BSONObj());
        ASSERT_TRUE(oplogEntry.getStringField("op") == "i"_sd);
    }

    void assertFindAndNoopUpdateAfterWriteThrowsWCE(OperationContext* opCtx1,
                                                    OperationContext* opCtx2,
                                                    const NamespaceString& nss,
                                                    Database* db,
                                                    const BSONObj& doc,
                                                    const BSONObj& idQuery) {
        {
            WriteUnitOfWork wuow1(opCtx1);
            auto lastApplied = repl::ReplicationCoordinator::get(opCtx1->getServiceContext())
                                   ->getMyLastAppliedOpTime()
                                   .getTimestamp();
            ASSERT_OK(opCtx1->recoveryUnit()->setTimestamp(lastApplied + 1));
            Helpers::emptyCollection(opCtx1, nss);

            {
                WriteUnitOfWork wuow2(opCtx2);
                auto collection2 =
                    CollectionCatalog::get(opCtx2)->lookupCollectionByNamespace(opCtx2, nss);
                ASSERT(collection2 != nullptr);

                BSONObj res;
                ASSERT_THROWS(Helpers::findByIdAndNoopUpdate(opCtx2, collection2, idQuery, res),
                              WriteConflictException);
            }

            wuow1.commit();
        }

        // Assert that the first storage transaction succeeded and that the doc is removed.
        BSONObj res1;
        Helpers::findById(opCtx1, nss.ns(), idQuery, res1, nullptr, nullptr);
        ASSERT_BSONOBJ_EQ(res1, BSONObj());

        BSONObj res2;
        Helpers::findById(opCtx2, nss.ns(), idQuery, res2, nullptr, nullptr);
        ASSERT_BSONOBJ_EQ(res2, BSONObj());
    }

    repl::ReplicationCoordinatorMock* _coordinatorMock;
};

class All : public OldStyleSuiteSpecification {
public:
    All() : OldStyleSuiteSpecification("dbhelpers") {}
    void setupTests() {
        add<RemoveRange>();
        add<FindAndNoopUpdateTest>();
    }
};

OldStyleSuiteInitializer<All> myall;

}  // namespace
}  // namespace mongo