summaryrefslogtreecommitdiff
path: root/jstests/core/currentop_cursors.js
blob: 15fe92ce4d826cf631fcde2106da365edddee05f (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
/**
 * Tests that an idle cursor will appear in the $currentOp output if the idleCursors option is
 * set to true.
 *
 * @tags: [
 *   assumes_read_concern_unchanged,
 *   assumes_read_preference_unchanged,
 *   requires_capped,
 * ]
 */

(function() {
"use strict";
const coll = db.jstests_currentop_cursors;

load("jstests/libs/fixture_helpers.js");  // for FixtureHelpers

// Avoiding using the shell helper to avoid the implicit collection recreation.
db.runCommand({drop: coll.getName()});
assert.commandWorked(db.createCollection(coll.getName(), {capped: true, size: 1000}));
for (let i = 0; i < 30; ++i) {
    assert.commandWorked(coll.insert({"val": i}));
}
/**
 * runTest creates a new collection called jstests_currentop_cursors and then runs the provided
 * find query. It calls $currentOp and does some basic assertions to make sure idleCursors is
 * behaving as intended in each case.
 * findFunc: A function that runs a find query. Is expected to return a cursorID.
 *  Arbitrary code can be run in findFunc as long as it returns a cursorID.
 * assertFunc: A function that runs assertions against the results of the $currentOp.
 * Takes the following arguments
 *  'findOut': The cursorID returned from findFunc.
 *  'result': The results from running $currenpOp as an array of JSON objects.
 * Arbitrary code can be run in assertFunc, and there is no return value needed.
 */
function runTest({findFunc, assertFunc}) {
    const adminDB = db.getSiblingDB("admin");
    const findOut = findFunc();
    const result = adminDB
                       .aggregate([
                           {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                           {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": findOut}]}}
                       ])
                       .toArray();
    assert.eq(result[0].ns, coll.getFullName(), result);
    assert.eq(result[0].cursor.originatingCommand.find, coll.getName(), result);
    assertFunc(findOut, result);
    const noIdle = adminDB
                       .aggregate([
                           {$currentOp: {allUsers: false, idleCursors: false}},
                           {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": findOut}]}}
                       ])
                       .toArray();
    assert.eq(noIdle.length, 0, tojson(noIdle));
    const noFlag =
        adminDB.aggregate([{$currentOp: {allUsers: false}}, {$match: {type: "idleCursor"}}])
            .toArray();

    assert.eq(noIdle.length, 0, tojson(noFlag));
}

// Basic test with default values.
runTest({
    findFunc: function() {
        return assert
            .commandWorked(db.runCommand({find: "jstests_currentop_cursors", batchSize: 2}))
            .cursor.id;
    },
    assertFunc: function(cursorId, result) {
        assert.eq(result.length, 1, result);
        // Plan summary does not exist on mongos, so skip this test on mongos.
        if (!FixtureHelpers.isMongos(db)) {
            assert.eq(result[0].planSummary, "COLLSCAN", result);
        } else {
            assert(!result[0].hasOwnProperty("planSummary"), result);
        }
        assert(result[0].lsid.hasOwnProperty('id'), result);
        assert(result[0].lsid.hasOwnProperty('uid'), result);
        const uri = new MongoURI(db.getMongo().host);
        assert(uri.servers.some((server) => {
            return result[0].host == getHostName() + ":" + server.port;
        }));
        const idleCursor = result[0].cursor;
        assert.eq(idleCursor.nDocsReturned, 2, result);
        assert.eq(idleCursor.nBatchesReturned, 1, result);
        assert.eq(idleCursor.tailable, false, result);
        assert.eq(idleCursor.awaitData, false, result);
        assert.eq(idleCursor.noCursorTimeout, false, result);
        assert.eq(idleCursor.originatingCommand.batchSize, 2, result);
        assert.lte(idleCursor.createdDate, idleCursor.lastAccessDate, result);
        // Make sure that the top level fields do not also appear in the cursor subobject.
        assert(!idleCursor.hasOwnProperty("planSummary"), result);
        assert(!idleCursor.hasOwnProperty('host'), result);
        assert(!idleCursor.hasOwnProperty('lsid'), result);
    }
});

// Test that tailable, awaitData, and noCursorTimeout are set.
runTest({
    findFunc: function() {
        return assert
            .commandWorked(db.runCommand({
                find: "jstests_currentop_cursors",
                batchSize: 2,
                tailable: true,
                awaitData: true,
                noCursorTimeout: true
            }))
            .cursor.id;
    },
    assertFunc: function(cursorId, result) {
        assert.eq(result.length, 1, result);
        const idleCursor = result[0].cursor;
        assert.eq(idleCursor.tailable, true, result);
        assert.eq(idleCursor.awaitData, true, result);
        assert.eq(idleCursor.noCursorTimeout, true, result);
        assert.eq(idleCursor.originatingCommand.batchSize, 2, result);
    }
});

// Test that dates are set correctly.
runTest({
    findFunc: function() {
        return assert
            .commandWorked(db.runCommand({find: "jstests_currentop_cursors", batchSize: 2}))
            .cursor.id;
    },
    assertFunc: function(cursorId, result) {
        const adminDB = db.getSiblingDB("admin");
        // Make sure the two cursors have different creation times.
        assert.soon(() => {
            const secondCursor = assert.commandWorked(
                db.runCommand({find: "jstests_currentop_cursors", batchSize: 2}));

            const secondResult =
                adminDB
                    .aggregate([
                        {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                        {
                            $match: {
                                $and: [
                                    {type: "idleCursor"},
                                    {"cursor.cursorId": secondCursor.cursor.id}
                                ]
                            }
                        }
                    ])
                    .toArray();
            return result[0].cursor.createdDate < secondResult[0].cursor.createdDate;
        });
    }
});

// Test larger batch size.
runTest({
    findFunc: function() {
        return assert
            .commandWorked(db.runCommand(
                {find: "jstests_currentop_cursors", batchSize: 4, noCursorTimeout: true}))
            .cursor.id;
    },
    assertFunc: function(cursorId, result) {
        const idleCursor = result[0].cursor;
        assert.eq(result.length, 1, result);
        assert.eq(idleCursor.nDocsReturned, 4, result);
        assert.eq(idleCursor.nBatchesReturned, 1, result);
        assert.eq(idleCursor.noCursorTimeout, true, result);
        assert.eq(idleCursor.originatingCommand.batchSize, 4, result);
    }
});

// Test batchSize and nDocs are incremented correctly.
runTest({
    findFunc: function() {
        return assert
            .commandWorked(db.runCommand({find: "jstests_currentop_cursors", batchSize: 2}))
            .cursor.id;
    },
    assertFunc: function(cursorId, result) {
        const adminDB = db.getSiblingDB("admin");
        const originalAccess = result[0].cursor.lastAccessDate;
        assert.commandWorked(db.runCommand(
            {getMore: cursorId, collection: "jstests_currentop_cursors", batchSize: 2}));
        result = adminDB
                     .aggregate([
                         {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                         {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
                     ])
                     .toArray();
        let idleCursor = result[0].cursor;
        assert.eq(idleCursor.nDocsReturned, 4, result);
        assert.eq(idleCursor.nBatchesReturned, 2, result);
        assert.eq(idleCursor.originatingCommand.batchSize, 2, result);
        // Make sure that the getMore will not finish running in the same milli as the cursor
        // creation.
        assert.soon(() => {
            assert.commandWorked(db.runCommand(
                {getMore: cursorId, collection: "jstests_currentop_cursors", batchSize: 2}));
            result = adminDB
                         .aggregate([
                             {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                             {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
                         ])
                         .toArray();
            idleCursor = result[0].cursor;
            return idleCursor.createdDate < idleCursor.lastAccessDate &&
                originalAccess < idleCursor.lastAccessDate;
        });
    }
});

// planSummary does not exist on Mongos, so skip this test.
if (!FixtureHelpers.isMongos(db)) {
    runTest({
        findFunc: function() {
            assert.commandWorked(coll.createIndex({"val": 1}));
            return assert
                .commandWorked(db.runCommand(
                    {find: "jstests_currentop_cursors", filter: {"val": {$gt: 2}}, batchSize: 2}))
                .cursor.id;
        },
        assertFunc: function(cursorId, result) {
            assert.eq(result.length, 1, result);
            assert.eq(result[0].planSummary, "IXSCAN { val: 1 }", result);
        }
    });
}
// Test lsid.id value is correct.
const session = db.getMongo().startSession();
runTest({
    findFunc: function() {
        const sessionDB = session.getDatabase("test");
        return assert
            .commandWorked(sessionDB.runCommand({find: "jstests_currentop_cursors", batchSize: 2}))
            .cursor.id;
    },
    assertFunc: function(cursorId, result) {
        assert.eq(result.length, 1, result);
        assert.eq(session.getSessionId().id, result[0].lsid.id);
    }
});
})();