summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/out_merge_majority_read.js
blob: 3c56851e6b699e2ee9870caf4d1aad00866813f3 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * Tests that $out and $merge with readConcern majority will only see committed data.
 *
 * Each operation is tested on a single node, and (if supported) through mongos on both sharded and
 * unsharded collections. Mongos doesn't directly handle readConcern majority, but these tests
 * should ensure that it correctly propagates the setting to the shards when running commands.
 * @tags: [
 *   requires_majority_read_concern,
 *   requires_sharding,
 * ]
 */

(function() {
'use strict';

const testServer = MongoRunner.runMongod();
const db = testServer.getDB("test");
if (!db.serverStatus().storageEngine.supportsCommittedReads) {
    print("Skipping read_majority.js since storageEngine doesn't support it.");
    MongoRunner.stopMongod(testServer);
    return;
}
MongoRunner.stopMongod(testServer);

function runTests(sourceColl, mongodConnection) {
    function makeSnapshot() {
        return assert.commandWorked(mongodConnection.adminCommand("makeSnapshot")).name;
    }
    function setCommittedSnapshot(snapshot) {
        assert.commandWorked(mongodConnection.adminCommand({"setCommittedSnapshot": snapshot}));
    }
    const db = sourceColl.getDB();
    const targetColl = db.targetColl;
    const targetReplaceDocsColl = db.targetReplaceDocsColl;

    assert.commandWorked(sourceColl.remove({}));
    assert.commandWorked(targetColl.remove({}));
    assert.commandWorked(targetReplaceDocsColl.remove({}));
    setCommittedSnapshot(makeSnapshot());

    // Insert a single document and make it visible by advancing the snapshot.
    assert.commandWorked(sourceColl.insert({_id: 1, state: 'before'}));
    assert.commandWorked(targetReplaceDocsColl.insert({_id: 1, state: 'before'}));
    setCommittedSnapshot(makeSnapshot());

    // This insert will not be visible to $merge.
    assert.commandWorked(sourceColl.insert({_id: 2, state: 'before'}));
    assert.commandWorked(targetReplaceDocsColl.insert({_id: 2, state: 'before'}));
    // Similarly this update will not be visible.
    assert.commandWorked(sourceColl.update({_id: 1}, {state: 'after'}));
    assert.commandWorked(targetReplaceDocsColl.update({_id: 1}, {state: 'after'}));

    // Make sure we see only the first document.
    let res = sourceColl.aggregate([], {readConcern: {level: 'majority'}});
    assert.eq(res.itcount(), 1);

    // Run $merge with whenNotMatched set to "insert". It will pick only the first document.
    // Also it will not see the update ('after').
    res = sourceColl.aggregate(
        [
            {$match: {state: 'before'}},
            {$project: {state: 'merge'}},
            {
                $merge: {
                    into: {db: targetColl.getDB().getName(), coll: targetColl.getName()},
                    whenMatched: "fail",
                    whenNotMatched: "insert"
                }
            }
        ],
        {readConcern: {level: 'majority'}});

    assert.eq(res.itcount(), 0);

    res = targetColl.find().sort({_id: 1});
    // Only a single document is visible ($merge did not see the second insert).
    assert.docEq(res.next(), {_id: 1, state: 'merge'});
    assert(res.isExhausted());

    // The same $merge but with whenMatched set to "replace".
    res = sourceColl.aggregate(
        [
            {$match: {state: 'before'}},
            {$project: {state: 'merge'}},
            {
                $merge: {
                    into: {
                        db: targetReplaceDocsColl.getDB().getName(),
                        coll: targetReplaceDocsColl.getName()
                    },
                    whenMatched: "replace",
                    whenNotMatched: "insert"
                }
            }
        ],
        {readConcern: {level: 'majority'}});
    assert.eq(res.itcount(), 0);

    setCommittedSnapshot(makeSnapshot());

    res = targetReplaceDocsColl.find().sort({_id: 1});
    // The first document must overwrite the update that the read portion of $merge did not see.
    assert.docEq(res.next(), {_id: 1, state: 'merge'});
    // The second document is the result of the independent insert that $merge did not see.
    assert.docEq(res.next(), {_id: 2, state: 'before'});
    assert(res.isExhausted());

    assert.commandWorked(targetColl.remove({}));
    setCommittedSnapshot(makeSnapshot());

    // Insert a document that will collide with $merge insert. The insert is not majority
    // commited.
    assert.commandWorked(targetColl.insert({_id: 1, state: 'collision'}));

    res = db.runCommand({
        aggregate: sourceColl.getName(),
        pipeline: [
            {$project: {state: 'merge'}},
            {
                $merge: {
                    into: {db: targetColl.getDB().getName(), coll: targetColl.getName()},
                    whenMatched: "fail",
                    whenNotMatched: "insert"
                }
            }
        ],
        cursor: {},
        readConcern: {level: 'majority'}
    });

    assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey);

    // Remove the documents (not majority).
    assert.commandWorked(targetColl.remove({_id: 1}));
    assert.commandWorked(targetColl.remove({_id: 2}));

    // $merge should successfuly 'overwrite' the collection as it is 'empty' (not majority).
    res = targetReplaceDocsColl.aggregate(
        [
            {$match: {state: 'before'}},
            {$project: {state: 'merge'}},
            {
                $merge: {
                    into: {db: targetColl.getDB().getName(), coll: targetColl.getName()},
                    whenMatched: "fail",
                    whenNotMatched: "insert"
                }
            }
        ],
        {readConcern: {level: 'majority'}});

    assert.eq(res.itcount(), 0);

    setCommittedSnapshot(makeSnapshot());

    res = targetColl.find().sort({_id: 1});
    // Only a single document is visible ($merge did not see the second insert).
    assert.docEq(res.next(), {_id: 2, state: 'merge'});
    assert(res.isExhausted());
}

const replTest = new ReplSetTest({
    nodes: 1,
    oplogSize: 2,
    nodeOptions: {
        setParameter: 'testingSnapshotBehaviorInIsolation=true',
        enableMajorityReadConcern: '',
        shardsvr: ''
    }
});
replTest.startSet();
// Cannot wait for a stable recovery timestamp with 'testingSnapshotBehaviorInIsolation' set.
replTest.initiateWithAnyNodeAsPrimary(
    null, "replSetInitiate", {doNotWaitForStableRecoveryTimestamp: true});

const mongod = replTest.getPrimary();

(function testSingleNode() {
    const db = mongod.getDB("singleNode");
    runTests(db.collection, mongod);
})();

const shardingTest = new ShardingTest({
    shards: 0,
    mongos: 1,
});
assert(shardingTest.adminCommand({addShard: replTest.getURL()}));

(function testUnshardedDBThroughMongos() {
    const db = shardingTest.getDB("throughMongos");
    runTests(db.unshardedDB, mongod);
})();

shardingTest.adminCommand({enableSharding: 'throughMongos'});

(function testUnshardedCollectionThroughMongos() {
    const db = shardingTest.getDB("throughMongos");
    runTests(db.unshardedCollection, mongod);
})();

(function testShardedCollectionThroughMongos() {
    const db = shardingTest.getDB("throughMongos");
    const collection = db.shardedCollection;
    shardingTest.adminCommand({shardCollection: collection.getFullName(), key: {_id: 1}});
    runTests(collection, mongod);
})();

shardingTest.stop();
replTest.stopSet();
})();