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

// 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';

    load('jstests/libs/fixture_helpers.js');  // For FixtureHelpers.

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

    const nDocs = 100;

    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();

    // Make sure the batch size is small enough to ensure a getMore will need to be sent to at least
    // one shard.
    const batchSize = (nDocs / FixtureHelpers.numberOfShardsForCollection(coll)) - 1;

    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.QueryPlanKilled, 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());
    assert.eq(error.code, ErrorCodes.QueryPlanKilled, tojson(error));
    // In replica sets, collection drops are done in two phases, first renaming the collection to a
    // "drop pending" namespace, and then later reaping the collection. Therefore, we expect to
    // either see an error message related to a collection drop, or one related to a collection
    // rename.
    const droppedMsg = 'collection dropped';
    const renamedMsg = 'collection renamed';
    assert(-1 !== error.message.indexOf(droppedMsg) || -1 !== error.message.indexOf(renamedMsg),
           error.message);

    // Test that dropping an index between a find and a getMore has no effect on the query if the
    // query is not using the index.
    setupCollection();
    cursor = coll.find().batchSize(batchSize);
    cursor.next();  // Send the query to the server.
    assert.commandWorked(testDB.runCommand({dropIndexes: coll.getName(), index: {x: 1}}));
    assert.eq(cursor.itcount(), nDocs - 1);

    // Test that dropping the index being scanned by a cursor between a find and a getMore kills the
    // query with the appropriate code and message.
    setupCollection();
    cursor = coll.find().hint({x: 1}).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.QueryPlanKilled, tojson(error));
        assert.neq(-1, error.message.indexOf('collection renamed'), error.message);
    }

}());