summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/snapshot_cursor_integrity.js
blob: 95877a053fe3f8f6e6f36ab552203412277b73dc (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
// Tests that a cursor is iterated in a transaction/session iff it was created in that
// transaction/session. Specifically tests this in the context of snapshot cursors.
// @tags: [requires_replication]
(function() {
    "use strict";

    const dbName = "test";
    const collName = "coll";

    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();

    const primaryDB = rst.getPrimary().getDB(dbName);
    if (!primaryDB.serverStatus().storageEngine.supportsSnapshotReadConcern) {
        rst.stopSet();
        return;
    }

    const session1 = primaryDB.getMongo().startSession();
    const sessionDB1 = session1.getDatabase(dbName);

    const session2 = primaryDB.getMongo().startSession();
    const sessionDB2 = session2.getDatabase(dbName);

    const bulk = primaryDB.coll.initializeUnorderedBulkOp();
    for (let i = 0; i < 10; ++i) {
        bulk.insert({_id: i});
    }
    assert.commandWorked(bulk.execute({w: "majority"}));

    // Establish a snapshot cursor in session1.
    let res = assert.commandWorked(sessionDB1.runCommand({
        find: collName,
        readConcern: {level: "snapshot"},
        txnNumber: NumberLong(0),
        batchSize: 2
    }));
    assert(res.hasOwnProperty("cursor"));
    assert(res.cursor.hasOwnProperty("id"));
    let cursorID = res.cursor.id;

    // The cursor may not be iterated outside of any session.
    assert.commandFailedWithCode(
        primaryDB.runCommand({getMore: cursorID, collection: collName, batchSize: 2}), 50737);

    // The cursor can still be iterated in session1.
    assert.commandWorked(sessionDB1.runCommand(
        {getMore: cursorID, collection: collName, txnNumber: NumberLong(0), batchSize: 2}));

    // The cursor may not be iterated in a different session.
    assert.commandFailedWithCode(
        sessionDB2.runCommand(
            {getMore: cursorID, collection: collName, txnNumber: NumberLong(0), batchSize: 2}),
        50738);

    // The cursor can still be iterated in session1.
    assert.commandWorked(sessionDB1.runCommand(
        {getMore: cursorID, collection: collName, txnNumber: NumberLong(0), batchSize: 2}));

    // The cursor may not be iterated outside of any transaction.
    assert.commandFailedWithCode(
        sessionDB1.runCommand({getMore: cursorID, collection: collName, batchSize: 2}), 50740);

    // The cursor can still be iterated in its transaction in session1.
    assert.commandWorked(sessionDB1.runCommand(
        {getMore: cursorID, collection: collName, txnNumber: NumberLong(0), batchSize: 2}));

    // The cursor may not be iterated in a different transaction on session1.
    assert.commandFailedWithCode(
        sessionDB1.runCommand(
            {getMore: cursorID, collection: collName, txnNumber: NumberLong(1), batchSize: 2}),
        50741);

    // The cursor can no longer be iterated because its transaction has ended.
    assert.commandFailedWithCode(
        sessionDB1.runCommand(
            {getMore: cursorID, collection: collName, txnNumber: NumberLong(0), batchSize: 2}),
        ErrorCodes.TransactionTooOld);

    // Kill the cursor.
    // TODO SERVER-33690: Aborting the transaction should kill the cursor.
    assert.commandWorked(
        sessionDB1.runCommand({killCursors: sessionDB1.coll.getName(), cursors: [cursorID]}));

    // Establish a cursor outside of any transaction in session1.
    res = assert.commandWorked(sessionDB1.runCommand({find: collName, batchSize: 2}));
    assert(res.hasOwnProperty("cursor"));
    assert(res.cursor.hasOwnProperty("id"));
    cursorID = res.cursor.id;

    // The cursor may not be iterated inside a transaction.
    assert.commandFailedWithCode(
        sessionDB1.runCommand(
            {getMore: cursorID, collection: collName, txnNumber: NumberLong(2), batchSize: 2}),
        50739);

    // The cursor can still be iterated outside of any transaction. Exhaust the cursor.
    assert.commandWorked(sessionDB1.runCommand({getMore: cursorID, collection: collName}));

    // Establish a cursor outside of any session.
    res = assert.commandWorked(primaryDB.runCommand({find: collName, batchSize: 2}));
    assert(res.hasOwnProperty("cursor"));
    assert(res.cursor.hasOwnProperty("id"));
    cursorID = res.cursor.id;

    // The cursor may not be iterated inside a session.
    assert.commandFailedWithCode(
        sessionDB1.runCommand({getMore: cursorID, collection: collName, batchSize: 2}), 50736);

    // The cursor can still be iterated outside of any session. Exhaust the cursor.
    assert.commandWorked(primaryDB.runCommand({getMore: cursorID, collection: collName}));

    session1.endSession();
    session2.endSession();
    rst.stopSet();
})();