summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/tenant_oplog_batcher_test.cpp
blob: 47d02f9084a815d85b76993df6815155c46d354a (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/**
 *    Copyright (C) 2020-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::kTest

#include "mongo/platform/basic.h"

#include "mongo/db/repl/oplog_batcher_test_fixture.h"
#include "mongo/db/repl/tenant_oplog_batcher.h"
#include "mongo/db/service_context_test_fixture.h"
#include "mongo/executor/thread_pool_task_executor_test_fixture.h"
#include "mongo/logv2/log.h"
#include "mongo/unittest/log_test.h"

namespace mongo {

using executor::TaskExecutor;
using executor::ThreadPoolExecutorTest;

namespace repl {

class TenantOplogBatcherTest : public unittest::Test, public ScopedGlobalServiceContextForTest {
public:
    void setUp() override {
        unittest::Test::setUp();
        Client::initThread("TenantOplogBatcherTest");
        auto network = std::make_unique<executor::NetworkInterfaceMock>();
        _net = network.get();
        executor::ThreadPoolMock::Options thread_pool_options;
        thread_pool_options.onCreateThread = [] { Client::initThread("TenantOplogBatcher"); };
        _executor = makeSharedThreadPoolTestExecutor(std::move(network), thread_pool_options);
        _executor->startup();
        _oplogBuffer.startup(nullptr);
    }

    void tearDown() override {
        _executor->shutdown();
        _executor->join();
        Client::releaseCurrent();
    }

protected:
    TenantOplogBatcher::BatchLimits bigBatchLimits =
        TenantOplogBatcher::BatchLimits(1ULL << 32, 1ULL << 32);
    OplogBufferMock _oplogBuffer;
    executor::NetworkInterfaceMock* _net;
    std::shared_ptr<executor::ThreadPoolTaskExecutor> _executor;

private:
    unittest::MinimumLoggedSeverityGuard _replicationSeverityGuard{
        logv2::LogComponent::kReplication, logv2::LogSeverity::Debug(1)};
};

/**
 * Returns string representation of std::vector<TenantOplogEntry>.
 */
std::string toString(const std::vector<TenantOplogEntry>& ops) {
    StringBuilder sb;
    sb << "[";
    for (const auto& op : ops) {
        sb << " " << op.entry.toStringForLogging() << "(" << op.expansionsEntry << ")";
    }
    sb << " ]";
    return sb.str();
}
std::string toString(TenantOplogBatch& batch) {
    StringBuilder sb;
    sb << toString(batch.ops);
    for (const auto& txn : batch.expansions) {
        sb << "\n" << toString(txn);
    }
    return sb.str();
}

constexpr auto dbName = "tenant_test"_sd;

TEST_F(TenantOplogBatcherTest, CannotRequestTwoBatchesAtOnce) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    // We just started, no batch should be available.
    ASSERT(!batchFuture.isReady());
    // Can't ask for the next batch until the current batch is done.
    ASSERT_THROWS(batcher->getNextBatch(bigBatchLimits), AssertionException);

    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherGroupsCrudOps) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    // We just started, no batch should be available.
    ASSERT(!batchFuture.isReady());
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeInsertOplogEntry(1, NamespaceString(dbName, "foo")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "bar")).getEntry().toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    auto batch = batchFuture.get();
    batcher->shutdown();

    ASSERT_EQUALS(srcOps.size(), batch.ops.size()) << toString(batch);
    ASSERT(batch.expansions.empty());
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[1].expansionsEntry);

    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherFailsOnPreparedApplyOps) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);

    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeApplyOpsOplogEntry(1, true).getEntry().toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());
    ASSERT_THROWS(batchFuture.get(), AssertionException);

    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherFailsOnPreparedCommit) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);

    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeCommitTransactionOplogEntry(1, dbName, true /* prepared*/, 1 /* count */)
                         .getEntry()
                         .toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());
    ASSERT_THROWS(batchFuture.get(), AssertionException);

    batcher->shutdown();
    batcher->join();
}

// We internally add the 'b' field during applyOps expansion; we need to remove it when we check to
// see that the expansion matches the expected test values input.
static DurableReplOperation stripB(const DurableReplOperation& withB) {
    DurableReplOperation withoutB(withB);
    withoutB.setUpsert(boost::none);
    return withoutB;
}

TEST_F(TenantOplogBatcherTest, GetNextApplierBatchGroupsUnpreparedApplyOpsOpWithOtherOps) {
    std::vector<OplogEntry> innerOps;
    std::vector<BSONObj> srcOps;
    innerOps.push_back(makeInsertOplogEntry(10, NamespaceString(dbName, "foo")));
    innerOps.push_back(makeInsertOplogEntry(11, NamespaceString(dbName, "foo")));
    srcOps.push_back(makeApplyOpsOplogEntry(1, false, innerOps).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "bar")).getEntry().toBSON());

    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());
    auto batch = batchFuture.get();
    batcher->shutdown();

    ASSERT_EQUALS(2, batch.ops.size()) << toString(batch);
    ASSERT_EQUALS(1, batch.expansions.size());
    ASSERT_EQUALS(2, batch.expansions[0].size());
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(0, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(innerOps[0].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][0].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps[1].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][1].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[1].expansionsEntry);

    batcher->join();
}

TEST_F(TenantOplogBatcherTest, GetNextApplierBatchGroupsMultipleTransactions) {
    std::vector<OplogEntry> innerOps1;
    std::vector<OplogEntry> innerOps2;
    std::vector<BSONObj> srcOps;
    innerOps1.push_back(makeInsertOplogEntry(10, NamespaceString(dbName, "foo")));
    innerOps1.push_back(makeInsertOplogEntry(11, NamespaceString(dbName, "foo")));
    innerOps2.push_back(makeInsertOplogEntry(20, NamespaceString(dbName, "foo")));
    innerOps2.push_back(makeInsertOplogEntry(21, NamespaceString(dbName, "foo")));
    srcOps.push_back(makeApplyOpsOplogEntry(1, false, innerOps1).getEntry().toBSON());
    srcOps.push_back(makeApplyOpsOplogEntry(2, false, innerOps2).getEntry().toBSON());

    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());
    auto batch = batchFuture.get();
    batcher->shutdown();

    ASSERT_EQUALS(2, batch.ops.size()) << toString(batch);
    ASSERT_EQUALS(2, batch.expansions.size());

    // First transaction.
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(0, batch.ops[0].expansionsEntry);
    ASSERT_EQUALS(2, batch.expansions[0].size());
    ASSERT_BSONOBJ_EQ(innerOps1[0].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][0].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps1[1].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][1].getDurableReplOperation()).toBSON());

    // Second transaction.
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(1, batch.ops[1].expansionsEntry);
    ASSERT_EQUALS(2, batch.expansions[1].size());
    ASSERT_BSONOBJ_EQ(innerOps2[0].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[1][0].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps2[1].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[1][1].getDurableReplOperation()).toBSON());

    batcher->join();
}

TEST_F(TenantOplogBatcherTest, GetNextApplierBatchChecksBatchLimitsForNumberOfOperations) {
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeInsertOplogEntry(1, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(3, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(4, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(5, NamespaceString(dbName, "bar")).getEntry().toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    // Set batch limits so that each batch contains a maximum of 'BatchLimit::ops'.
    auto limits = bigBatchLimits;
    limits.ops = 3U;
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(limits);

    // First batch: [insert, insert, insert]
    auto batch = batchFuture.get();
    ASSERT_EQUALS(3U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_BSONOBJ_EQ(srcOps[2], batch.ops[2].entry.getEntry().toBSON());

    // Second batch: [insert, insert]
    batchFuture = batcher->getNextBatch(limits);
    batch = batchFuture.get();
    ASSERT_EQUALS(2U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[3], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_BSONOBJ_EQ(srcOps[4], batch.ops[1].entry.getEntry().toBSON());
    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, GetNextApplierBatchChecksBatchLimitsForSizeOfOperations) {
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeInsertOplogEntry(1, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(3, NamespaceString(dbName, "bar")).getEntry().toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    // Set batch limits so that only the first two operations can fit into the first batch.
    auto limits = bigBatchLimits;
    limits.bytes = std::size_t(srcOps[0].objsize() + srcOps[1].objsize());
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(limits);

    // First batch: [insert, insert]
    auto batch = batchFuture.get();
    ASSERT_EQUALS(2U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());

    // Second batch: [insert]
    batchFuture = batcher->getNextBatch(limits);
    batch = batchFuture.get();
    ASSERT_EQUALS(1U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[2], batch.ops[0].entry.getEntry().toBSON());
    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, LargeTransactionProcessedIndividuallyAndExpanded) {
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeInsertOplogEntry(1, NamespaceString(dbName, "bar")).getEntry().toBSON());
    std::vector<OplogEntry> innerOps1;
    innerOps1.push_back(makeInsertOplogEntry(11, NamespaceString(dbName, "bar")));
    innerOps1.push_back(makeInsertOplogEntry(12, NamespaceString(dbName, "bar")));
    std::vector<OplogEntry> innerOps2;
    innerOps2.push_back(makeInsertOplogEntry(21, NamespaceString(dbName, "bar")));
    innerOps2.push_back(makeInsertOplogEntry(22, NamespaceString(dbName, "bar")));
    std::vector<OplogEntry> innerOps3;
    innerOps3.push_back(makeInsertOplogEntry(31, NamespaceString(dbName, "bar")));
    innerOps3.push_back(makeInsertOplogEntry(32, NamespaceString(dbName, "bar")));

    // Makes entries with ts from range [2, 5).
    std::vector<OplogEntry> multiEntryTransaction = makeMultiEntryTransactionOplogEntries(
        2, dbName, /* prepared */ false, {innerOps1, innerOps2, innerOps3});
    for (auto entry : multiEntryTransaction) {
        srcOps.push_back(entry.getEntry().toBSON());
    }

    // Push one extra operation to ensure that the last oplog entry of a large transaction
    // is processed by itself.
    srcOps.push_back(makeInsertOplogEntry(5, NamespaceString(dbName, "bar")).getEntry().toBSON());

    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);

    // First batch: [insert, applyops, applyops].
    auto batch = batchFuture.get();
    ASSERT_EQUALS(3U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQ(-1, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQ(-1, batch.ops[1].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[2], batch.ops[2].entry.getEntry().toBSON());
    ASSERT_EQ(-1, batch.ops[2].expansionsEntry);
    // Partial applyops are not expanded.
    ASSERT(batch.expansions.empty());

    // Second batch: 6 inserts, with 3 transaction oplog entries.
    // The last oplog entry of a large transaction must be processed by itself; all transactions are
    // expanded.
    batchFuture = batcher->getNextBatch(bigBatchLimits);
    batch = batchFuture.get();
    ASSERT_EQUALS(1U, batch.expansions.size()) << toString(batch);
    ASSERT_EQUALS(6U, batch.expansions[0].size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[3], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQ(0, batch.ops[0].expansionsEntry);

    ASSERT_BSONOBJ_EQ(innerOps1[0].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][0].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps1[1].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][1].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps2[0].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][2].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps2[1].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][3].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps3[0].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][4].getDurableReplOperation()).toBSON());
    ASSERT_BSONOBJ_EQ(innerOps3[1].getDurableReplOperation().toBSON(),
                      stripB(batch.expansions[0][5].getDurableReplOperation()).toBSON());

    // Third batch: [insert]. This confirms that the last oplog entry of a large txn will be batched
    // individually.
    batchFuture = batcher->getNextBatch(bigBatchLimits);
    batch = batchFuture.get();
    ASSERT_EQUALS(1U, batch.ops.size()) << toString(batch);
    ASSERT(batch.expansions.empty());
    ASSERT_BSONOBJ_EQ(srcOps[4], batch.ops[0].entry.getEntry().toBSON());

    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherRetreivesPreImageOutOfOrder) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    // We just started, no batch should be available.
    ASSERT(!batchFuture.isReady());
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeNoopOplogEntry(1, "preImage").getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "foo")).getEntry().toBSON());
    srcOps.push_back(
        makeUpdateOplogEntry(3, NamespaceString(dbName, "bar"), UUID::gen(), OpTime({1, 1}, 1))
            .getEntry()
            .toBSON());

    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    auto batch = batchFuture.get();
    batcher->shutdown();

    // Expect the pre-image to have been inserted twice.
    ASSERT_EQUALS(srcOps.size() + 1, batch.ops.size()) << toString(batch);
    ASSERT(batch.expansions.empty());
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[1].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[2].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[2].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[2], batch.ops[3].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[3].expansionsEntry);

    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherRetreivesPostImageOutOfOrder) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    // We just started, no batch should be available.
    ASSERT(!batchFuture.isReady());
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeNoopOplogEntry(1, "postImage").getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "foo")).getEntry().toBSON());
    srcOps.push_back(makeUpdateOplogEntry(3,
                                          NamespaceString(dbName, "bar"),
                                          UUID::gen(),
                                          boost::none /* preImageOpTime */,
                                          OpTime({1, 1}, 1))
                         .getEntry()
                         .toBSON());

    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    auto batch = batchFuture.get();
    batcher->shutdown();

    // Expect the post-image to have been inserted twice.
    ASSERT_EQUALS(srcOps.size() + 1, batch.ops.size()) << toString(batch);
    ASSERT(batch.expansions.empty());
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[1].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[2].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[2].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[2], batch.ops[3].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[3].expansionsEntry);

    batcher->join();
}

TEST_F(TenantOplogBatcherTest, GetNextApplierBatchRejectsZeroBatchOpsLimits) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    // bigBatchLimits is a legal batch limit.
    auto limits = bigBatchLimits;
    limits.ops = 0;
    ASSERT_THROWS_CODE(batcher->getNextBatch(limits), DBException, 4885607);

    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherRetreivesPreImageBeforeBatchStart) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeNoopOplogEntry(1, "preImage").getEntry().toBSON());
    srcOps.push_back(
        makeUpdateOplogEntry(2, NamespaceString(dbName, "bar"), UUID::gen(), OpTime({1, 1}, 1))
            .getEntry()
            .toBSON());

    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());
    // Pull the preImage off the buffer.
    BSONObj preImagePopped;
    ASSERT(_oplogBuffer.tryPop(nullptr /* mock does not need opCtx */, &preImagePopped));
    // Start the batcher reading after the preImage has been removed.
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    ASSERT_BSONOBJ_EQ(preImagePopped, srcOps[0]);
    auto batch = batchFuture.get();
    batcher->shutdown();

    // Expect the pre-image to have been inserted.
    ASSERT_EQUALS(srcOps.size(), batch.ops.size()) << toString(batch);
    ASSERT(batch.expansions.empty());
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[1].expansionsEntry);
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, OplogBatcherRetreivesPostImageBeforeBatchStart) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeNoopOplogEntry(1, "postImage").getEntry().toBSON());
    srcOps.push_back(makeUpdateOplogEntry(2,
                                          NamespaceString(dbName, "bar"),
                                          UUID::gen(),
                                          boost::none /* preImageOpTime */,
                                          OpTime({1, 1}, 1))
                         .getEntry()
                         .toBSON());

    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());
    // Pull the postImage off the buffer.
    BSONObj postImagePopped;
    ASSERT(_oplogBuffer.tryPop(nullptr /* mock does not need opCtx */, &postImagePopped));
    // Start the batcher reading after the preImage has been removed.
    auto batchFuture = batcher->getNextBatch(bigBatchLimits);
    ASSERT_BSONOBJ_EQ(postImagePopped, srcOps[0]);
    auto batch = batchFuture.get();
    batcher->shutdown();

    // Expect the post-image to have been inserted.
    ASSERT_EQUALS(srcOps.size(), batch.ops.size()) << toString(batch);
    ASSERT(batch.expansions.empty());
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[0].expansionsEntry);
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());
    ASSERT_EQUALS(-1, batch.ops[1].expansionsEntry);
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, GetNextApplierBatchRejectsZeroBatchSizeLimits) {
    auto batcher = std::make_shared<TenantOplogBatcher>(
        "tenant", &_oplogBuffer, _executor, Timestamp(0, 0) /* resumeBatchingTs */);
    ASSERT_OK(batcher->startup());
    // bigBatchLimits is a legal batch limit.
    auto limits = bigBatchLimits;
    limits.bytes = 0;
    ASSERT_THROWS_CODE(batcher->getNextBatch(limits), DBException, 4885601);

    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, ResumeOplogBatcherFromTimestamp) {
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeInsertOplogEntry(1, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(2, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(3, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(4, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(5, NamespaceString(dbName, "bar")).getEntry().toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    auto batcher =
        std::make_shared<TenantOplogBatcher>("tenant", &_oplogBuffer, _executor, Timestamp(4, 1));
    ASSERT_OK(batcher->startup());

    auto batchFuture = batcher->getNextBatch(bigBatchLimits);

    auto batch = batchFuture.get();
    ASSERT_EQUALS(1U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[4], batch.ops[0].entry.getEntry().toBSON());

    batcher->shutdown();
    batcher->join();
}

TEST_F(TenantOplogBatcherTest, ResumeOplogBatcherFromNonExistentTimestamp) {
    std::vector<BSONObj> srcOps;
    srcOps.push_back(makeInsertOplogEntry(4, NamespaceString(dbName, "bar")).getEntry().toBSON());
    srcOps.push_back(makeInsertOplogEntry(5, NamespaceString(dbName, "bar")).getEntry().toBSON());
    _oplogBuffer.push(nullptr, srcOps.cbegin(), srcOps.cend());

    auto batcher =
        std::make_shared<TenantOplogBatcher>("tenant", &_oplogBuffer, _executor, Timestamp(3, 1));
    ASSERT_OK(batcher->startup());

    auto batchFuture = batcher->getNextBatch(bigBatchLimits);

    auto batch = batchFuture.get();
    ASSERT_EQUALS(2U, batch.ops.size()) << toString(batch);
    ASSERT_BSONOBJ_EQ(srcOps[0], batch.ops[0].entry.getEntry().toBSON());
    ASSERT_BSONOBJ_EQ(srcOps[1], batch.ops[1].entry.getEntry().toBSON());

    batcher->shutdown();
    batcher->join();
}

}  // namespace repl
}  // namespace mongo