summaryrefslogtreecommitdiff
path: root/jstests/sharding/find_and_modify_after_multi_write.js
blob: c8081ce911919bd5fd8f1e17159beb4e22887df3 (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
(function() {
"use strict";

/**
 * Test that a targetted findAndModify will be properly routed after executing a write that
 * does not perform any shard version checks.
 */
var runTest = function(writeFunc) {
    var st = new ShardingTest({ shards: 2, mongos: 2 });

    var testDB = st.s.getDB('test');
    testDB.dropDatabase();

    assert.commandWorked(testDB.adminCommand({ enableSharding: 'test' }));
    st.ensurePrimaryShard('test', 'shard0000');

    assert.commandWorked(testDB.adminCommand({ shardCollection: 'test.user', key: { x: 1 }}));
    assert.commandWorked(testDB.adminCommand({ split: 'test.user', middle: { x: 0 }}));
    assert.commandWorked(testDB.adminCommand({ moveChunk: 'test.user',
                                               find: { x: 0 },
                                               to: 'shard0001',
                                               _waitForDelete: true }));

    var testDB2 = st.s1.getDB('test');
    testDB2.user.insert({ x: 123456 });

    // Move chunk to bump version on a different mongos.
    assert.commandWorked(testDB.adminCommand({ moveChunk: 'test.user',
                                               find: { x: 0 },
                                               to: 'shard0000',
                                               _waitForDelete: true }));

    // Issue a targetted findAndModify and check that it was upserted to the right shard.
    assert.commandWorked(testDB2.runCommand({
        findAndModify: 'user',
        query: { x: 100 },
        update: { $set: { y: 1 }},
        upsert: true
    }));

    assert.neq(null, st.d0.getDB('test').user.findOne({ x: 100 }));
    assert.eq(null, st.d1.getDB('test').user.findOne({ x: 100 }));

    // At this point, s1 thinks the version of 'test.user' is 2, bounce it again so it gets
    // incremented to 3
    assert.commandWorked(testDB.adminCommand({ moveChunk: 'test.user',
                                               find: { x: 0 },
                                               to: 'shard0001',
                                               _waitForDelete: true }));

    assert.commandWorked(testDB2.runCommand({
        findAndModify: 'user',
        query: { x: 200 },
        update: { $set: { y: 1 }},
        upsert: true
    }));

    assert.eq(null, st.d0.getDB('test').user.findOne({ x: 200 }));
    assert.neq(null, st.d1.getDB('test').user.findOne({ x: 200 }));

    // At this point, s0 thinks the version of 'test.user' is 3, bounce it again so it gets
    // incremented to 4
    assert.commandWorked(testDB.adminCommand({ moveChunk: 'test.user',
                                               find: { x: 0 },
                                               to: 'shard0000',
                                               _waitForDelete: true }));

    // Ensure that write commands with multi version do not reset the connection shard version to
    // ignored.
    writeFunc(testDB2);

    assert.commandWorked(testDB2.runCommand({
        findAndModify: 'user',
        query: { x: 300 },
        update: { $set: { y: 1 }},
        upsert: true
    }));

    assert.neq(null, st.d0.getDB('test').user.findOne({ x: 300 }));
    assert.eq(null, st.d1.getDB('test').user.findOne({ x: 300 }));

    st.stop();
};

runTest(function(db) {
    db.user.update({}, { $inc: { y: 987654 }}, false, true);
});

runTest(function(db) {
    db.user.remove({ y: 'noMatch' }, false);
});

})();