summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/cursor_server_status_metrics.js
blob: b8f8a240662a14082555eae236e095877be77bed (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
/**
 * Tests for serverStatus metrics.cursor stats.
 */
(function() {
    var coll = db[jsTest.name()];
    coll.drop();
    assert.writeOK(coll.insert({_id: 1}));
    assert.writeOK(coll.insert({_id: 2}));
    assert.writeOK(coll.insert({_id: 3}));

    assert.eq(3, coll.find().count());

    function getCurrentCursorsOpen() {
        return db.serverStatus().metrics.cursor.open.total;
    }

    function getCurrentCursorsPinned() {
        return db.serverStatus().metrics.cursor.open.pinned;
    }

    var initialTotalOpen = getCurrentCursorsOpen();

    // We expect no pinned cursors
    assert.eq(0, getCurrentCursorsPinned());

    // Total open cursors should not have changed after exhausting a cursor.
    assert.eq(3, coll.find().itcount());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());
    assert.eq(3, coll.find().batchSize(2).itcount());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());
    assert.eq(3, coll.find().batchSize(1).itcount());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());

    assert.eq(3, coll.aggregate([]).itcount());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());
    assert.eq(3, coll.aggregate([], {cursor: {batchSize: 2}}).itcount());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());
    assert.eq(3, coll.aggregate([], {cursor: {batchSize: 1}}).itcount());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());

    // Total pinned cursors should remain zero exhausting a cursor.
    assert.eq(3, coll.find().itcount());
    assert.eq(0, getCurrentCursorsPinned());
    assert.eq(3, coll.find().batchSize(2).itcount());
    assert.eq(0, getCurrentCursorsPinned());
    assert.eq(3, coll.find().batchSize(1).itcount());
    assert.eq(0, getCurrentCursorsPinned());

    assert.eq(3, coll.aggregate([]).itcount());
    assert.eq(0, getCurrentCursorsPinned());
    assert.eq(3, coll.aggregate([], {cursor: {batchSize: 2}}).itcount());
    assert.eq(0, getCurrentCursorsPinned());
    assert.eq(3, coll.aggregate([], {cursor: {batchSize: 1}}).itcount());
    assert.eq(0, getCurrentCursorsPinned());

    // This cursor should remain open on the server, but not pinned.
    var cursor = coll.find().batchSize(2);
    cursor.next();
    assert.eq(initialTotalOpen + 1, getCurrentCursorsOpen());
    assert.eq(0, getCurrentCursorsPinned());

    // Same should be true after pulling the second document out of the cursor, since we haven't
    // issued a getMore yet.
    cursor.next();
    assert.eq(initialTotalOpen + 1, getCurrentCursorsOpen());
    assert.eq(0, getCurrentCursorsPinned());

    // Cursor no longer reported as open after being exhausted.
    cursor.next();
    assert(!cursor.hasNext());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());
    assert.eq(0, getCurrentCursorsPinned());

    // Same behavior expected for an aggregation cursor.
    var cursor = coll.aggregate([], {cursor: {batchSize: 2}});
    cursor.next();
    assert.eq(initialTotalOpen + 1, getCurrentCursorsOpen());
    assert.eq(0, getCurrentCursorsPinned());
    cursor.next();
    assert.eq(initialTotalOpen + 1, getCurrentCursorsOpen());
    assert.eq(0, getCurrentCursorsPinned());
    cursor.next();
    assert(!cursor.hasNext());
    assert.eq(initialTotalOpen, getCurrentCursorsOpen());
    assert.eq(0, getCurrentCursorsPinned());
}());