summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/throttle_cursor_test.cpp
blob: 8de3f08fbebff458c188d43fb2e6052b297d7515 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
 *    Copyright (C) 2019-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/db/catalog/throttle_cursor.h"

#include "mongo/db/catalog/catalog_test_fixture.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/catalog/index_catalog_entry.h"
#include "mongo/db/catalog/validate_gen.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/index/index_access_method.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/clock_source_mock.h"
#include "mongo/util/time_support.h"

namespace mongo {

namespace {

const NamespaceString kNss = NamespaceString("test.throttleCursor");
const uint8_t kTickDelay = 200;

class ThrottleCursorTest : public CatalogTestFixture {
private:
    void setUp() override;
    void tearDown() override;

protected:
    const KeyString::Value kMinKeyString = KeyString::Builder{KeyString::Version::kLatestVersion,
                                                              kMinBSONKey,
                                                              KeyString::ALL_ASCENDING}
                                               .getValueCopy();

    explicit ThrottleCursorTest(Milliseconds clockIncrement = Milliseconds{kTickDelay})
        : CatalogTestFixture(Options{}.useMockClock(true, clockIncrement)) {}

public:
    void setMaxMbPerSec(int maxMbPerSec);

    Date_t getTime();
    int64_t getDifferenceInMillis(Date_t start, Date_t end);
    SortedDataInterfaceThrottleCursor getIdIndex(const CollectionPtr& coll);

    std::unique_ptr<DataThrottle> _dataThrottle;
};

class ThrottleCursorTestFastClock : public ThrottleCursorTest {
protected:
    // Move the clock faster to speed up the test.
    ThrottleCursorTestFastClock() : ThrottleCursorTest(Milliseconds{1000}) {}
};

void ThrottleCursorTest::setUp() {
    CatalogTestFixture::setUp();
    CollectionOptions defaultCollectionOptions;
    ASSERT_OK(
        storageInterface()->createCollection(operationContext(), kNss, defaultCollectionOptions));

    // Insert random data into the collection. We don't need to create an index as the _id index is
    // created by default.
    AutoGetCollection collection(operationContext(), kNss, MODE_X);
    invariant(collection);

    OpDebug* const nullOpDebug = nullptr;
    for (int i = 0; i < 10; i++) {
        WriteUnitOfWork wuow(operationContext());

        ASSERT_OK(collection->insertDocument(
            operationContext(), InsertStatement(BSON("_id" << i)), nullOpDebug));
        wuow.commit();
    }

    _dataThrottle = std::make_unique<DataThrottle>(operationContext());
}

void ThrottleCursorTest::tearDown() {
    CatalogTestFixture::tearDown();
}

void ThrottleCursorTest::setMaxMbPerSec(int maxMbPerSec) {
    gMaxValidateMBperSec.store(maxMbPerSec);
}

Date_t ThrottleCursorTest::getTime() {
    return operationContext()->getServiceContext()->getFastClockSource()->now();
}

int64_t ThrottleCursorTest::getDifferenceInMillis(Date_t start, Date_t end) {
    return end.toMillisSinceEpoch() - start.toMillisSinceEpoch();
}

SortedDataInterfaceThrottleCursor ThrottleCursorTest::getIdIndex(const CollectionPtr& coll) {
    const IndexDescriptor* idDesc = coll->getIndexCatalog()->findIdIndex(operationContext());
    const IndexCatalogEntry* idEntry = coll->getIndexCatalog()->getEntry(idDesc);
    auto iam = idEntry->accessMethod()->asSortedData();

    return SortedDataInterfaceThrottleCursor(operationContext(), iam, _dataThrottle.get());
}

TEST_F(ThrottleCursorTest, TestSeekableRecordThrottleCursorOff) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf512KBForDataThrottle");

    SeekableRecordThrottleCursor cursor =
        SeekableRecordThrottleCursor(opCtx, coll->getRecordStore(), _dataThrottle.get());

    // With the data throttle off, all operations should finish within a second.
    setMaxMbPerSec(0);
    Date_t start = getTime();

    int numRecords = 0;

    while (cursor.next(opCtx)) {
        numRecords++;
    }

    int64_t recordId = 1;
    while (cursor.seekExact(opCtx, RecordId(recordId))) {
        recordId++;
        numRecords++;
    }

    Date_t end = getTime();

    ASSERT_EQ(numRecords, 20);
    ASSERT_EQ(getDifferenceInMillis(start, end), kTickDelay * numRecords + kTickDelay);
}

TEST_F(ThrottleCursorTest, TestSeekableRecordThrottleCursorOn) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf512KBForDataThrottle");

    SeekableRecordThrottleCursor cursor =
        SeekableRecordThrottleCursor(opCtx, coll->getRecordStore(), _dataThrottle.get());

    // Using a throttle with a limit of 1MB per second, all operations should take at least 5
    // seconds to finish. We have 10 records, each of which is 0.5MB courtesy of the fail point, so
    // 2 records per second.
    {
        setMaxMbPerSec(1);
        Date_t start = getTime();

        ASSERT_TRUE(cursor.seekExact(opCtx, RecordId(1)));
        int numRecords = 1;

        while (cursor.next(opCtx)) {
            numRecords++;
        }

        Date_t end = getTime();

        ASSERT_EQ(numRecords, 10);
        ASSERT_TRUE(getDifferenceInMillis(start, end) >= 5000);
    }

    // Using a throttle with a limit of 5MB per second, all operations should take at least 1
    // second to finish. We have 10 records, each of which is 0.5MB courtesy of the fail point, so
    // 10 records per second.
    {
        setMaxMbPerSec(5);
        Date_t start = getTime();

        ASSERT_TRUE(cursor.seekExact(opCtx, RecordId(1)));
        int numRecords = 1;

        while (cursor.next(opCtx)) {
            numRecords++;
        }

        Date_t end = getTime();

        ASSERT_EQ(numRecords, 10);
        ASSERT_TRUE(getDifferenceInMillis(start, end) >= 1000);
    }
}

TEST_F(ThrottleCursorTestFastClock, TestSeekableRecordThrottleCursorOnLargeDocs1MBps) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf2MBForDataThrottle");

    SeekableRecordThrottleCursor cursor =
        SeekableRecordThrottleCursor(opCtx, coll->getRecordStore(), _dataThrottle.get());

    // Using a throttle with a limit of 1MB per second, all operations should take at least 10
    // seconds to finish. We scan 5 records, each of which is 2MB courtesy of the fail point, so
    // 1 record every 2 seconds.
    setMaxMbPerSec(1);
    Date_t start = getTime();

    // Seek to the first record, then iterate through 4 more.
    ASSERT_TRUE(cursor.seekExact(opCtx, RecordId(1)));
    int scanRecords = 4;

    while (scanRecords > 0 && cursor.next(opCtx)) {
        scanRecords--;
    }

    Date_t end = getTime();

    ASSERT_EQ(scanRecords, 0);
    ASSERT_GTE(getDifferenceInMillis(start, end), 10 * 1000);
}

TEST_F(ThrottleCursorTest, TestSeekableRecordThrottleCursorOnLargeDocs5MBps) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf2MBForDataThrottle");

    SeekableRecordThrottleCursor cursor =
        SeekableRecordThrottleCursor(opCtx, coll->getRecordStore(), _dataThrottle.get());

    // Using a throttle with a limit of 5MB per second, all operations should take at least 2
    // second to finish. We scan 5 records, each of which is 2MB courtesy of the fail point, so
    // 2.5 records per second.
    setMaxMbPerSec(5);
    Date_t start = getTime();

    // Seek to the first record, then iterate through 4 more.
    ASSERT_TRUE(cursor.seekExact(opCtx, RecordId(1)));
    int scanRecords = 4;

    while (scanRecords > 0 && cursor.next(opCtx)) {
        scanRecords--;
    }

    Date_t end = getTime();

    ASSERT_EQ(scanRecords, 0);
    ASSERT_GTE(getDifferenceInMillis(start, end), 2000);
}

TEST_F(ThrottleCursorTest, TestSortedDataInterfaceThrottleCursorOff) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf512KBForDataThrottle");

    SortedDataInterfaceThrottleCursor cursor = getIdIndex(coll);

    // With the data throttle off, all operations should finish within a second.
    setMaxMbPerSec(0);
    Date_t start = getTime();

    ASSERT_TRUE(cursor.seek(opCtx, kMinKeyString));
    int numRecords = 1;

    while (cursor.next(opCtx)) {
        numRecords++;
    }

    Date_t end = getTime();

    ASSERT_EQ(numRecords, 10);
    ASSERT_EQ(getDifferenceInMillis(start, end), kTickDelay * numRecords + kTickDelay);
}

TEST_F(ThrottleCursorTest, TestSortedDataInterfaceThrottleCursorOn) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf512KBForDataThrottle");

    SortedDataInterfaceThrottleCursor cursor = getIdIndex(coll);

    // Using a throttle with a limit of 1MB per second, all operations should take at least 5
    // seconds to finish. We have 10 records, each of which is 0.5MB courtesy of the fail point, so
    // 2 records per second.
    {
        setMaxMbPerSec(1);
        Date_t start = getTime();

        ASSERT_TRUE(cursor.seek(opCtx, kMinKeyString));
        int numRecords = 1;

        while (cursor.next(opCtx)) {
            numRecords++;
        }

        Date_t end = getTime();

        ASSERT_EQ(numRecords, 10);
        ASSERT_TRUE(getDifferenceInMillis(start, end) >= 5000);
    }

    // Using a throttle with a limit of 5MB per second, all operations should take at least 1
    // second to finish. We have 10 records, each of which is 0.5MB courtesy of the fail point, so
    // 10 records per second.
    {
        setMaxMbPerSec(5);
        Date_t start = getTime();

        ASSERT_TRUE(cursor.seek(opCtx, kMinKeyString));
        int numRecords = 1;

        while (cursor.next(opCtx)) {
            numRecords++;
        }

        Date_t end = getTime();

        ASSERT_EQ(numRecords, 10);
        ASSERT_TRUE(getDifferenceInMillis(start, end) >= 1000);
    }
}

TEST_F(ThrottleCursorTest, TestMixedCursorsWithSharedThrottleOff) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf512KBForDataThrottle");

    SeekableRecordThrottleCursor recordCursor =
        SeekableRecordThrottleCursor(opCtx, coll->getRecordStore(), _dataThrottle.get());

    SortedDataInterfaceThrottleCursor indexCursor = getIdIndex(coll);

    // With the data throttle off, all operations should finish within a second, regardless if
    // the 'maxValidateMBperSec' server parameter is set.
    _dataThrottle->turnThrottlingOff();
    setMaxMbPerSec(10);
    Date_t start = getTime();

    ASSERT_TRUE(indexCursor.seek(opCtx, kMinKeyString));
    int numRecords = 1;

    while (indexCursor.next(opCtx)) {
        numRecords++;
    }

    while (recordCursor.next(opCtx)) {
        numRecords++;
    }

    int64_t recordId = 1;
    while (recordCursor.seekExact(opCtx, RecordId(recordId))) {
        recordId++;
        numRecords++;
    }

    Date_t end = getTime();

    ASSERT_EQ(numRecords, 30);
    ASSERT_EQ(getDifferenceInMillis(start, end), kTickDelay * numRecords + kTickDelay);
}

TEST_F(ThrottleCursorTest, TestMixedCursorsWithSharedThrottleOn) {
    auto opCtx = operationContext();
    AutoGetCollection autoColl(opCtx, kNss, MODE_X);
    const CollectionPtr& coll = autoColl.getCollection();

    // Use a fixed record data size to simplify the timing calculations.
    FailPointEnableBlock failPoint("fixedCursorDataSizeOf512KBForDataThrottle");

    SeekableRecordThrottleCursor recordCursor =
        SeekableRecordThrottleCursor(opCtx, coll->getRecordStore(), _dataThrottle.get());

    SortedDataInterfaceThrottleCursor indexCursor = getIdIndex(coll);

    // Using a throttle with a limit of 2MB per second, all operations should take at least 5
    // seconds to finish. We have 20 records, each of which is 0.5MB courtesy of the fail point, so
    // 4 records per second.
    {
        setMaxMbPerSec(2);
        Date_t start = getTime();

        ASSERT_TRUE(indexCursor.seek(opCtx, kMinKeyString));
        ASSERT_TRUE(recordCursor.seekExact(opCtx, RecordId(1)));
        int numRecords = 2;

        while (indexCursor.next(opCtx)) {
            ASSERT_TRUE(recordCursor.next(opCtx));
            numRecords += 2;
        }

        Date_t end = getTime();

        ASSERT_EQ(numRecords, 20);
        ASSERT_TRUE(getDifferenceInMillis(start, end) >= 5000);
    }

    // Using a throttle with a limit of 5MB per second, all operations should take at least 2
    // seconds to finish. We have 20 records, each of which is 0.5MB courtesy of the fail point, so
    // 10 records per second.
    {
        setMaxMbPerSec(5);
        Date_t start = getTime();

        ASSERT_TRUE(indexCursor.seek(opCtx, kMinKeyString));
        ASSERT_TRUE(recordCursor.seekExact(opCtx, RecordId(1)));
        int numRecords = 2;

        while (indexCursor.next(opCtx)) {
            ASSERT_TRUE(recordCursor.next(opCtx));
            numRecords += 2;
        }

        Date_t end = getTime();

        ASSERT_EQ(numRecords, 20);
        ASSERT_TRUE(getDifferenceInMillis(start, end) >= 2000);
    }
}

}  // namespace

}  // namespace mongo