summaryrefslogtreecommitdiff
path: root/jstests/multiVersion/map_reduce_multiversion_cluster.js
blob: 5b7379c92ae4896a15744215c9c6d6b811fad2aa (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
// Verify that we can run various forms of the mapReduce command during different stages of the
// cluster upgrade process.

// Checking UUID consistency uses cached connections, which are not valid across restarts or
// stepdowns.
TestData.skipCheckingUUIDsConsistentAcrossCluster = true;

(function() {
"use strict";

load("jstests/multiVersion/libs/multi_cluster.js");  // For upgradeCluster.

const testName = "map_reduce_multiversion_cluster";
const dbName = "test_" + testName;
const collName = testName;

// Start a sharded cluster in which all mongod and mongos processes are "last-stable" binVersion.
var st = new ShardingTest({
    shards: 2,
    rs: {nodes: 2, binVersion: "last-stable"},
    other: {mongosOptions: {binVersion: "last-stable"}}
});

let mongosConn = st.s;
assert.commandWorked(mongosConn.getDB(dbName).runCommand({create: collName}));
st.ensurePrimaryShard(dbName, st.shard0.shardName);

// Shard the test collection and split it into two chunks.
st.shardColl(collName,
             {state: 1} /* Shard key */,
             {state: "MA"} /* Split at */,
             {state: "MA"} /* Move the chunk containing {state: MA} to its own shard */,
             dbName,
             true /* Wait until documents orphaned by the move get deleted */);

// Seed the source collection with example user documents.
let sourceColl = mongosConn.getDB(dbName)[collName];
const nDocs = 100;
const states = ["AL", "MA", "NY"];

Random.setRandomSeed();
const bulk = sourceColl.initializeUnorderedBulkOp();
for (let i = 0; i < nDocs; i++) {
    const randomState = states[Math.floor(Math.random() * states.length)];
    bulk.insert({state: randomState, age: Random.randInt(100)});
}
assert.commandWorked(bulk.execute());

// Runs a set of MapReduce commands which are expected to be supported regardless of the FCV and
// binary version of the nodes in the cluster.
function runValidMrTests(coll) {
    // Map/reduce/finalize functions to compute the average age per state.
    function map() {
        emit(this.state, {count: 1, avgAge: 0, total: this.age});
    }
    function reduce(key, values) {
        let reducedObject = {total: 0, count: 0, avgAge: 0};
        values.forEach(function(value) {
            reducedObject.total += value.total;
            reducedObject.count += value.count;
        });
        return reducedObject;
    }
    function fin(key, reducedValue) {
        if (reducedValue.count > 0) {
            reducedValue.avgAge = reducedValue.total / reducedValue.count;
        }
        return reducedValue;
    }

    function assertResultsValid(results, expectedCount) {
        assert.gt(results.length, 0, tojson(results));
        assert.lte(results.length, expectedCount, tojson(results));
        results.map(resultDoc => assert.eq(resultDoc.value.avgAge,
                                           resultDoc.value.total / resultDoc.value.count,
                                           tojson(results)));
    }

    // Inline output.
    let res = assert.commandWorked(coll.mapReduce(map, reduce, {finalize: fin, out: {inline: 1}}));
    assertResultsValid(res.results, states.length);

    // Output mode "replace" to a non-existent unsharded collection.
    const replaceColl = coll.getDB().mr_replace_out;
    assert.commandWorked(coll.mapReduce(map, reduce, {finalize: fin, out: replaceColl.getName()}));
    res = replaceColl.find().toArray();
    assertResultsValid(res, states.length);

    // Output mode "replace" to an existing unsharded collection.
    assert.commandWorked(coll.mapReduce(map, reduce, {finalize: fin, out: replaceColl.getName()}));
    res = replaceColl.find().toArray();
    assertResultsValid(res, states.length);

    function testMergeAgainstColl(mergeColl) {
        // Output mode "merge" to a non-existent unsharded collection.
        mergeColl.drop();
        assert.commandWorked(coll.mapReduce(
            map,
            reduce,
            {finalize: fin, out: {merge: mergeColl.getName(), db: mergeColl.getDB().getName()}}));
        res = mergeColl.find().toArray();
        assertResultsValid(res, states.length);

        // Cache a sample result document to ensure that re-reducing actually occurs below.
        let sampleDoc = mergeColl.findOne();

        // Output mode "reduce" to an existing unsharded collection.
        assert.commandWorked(coll.mapReduce(
            map,
            reduce,
            {finalize: fin, out: {reduce: mergeColl.getName(), db: mergeColl.getDB().getName()}}));
        res = mergeColl.find().toArray();
        assertResultsValid(res, states.length);
        assert.gte(mergeColl.findOne({_id: sampleDoc._id}).value.avgAge, sampleDoc.value.avgAge);

        // Drop and recreate the target collection as sharded.
        mergeColl.drop();
        st.shardColl(mergeColl.getName(),
                     {_id: 1} /* Shard key */,
                     {_id: "MA"} /* Split at */,
                     {_id: "MA"} /* Move the chunk containing {state: MA} to its own shard */,
                     mergeColl.getDB().getName());

        // Insert sentinel docs in the output collection to ensure that output mode "merge" does
        // not blow it away. To workaround SERVER-44477, ensure that there is at least one document
        // on each shard.
        assert.commandWorked(mergeColl.insert({_id: "PA", value: {total: 5, avgAge: 5, count: 1}}));
        assert.commandWorked(
            mergeColl.insert({_id: "AL", value: {total: 50, avgAge: 50, count: 1}}));

        // Output mode "merge" to an existing sharded collection.
        assert.commandWorked(coll.mapReduce(map, reduce, {
            finalize: fin,
            out: {merge: mergeColl.getName(), db: mergeColl.getDB().getName(), sharded: true}
        }));
        res = mergeColl.find().toArray();
        assertResultsValid(res, states.length + 2);
        assert.eq(mergeColl.find({_id: "PA"}).itcount(), 1);

        // Cache a sample result document to ensure that re-reducing actually occurs below.
        sampleDoc = mergeColl.findOne({_id: {$not: {$in: ["AL", "PA"]}}});

        // Output mode "reduce" to an existing sharded collection.
        assert.commandWorked(coll.mapReduce(map, reduce, {
            finalize: fin,
            out: {reduce: mergeColl.getName(), db: mergeColl.getDB().getName(), sharded: true}
        }));
        res = mergeColl.find().toArray();
        assertResultsValid(res, states.length + 2);
        assert.gte(mergeColl.findOne({_id: sampleDoc._id}).value.avgAge, sampleDoc.value.avgAge);
    }

    // Test merge to a collection in the same database as the source collection.
    testMergeAgainstColl(coll.getDB().mr_merge_out);

    // Test merge to a collection in a foreign database. Creating the collection will also
    // implicitly create the database.
    assert.commandWorked(
        coll.getDB().getSiblingDB("foreign_db").runCommand({create: "mr_merge_out"}));
    testMergeAgainstColl(coll.getDB().getSiblingDB("foreign_db").mr_merge_out);
}

//
// Test against an all 4.2 cluster.
//
runValidMrTests(sourceColl);

//
// Upgrade the config servers and the shards to the "latest" binVersion.
//
st.upgradeCluster("latest", {upgradeShards: true, upgradeConfigs: true, upgradeMongos: false});

//
// Test against a mixed version cluster where the shards are upgraded to the latest binary but still
// in FCV 4.2. Mongos is still on the 4.2 binary version.
//
runValidMrTests(sourceColl);

//
// Upgrade mongos to the "latest" binVersion but keep the old FCV.
//
st.upgradeCluster("latest", {upgradeShards: false, upgradeConfigs: false, upgradeMongos: true});
mongosConn = st.s;
sourceColl = mongosConn.getDB(dbName)[collName];

//
// Test against a cluster where both mongos and the shards are upgraded to the latest binary
// version, but remain in the old FCV.
//
runValidMrTests(sourceColl);

//
// Fully upgraded to 4.4.
//
assert.commandWorked(
    mongosConn.getDB(dbName).adminCommand({setFeatureCompatibilityVersion: latestFCV}));
runValidMrTests(sourceColl);

st.stop();
}());