File: upgrade_cluster_v3_to_v4_db.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (245 lines) | stat: -rw-r--r-- 6,568 bytes parent folder | download | duplicates (3)
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
/**
 * 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' )

jsTest.log( "Starting 2.2 cluster..." );

var options = {
    
    mongosOptions : { binVersion : "2.2" },
    configOptions : { binVersion : "2.2" },
    shardOptions : { binVersion : "2.2" },
    
    separateConfig : true,
    sync : false
}

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

// Just set balancer to false, don't wait for it
st.setBalancer(false);

var mongos = st.s0
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();

st.printShardingStatus();

var resetBackupDBs = function() {
    
    var configConn = new Mongo(configConnStr);
    var databases = configConn.getDBs().databases;
    
    //
    // Drop all new backup databases
    //
    
    for (var i = 0; i < databases.length; i++) {
        var dbName = databases[i].name + "";
        if (!/^config$|^admin$|^local$/.test(dbName)) {
            print("Dropping " + dbName + "...")
            configConn.getDB(dbName).dropDatabase();
        }
    }
}

var resetVersion = function() {    
    config.getMongo().getCollection("config.version").update({ _id : 1 }, originalVersion, true);
    assert.eq(null, config.getLastError());
}

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 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)
    
}

//
// Default config upgrade
//

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

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


//
// Invalid config.version upgrade
//

jsTest.log("Clearing config.version collection...")

config.getMongo().getCollection("config.version").remove({})
assert.eq(null, config.getLastError());

// Make sure down
var mongosNew = MongoRunner.runMongos({ binVersion : "2.4", configdb : configConnStr, upgrade : "" })
assert.eq(null, mongosNew);
resetVersion();
resetBackupDBs();


//
// Bad config.version upgrade
//

jsTest.log("Adding bad config.version data...")

config.getMongo().getCollection("config.version").update({ _id : 1 }, { $unset : { version : 1 } });
assert.eq(null, config.getLastError());

// Make sure down
var mongosNew = MongoRunner.runMongos({ binVersion : "2.4", configdb : configConnStr, upgrade : "" })
assert.eq(null, mongosNew);
resetVersion();
resetBackupDBs();


//
// Invalid config.collections upgrade
//

jsTest.log("Adding bad sharded collection data...")

var coll = mongos.getCollection("foo.bar");

printjson(admin.runCommand({ enableSharding : coll.getDB() + "" }));
printjson(admin.runCommand({ shardCollection : coll + "", key : { _id : 1 } }));
printjson(admin.runCommand({ split : coll + "", middle : { _id : 0 } }));

config.collections.update({ _id : coll + "" }, { $set : { lastmodEpoch : ObjectId() }});
assert.eq(null, config.getLastError());

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


//
// Dropped collection upgrade
//

jsTest.log("Adding bad (dropped) sharded collection data...")

printjson(coll.drop());
// Disable balancing on the (dropped) coll to trigger additional collection validation.
// At least in 2.2, dropping a collection drops the noBalance flag as well, but this has been seen
// in the wild.
// TODO: Enable when 2.4.4 comes out
//sh.disableBalancing( coll );
printjson(config.collections.find().toArray());

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


//
// Invalid chunks collection upgrade
//

jsTest.log("Adding bad sharded chunks data...")

var coll = mongos.getCollection("foo2.bar2");

printjson(admin.runCommand({ enableSharding : coll.getDB() + "" }));
printjson(admin.runCommand({ shardCollection : coll + "", key : { _id : 1 } }));
printjson(admin.runCommand({ split : coll + "", middle : { _id : 0 } })); 

config.chunks.update({ ns : coll + "" }, { $set : { lastmodEpoch : ObjectId() }});
assert.eq(null, config.getLastError());

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

//
// Upgrade with modified old upgrade data
//

jsTest.log("Fiddling with data from last failed upgrade...")

var upgradeCollRegex = /^collections-upgrade/;
var configColls = config.getCollectionNames();
for (var i = 0; i < configColls.length; i++) {
    var configColl = configColls[i];
    if (upgradeCollRegex.test(configColl)) {
        print("Dropping collection: " + configColl);
        config.getCollection(configColl).drop();
        break;
    }
}

// Fix chunk data
config.chunks.update({}, { $unset : { versionEpoch : 1 }, $unset : { lastmodEpoch : 1 }}, false, true);
assert.eq(null, config.getLastError());

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

jsTest.log("DONE!")

st.stop();