summaryrefslogtreecommitdiff
path: root/jstests/replsets/rollback_unprepared_transactions.js
blob: fdd286399d615860628ae7ba895c366fbca9d29c (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
/**
 * Tests that an unprepared transaction can be rolled back.
 * @tags: [requires_replication, requires_wiredtiger]
 */
(function() {
    'use strict';

    load('jstests/libs/check_log.js');
    load('jstests/replsets/libs/rollback_test.js');
    load('jstests/replsets/libs/rollback_files.js');

    // Operations that will be present on both nodes, before the common point.
    const dbName = 'test';
    const collName = 'test.t';
    const collNameShort = 't';
    let CommonOps = (node) => {
        const coll = node.getCollection(collName);
        const mydb = coll.getDB();
        assert.commandWorked(coll.insert({_id: 0}));
    };

    // Operations that will be performed on the rollback node past the common point.
    let RollbackOps = (node) => {
        const session = node.startSession();
        const sessionDB = session.getDatabase(dbName);
        const sessionColl = sessionDB.getCollection(collNameShort);
        session.startTransaction();
        assert.commandWorked(sessionColl.insert({_id: "a"}));
        assert.commandWorked(sessionColl.insert({_id: "b"}));
        assert.commandWorked(sessionColl.insert({_id: "c"}));
        assert.commandWorked(session.commitTransaction_forTesting());
        session.endSession();
    };

    // Set up Rollback Test.
    const rollbackTest = new RollbackTest();

    CommonOps(rollbackTest.getPrimary());

    const rollbackNode = rollbackTest.transitionToRollbackOperations();
    RollbackOps(rollbackNode);

    // Wait for rollback to finish.
    rollbackTest.transitionToSyncSourceOperationsBeforeRollback();
    rollbackTest.transitionToSyncSourceOperationsDuringRollback();
    rollbackTest.transitionToSteadyStateOperations();

    // Check collection count.
    const primary = rollbackTest.getPrimary();
    const coll = primary.getCollection(collName);
    assert.eq(1, coll.find().itcount());
    assert.eq(1, coll.count());

    // Confirm that the rollback wrote deleted documents to a file.
    const replTest = rollbackTest.getTestFixture();
    const expectedDocs = [{_id: "a"}, {_id: "b"}, {_id: "c"}];
    checkRollbackFiles(replTest.getDbPath(rollbackNode), collName, expectedDocs);

    rollbackTest.stop();
})();