summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/currentop_active_cursor.js
blob: c0a3732205016ae6d7f11510daa3ad24dfd475d1 (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
// Test whether a pinned cursor does not show up as an idle cursor in curOp.
// Then test and make sure a pinned cursor shows up in the operation object.
// @tags: [requires_sharding]
(function() {
    "use strict";
    load("jstests/libs/pin_getmore_cursor.js");  // for "withPinnedCursor"

    function runTest(cursorId, coll) {
        const db = coll.getDB();
        const adminDB = db.getSiblingDB("admin");
        // Test that active cursors do not show up as idle cursors.
        const idleCursors =
            adminDB
                .aggregate([
                    {"$currentOp": {"localOps": true, "idleCursors": true, "allUsers": false}},
                    {"$match": {"type": "idleCursor"}}
                ])
                .toArray();
        assert.eq(idleCursors.length, 0, tojson(idleCursors));
        // Test that an active cursor shows up in currentOp.
        const activeCursors =
            adminDB
                .aggregate([
                    {"$currentOp": {"localOps": true, "idleCursors": false, "allUsers": false}},
                    {"$match": {"cursor": {"$exists": true}}}
                ])
                .toArray();
        assert.eq(activeCursors.length, 1, tojson(activeCursors));
        const cursorObject = activeCursors[0].cursor;
        assert.eq(cursorObject.originatingCommand.find, coll.getName(), tojson(activeCursors));
        assert.eq(cursorObject.nDocsReturned, 2, tojson(activeCursors));
        assert.eq(cursorObject.tailable, false, tojson(activeCursors));
        assert.eq(cursorObject.awaitData, false, tojson(activeCursors));
    }
    const conn = MongoRunner.runMongod({});
    let failPointName = "waitWithPinnedCursorDuringGetMoreBatch";
    withPinnedCursor({
        conn: conn,
        sessionId: null,
        db: conn.getDB("test"),
        assertFunction: runTest,
        runGetMoreFunc: function() {
            const response =
                assert.commandWorked(db.runCommand({getMore: cursorId, collection: collName}));
        },
        failPointName: failPointName,
        assertEndCounts: true
    });

    // Test OP_GET_MORE (legacy read mode) against a mongod.
    failPointName = "waitWithPinnedCursorDuringGetMoreBatch";
    const db = conn.getDB("test");
    db.getMongo().forceReadMode("legacy");
    withPinnedCursor({
        conn: conn,
        sessionId: null,
        db: db,
        assertFunction: runTest,
        runGetMoreFunc: function() {
            db.getMongo().forceReadMode("legacy");
            let cmdRes = {
                "cursor": {"firstBatch": [], "id": cursorId, "ns": db.jstest_with_pinned_cursor},
                "ok": 1
            };
            let cursor = new DBCommandCursor(db, cmdRes, 2);
            cursor.itcount();
        },
        failPointName: failPointName,
        assertEndCounts: true
    });
    MongoRunner.stopMongod(conn);

    // Sharded test
    failPointName = "waitAfterPinningCursorBeforeGetMoreBatch";
    let st = new ShardingTest({shards: 2, mongos: 1});
    withPinnedCursor({
        conn: st.s,
        sessionId: null,
        db: st.s.getDB("test"),
        assertFunction: runTest,
        runGetMoreFunc: function() {
            const response =
                assert.commandWorked(db.runCommand({getMore: cursorId, collection: collName}));
        },
        failPointName: failPointName,
        assertEndCounts: true
    });

    // Test OP_GET_MORE (legacy reead mode) against a mongos.
    withPinnedCursor({
        conn: st.s,
        sessionId: null,
        db: st.s.getDB("test"),
        assertFunction: runTest,
        runGetMoreFunc: function() {
            db.getMongo().forceReadMode("legacy");
            let cmdRes = {
                "cursor": {"firstBatch": [], "id": cursorId, "ns": db.jstest_with_pinned_cursor},
                "ok": 1
            };
            let cursor = new DBCommandCursor(db, cmdRes, 2);
            cursor.itcount();

        },
        failPointName: failPointName,
        assertEndCounts: true
    });
    st.stop();

})();