summaryrefslogtreecommitdiff
path: root/jstests/multiVersion/upgrade_cluster_v3_to_v4_with_parallel_ops.js
blob: 77579de561527c92a27587bca99b8f4a5c297216 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
 * Tests upgrading a config db which has different types of sharded collection data from v3 to v4
 */

load( './jstests/multiVersion/libs/multi_rs.js' )
load( './jstests/multiVersion/libs/multi_cluster.js' )
load( './jstests/libs/test_background_ops.js' )

jsTest.log( "Starting mixed 2.2/2.0 cluster..." );

var options = {
    
    mongosOptions : { binVersion : MongoRunner.versionIterator(["2.0","2.2"]) },
    configOptions : { binVersion : MongoRunner.versionIterator(["2.0","2.2"]) },
    shardOptions : { binVersion : MongoRunner.versionIterator(["2.0","2.2"]) },
    
    separateConfig : true,
    sync : true
}

//
// Create a basic mixed sharded cluster with extra mongoses to do work while we upgrade
//

var st = new ShardingTest({ shards : 2, mongos : 3, other : options });

var mongos = st.s0
var parallelMongoses = st._mongos.concat([]).splice(1);
var config = mongos.getDB("config")
var admin = mongos.getDB("admin")

var shards = config.shards.find().toArray();
var configConnStr = st._configDB;
var originalVersion = config.getMongo().getCollection("config.version").findOne();

//
// Shard a collection for each of our extra mongoses and distribute chunks to v2.0 and v2.2 shards
//

st.stopBalancer();

var shardedColls = [];
for (var i = 0; i < parallelMongoses.length; i++) {
    
    var parallelMongos = parallelMongoses[i];
    
    var coll = parallelMongos.getCollection("foo" + i + ".bar" + i);
    var admin = parallelMongos.getDB("admin");
    
    printjson(admin.runCommand({ enableSharding : coll.getDB() + "" }));
    printjson(admin.runCommand({ movePrimary : coll.getDB() + "", to : shards[0]._id }));
    printjson(admin.runCommand({ shardCollection : coll + "", key : { _id : 1 } }));
    
    printjson(admin.runCommand({ split : coll + "", middle : { _id : 0 } }));    
    printjson(admin.runCommand({ moveChunk : coll + "", find : { _id : 0 }, to : shards[1]._id }));
    
    printjson(admin.runCommand({ split : coll + "", middle : { _id : -0.3 } }));
    printjson(admin.runCommand({ split : coll + "", middle : { _id : -0.2 } }));
    printjson(admin.runCommand({ split : coll + "", middle : { _id : -0.1 } }));
    printjson(admin.runCommand({ split : coll + "", middle : { _id : 0.1 } }));
    printjson(admin.runCommand({ split : coll + "", middle : { _id : 0.2 } }));
    printjson(admin.runCommand({ split : coll + "", middle : { _id : 0.3 } }));
    
    printjson(admin.runCommand({ moveChunk : coll + "", find : { _id : -0.2 }, to : shards[1]._id }));
    printjson(admin.runCommand({ moveChunk : coll + "", find : { _id : 0.2 }, to : shards[0]._id }));
    
    shardedColls.push(coll);
}

st.startBalancer();

st.printShardingStatus();

//
// Upgrade cluster to v2.2
//

jsTest.log("Upgrading cluster to 2.2...")

st.upgradeCluster("2.2");
//Restart of mongos here is unfortunately necessary, connection pooling otherwise causes problems
st.restartMongoses();

jsTest.log("Cluster upgraded...")

var mongos = st.s0
var parallelMongoses = st._mongos.concat([]).splice(1);
var config = mongos.getDB("config");
var admin = mongos.getDB("admin");

function splitAndMove( mongosURL, shards, ns ){
    
    var coll = null;
    
    // Make sure we can eventually connect to the mongos
    assert.soon( function(){
        try{ 
            print("Waiting for connect to " + mongosURL + "...");
            coll = new Mongo(mongosURL).getCollection(ns + "");
            return true;
        }
        catch (e) {
            printjson(e);
            return false;
        }
    })
    
    var splitCount = 0;
    var moveCount = 0;
    
    jsTest.log("Starting splits and moves to " + ns + "...")
    
    while (!isFinished()) {
        try {
            // Split 75% of the time
            var isSplit = Random.rand() < 0.75;
            var key = (Random.rand() * 2.0) - 1.0;
            var shard = shards[parseInt(Random.rand() * shards.length)]
            
            var admin = coll.getMongo().getDB("admin");
            var result = null;
            
            if (isSplit) {
                result = admin.runCommand({ split : coll + "", 
                                            middle : { _id : key } });
                splitCount++;
                result["isSplit"] = true;
            }
            else {
                result = admin.runCommand({ moveChunk : coll + "",
                                            find : key,
                                            to : shards[shard]._id })
                moveCount++;
                result["isMove"] = true;
            }
            
            if (result.ok != 0) printjson(result);
        }
        catch (e) {
            sleep(1);
            printjson(e);
        }
    }
    
    jsTest.log("Finished splits and moves to " + ns + "...")
    return { splitCount : splitCount, moveCount : moveCount };
}

//
// Start split and move operations in the 2.2 cluster
//

jsTest.log("Starting split and move operations...")

var staticMongod = MongoRunner.runMongod({})
printjson( staticMongod )

var joinSplitAndMoves = [];
for (var i = 0; i < parallelMongoses.length; i++) {
    joinSplitAndMoves.push( 
        startParallelOps( staticMongod, // The connection where the test info is passed and stored
                          splitAndMove,
                          [ parallelMongoses[i].host, shards, shardedColls[i] + "" ] )
    );
}

jsTest.log("Sleeping for metadata operations to start...")

sleep(10 * 1000);

printShardingStatus(config, true);

//
// Do a config upgrade while the split and move operations are active
//

jsTest.log("Upgrading config db from v3 to v4...");

// Just stop the balancer, but don't wait for it to stop, to simulate race conds
st.setBalancer(false);
printjson(config.settings.find().toArray());

var startTime = new Date();

// Make sure up
var mongosNew = MongoRunner.runMongos({ binVersion : "2.4", configdb : configConnStr, upgrade : "" })
assert.neq(null, mongosNew);
MongoRunner.stopMongos(mongosNew);

var endTime = new Date();

jsTest.log( "Config db upgrade took " + ((endTime - startTime) / 1000) + " secs" );

//
// Stop the split and move operations
//

for (var i = 0; i < parallelMongoses.length; i++) {
    joinSplitAndMoves[i]();
}

printShardingStatus(config, true)

//
// Make sure our cluster was successfully upgraded with epochs
//

var checkUpgraded = function() {

    //
    // Verify backup collections are present
    //
    
    var collNames = config.getCollectionNames();
    
    var hasBackupColls = false;
    var hasBackupChunks = false;
    
    for (var i = 0; i < collNames.length; i++) {
        var collName = collNames[i];
        if (/^collections-backup/.test(collName)) {
        print("Found backup collections " + collName + "...")
        hasBackupColls = true;
    }
    if (/^chunks-backup/.test(collName)) {
        print("Found backup chunks " + collName + "...")
            hasBackupChunks = true;
        }
    }
    
    assert(hasBackupColls);
    assert(hasBackupChunks);
    
    //
    // Verify that all collections have correct epochs in new cluster
    //
    var collections = config.collections.find().toArray();
    
    var chunks = config.chunks.find().toArray();
    for (var i = 0; i < collections.length; i++) {
        
        var collection = collections[i];
        if (collection.dropped) continue;
        
        var epoch = collection.lastmodEpoch
        assert(epoch);
        
        for (var j = 0; j < chunks.length; j++) {
            var chunk = chunks[j];
            if (chunk.ns != collection._id) continue;
            assert.eq(chunk.lastmodEpoch, epoch);
        }
    }
    
    //
    // Verify cluster version is correct
    //

    var version = config.getMongo().getCollection("config.version").findOne();
    printjson(version)

    assert.eq(version.version, 3);
    assert.eq(version.minCompatibleVersion, 3);
    assert.eq(version.currentVersion, 4);
    assert(version.clusterId);
    assert.eq(version.excluding, undefined);
}

checkUpgraded();

jsTest.log("DONE!")

st.stop();