summaryrefslogtreecommitdiff
path: root/jstests/core/getmore_invalidated_cursors.js
blob: 527b6bf81e79fdd36612ba98564805985d13efd8 (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
// @tags: [requires_getmore, requires_non_retryable_commands]

// Tests that running a getMore on a cursor that has been invalidated by something like a collection
// drop will return an appropriate error message.
(function() {
    'use strict';

    const testDB = db.getSiblingDB("getmore_invalidated_cursors");
    const coll = testDB.test;

    const nDocs = 100;
    const batchSize = nDocs - 1;

    function setupCollection() {
        coll.drop();
        const bulk = coll.initializeUnorderedBulkOp();
        for (let i = 0; i < nDocs; ++i) {
            bulk.insert({_id: i, x: i});
        }
        assert.writeOK(bulk.execute());
        assert.commandWorked(coll.createIndex({x: 1}));
    }

    // Test that dropping the database between a find and a getMore will return an appropriate error
    // code and message.
    setupCollection();

    const isShardedCollection = coll.stats().sharded;
    const shellReadMode = testDB.getMongo().readMode();

    let cursor = coll.find().batchSize(batchSize);
    cursor.next();  // Send the query to the server.

    assert.commandWorked(testDB.dropDatabase());

    let error = assert.throws(() => cursor.itcount());

    if (testDB.runCommand({isdbgrid: 1}).isdbgrid && shellReadMode == 'legacy') {
        // The cursor will be invalidated on mongos, and we won't be able to find it.
        assert.neq(-1, error.message.indexOf('didn\'t exist on server'), error.message);
    } else {
        assert.eq(error.code, ErrorCodes.OperationFailed, tojson(error));
        assert.neq(-1, error.message.indexOf('collection dropped'), error.message);
    }

    // Test that dropping the collection between a find and a getMore will return an appropriate
    // error code and message.
    setupCollection();
    cursor = coll.find().batchSize(batchSize);
    cursor.next();  // Send the query to the server.

    coll.drop();

    error = assert.throws(() => cursor.itcount());
    if (isShardedCollection) {
        // The cursor will be invalidated on mongos, and we won't be able to find it.
        if (shellReadMode == 'legacy') {
            assert.neq(-1, error.message.indexOf('didn\'t exist on server'), error.message);
        } else {
            assert.eq(error.code, ErrorCodes.CursorNotFound, tojson(error));
            assert.neq(-1, error.message.indexOf('not found'), error.message);
        }
    } else {
        assert.eq(error.code, ErrorCodes.OperationFailed, tojson(error));
        assert.neq(-1, error.message.indexOf('collection dropped'), error.message);
    }

    // Test that dropping an index between a find and a getMore will return an appropriate error
    // code and message.
    setupCollection();
    cursor = coll.find().batchSize(batchSize);
    cursor.next();  // Send the query to the server.

    assert.commandWorked(testDB.runCommand({dropIndexes: coll.getName(), index: {x: 1}}));

    error = assert.throws(() => cursor.itcount());
    assert.eq(error.code, ErrorCodes.QueryPlanKilled, tojson(error));
    assert.neq(-1, error.message.indexOf('index \'x_1\' dropped'), error.message);

    // Test that killing a cursor between a find and a getMore will return an appropriate error
    // code and message.

    setupCollection();
    // Use the find command so that we can extract the cursor id to pass to the killCursors command.
    let cursorId = assert
                       .commandWorked(testDB.runCommand(
                           {find: coll.getName(), filter: {}, batchSize: batchSize}))
                       .cursor.id;
    assert.commandWorked(testDB.runCommand({killCursors: coll.getName(), cursors: [cursorId]}));
    assert.commandFailedWithCode(testDB.runCommand({getMore: cursorId, collection: coll.getName()}),
                                 ErrorCodes.CursorNotFound);

    // Test that all cursors on collections to be renamed get invalidated. Note that we can't do
    // renames on sharded collections.
    if (!isShardedCollection) {
        setupCollection();
        const collRenamed = testDB.test_rename;
        collRenamed.drop();
        cursor = coll.find().batchSize(batchSize);
        assert(cursor.hasNext(), "Expected more data from find call on " + coll.getName());
        assert.commandWorked(testDB.adminCommand({
            renameCollection: testDB.getName() + "." + coll.getName(),
            to: testDB.getName() + "." + collRenamed.getName()
        }));

        // Ensure getMore fails with an appropriate error code and message.
        error = assert.throws(() => cursor.itcount());
        assert.eq(error.code, ErrorCodes.OperationFailed, tojson(error));
        assert.neq(-1, error.message.indexOf('collection dropped'), error.message);
    }

}());