summaryrefslogtreecommitdiff
path: root/jstests/sharding/multi_update_orphan_shard_key.js
blob: 80741e397a4d7c9438302ee50adc3b1672073780 (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
/**
 * Tests it isn't possible to update an orphan document's shard key. Only multi=true updates skip
 * shard versioning. They are therefore the only case which skips ownership filtering.
 *
 *  @tags: [
 *    requires_fcv_52
 * ]
 */
(function() {
"use strict";

load("jstests/libs/fail_point_util.js");

const st = new ShardingTest({mongos: 1, shards: 2, rs: {nodes: 1}});
const dbName = "test";
const collName = "update_orphan_shard_key";
const collection = st.s.getDB(dbName).getCollection(collName);

// Create a sharded collection with two chunks on shard0, split at the key {x: -1}.
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
assert.commandWorked(st.s.adminCommand({movePrimary: dbName, to: st.shard0.shardName}));
assert.commandWorked(st.s.adminCommand({shardCollection: collection.getFullName(), key: {x: 1}}));
assert.commandWorked(st.s.adminCommand({split: collection.getFullName(), middle: {x: -1}}));

// Insert some documents into the collection, but only into the higher of the two chunks.
assert.commandWorked(collection.insert(Array.from({length: 100}, (_, i) => ({_id: i, x: i}))));

// Enable the failpoint to cause range deletion to hang indefinitely.
const suspendRangeDeletionFailpoint = configureFailPoint(st.shard0, "suspendRangeDeletion");

// Note: Use _waitForDelete=false to ensure the command completes since the test intentionally
// causes range deletion to hang.
assert.commandWorked(st.s.adminCommand({
    moveChunk: collection.getFullName(),
    find: {x: 1},
    to: st.shard1.shardName,
    _waitForDelete: false,
}));

let res = assert.commandWorked(collection.update({x: 0}, {$set: {y: 1}}));
assert.eq(1, res.nMatched, res);
assert.eq(1, res.nModified, res);

// Do a multi=true update that will target both shards but not update any documents on the shard
// which owns the range [-1, MaxKey].
res = assert.commandWorked(
    collection.update({x: {$lte: 0}, y: {$exists: false}}, {$set: {x: -10, y: 2}}, {multi: true}));
assert.eq(0, res.nMatched, res);
assert.eq(0, res.nModified, res);
assert.eq(0, res.nUpserted, res);

// Wait for range deletion to happen on the donor.
suspendRangeDeletionFailpoint.off();
assert.soon(() => {
    return st.shard0.getCollection(collection.getFullName()).findOne({x: 1}) === null;
});

// Run a $out aggregation as a simple way to check for duplicate _id values.
assert.doesNotThrow(() => collection.aggregate([{$out: "output"}]));

st.stop();
})();