summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workload_helpers/snapshot_read_utils.js
blob: 233f44ca68bad1ba0e01c0a294b51361493e026b (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
/**
 * Helpers for doing a snapshot read in concurrency suites. Specifically, the read is a find that
 * spans a getmore.
 */

/**
 * Parses a cursor from cmdResult, if possible.
 */
function parseCursor(cmdResult) {
    if (cmdResult.hasOwnProperty("cursor")) {
        assert(cmdResult.cursor.hasOwnProperty("id"));
        return cmdResult.cursor;
    }
    return null;
}

/**
 * Asserts cmd has either failed with a code in a specified set of codes or has succeeded.
 */
function assertWorkedOrFailed(cmd, cmdResult, errorCodeSet) {
    if (!cmdResult.ok) {
        assert.commandFailedWithCode(cmdResult,
                                     errorCodeSet,
                                     "expected command to fail with one of " + errorCodeSet +
                                         ", cmd: " + tojson(cmd) + ", result: " +
                                         tojson(cmdResult));
    } else {
        assert.commandWorked(cmdResult);
    }
}

/**
 * Performs a snapshot find.
 */
function doSnapshotFind(sortByAscending, collName, data, findErrorCodes) {
    // Reset txnNumber and stmtId for this transaction.
    data.txnNumber++;
    data.stmtId = 0;

    const sortOrder = sortByAscending ? {_id: 1} : {_id: -1};
    const findCmd = {
        find: collName,
        sort: sortOrder,
        batchSize: 0,
        readConcern: {level: "snapshot"},
        txnNumber: NumberLong(data.txnNumber),
        stmtId: NumberInt(data.stmtId++),
        startTransaction: true,
        autocommit: false
    };

    // Establish a snapshot batchSize:0 cursor.
    let res = data.sessionDb.runCommand(findCmd);
    assertWorkedOrFailed(findCmd, res, findErrorCodes);
    const cursor = parseCursor(res);

    if (!cursor) {
        const abortCmd = {
            abortTransaction: 1,
            txnNumber: NumberLong(data.txnNumber),
            autocommit: false
        };
        res = data.sessionDb.adminCommand(abortCmd);
        const abortErrorCodes = [
            ErrorCodes.NoSuchTransaction,
            ErrorCodes.TransactionCommitted,
            ErrorCodes.TransactionTooOld,
            ErrorCodes.Interrupted
        ];
        assertWorkedOrFailed(abortCmd, res, abortErrorCodes);
        data.cursorId = 0;
    } else {
        assert(cursor.hasOwnProperty("firstBatch"), tojson(res));
        assert.eq(0, cursor.firstBatch.length, tojson(res));
        assert.neq(cursor.id, 0);

        // Store the cursor Id in the data object.
        data.cursorId = cursor.id;
    }
}

/**
 * Performs a snapshot getmore. This function is to be used in conjunction with doSnapshotFind.
 */
function doSnapshotGetMore(collName, data, getMoreErrorCodes, commitTransactionErrorCodes) {
    // doSnapshotGetMore may be called even if doSnapshotFind fails to obtain a cursor.
    if (!data.cursorId) {
        return;
    }
    const getMoreCmd = {
        getMore: data.cursorId,
        collection: collName,
        batchSize: data.batchSize,
        txnNumber: NumberLong(data.txnNumber),
        stmtId: NumberInt(data.stmtId++),
        autocommit: false
    };
    let res = data.sessionDb.runCommand(getMoreCmd);
    assertWorkedOrFailed(getMoreCmd, res, getMoreErrorCodes);

    const commitCmd = {
        commitTransaction: 1,
        txnNumber: NumberLong(data.txnNumber),
        stmtId: NumberInt(data.stmtId++),
        autocommit: false
    };
    res = data.sessionDb.adminCommand(commitCmd);
    assertWorkedOrFailed(commitCmd, res, commitTransactionErrorCodes);
}

/**
 * This function can be used to share session data across threads.
 */
function insertSessionDoc(db, collName, data, session) {
    const sessionDoc = {"_id": "sessionDoc" + data.tid, "id": session.getSessionId().id};
    const res = db[collName].insert(sessionDoc);
    assert.writeOK(res);
    assert.eq(1, res.nInserted);
}

/**
 * This function can be used in conjunction with insertSessionDoc to kill any active sessions on
 * teardown.
 */
function killSessionsFromDocs(db, collName) {
    const sessionDocCursor = db[collName].find({"_id": {$regex: "sessionDoc*"}});
    assert(sessionDocCursor.hasNext());
    while (sessionDocCursor.hasNext()) {
        const sessionDoc = sessionDocCursor.next();
        assert.commandWorked(db.runCommand({killSessions: [{id: sessionDoc.id}]}));
    }
}