-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupgrade-test-client-cluster.js
150 lines (139 loc) · 4.67 KB
/
upgrade-test-client-cluster.js
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
/*
* This script will attempt to write & read
* data from a mongodb replica set over a long
* period of time.
* It should be run and the output monitored while
* you test an upgrade scenario to ensure the replica
* set maintains full availability
*/
// Standard connection string - note the client you run this from
// must be able to connect to each node listed (so if running locally on
// your laptop to EC2 nodes, make sure to use public DNS and firewalls are
// set appropriately).
connectionStrings = [ "mongodb://ec2-52-91-1-26.compute-1.amazonaws.com:27017/?" +
"w=majority&readPreference=primaryPreferred",
"mongodb://ec2-52-91-1-26.compute-1.amazonaws.com:27018/?" +
"w=majority&readPreference=primaryPreferred" ];
var connections = [];
connectionStrings.forEach( function(connStr) {
connections.push(new Mongo( connStr ) );
});
var dbs = [];
connections.forEach( function(conn) {
dbs.push( conn.getDB("test") );
});
var try_db = function( fn ) {
for (var i=0;i<dbs.length;i++) {
var db = dbs[i];
//print("---db---");printjson(db);
try {
//printjson(fn);
var result = fn(db);
//print("result from invoking fn(db)");
//printjson(result);
return result;
} catch (error) {
print("Error on " + db);
printjson(error);
}
}
}
//db.foo.drop();
try_db( function(db) {
db.testcontrol.drop();
});
//print("db.foo.count()=" + db.foo.count() );
var initialCount = try_db( function(db) { return db.foo.count() } );
print("db.foo.count() = " + initialCount );
var numDocsShouldBe = initialCount;
var numDocsActuallyInserted = 0;
try_db( function(db) {
var wr = db.testcontrol.insert( { "_id" : "MAIN", "shouldRun" : 1 } )
if ( wr.nInserted != 1 ) {
throw new Error("Error inserting test control document!");
}
});
var MAX_RETRIES = 100;
var __checkShouldRun = function(checkTries) {
try {
return try_db( function(db) {
var controller = db.testcontrol.findOne( { "_id" : "MAIN" } );
return controller;
});
} catch (error) {
print("error finding MAIN controldoc");
printjson(error);
if ( checkTries > MAX_RETRIES ) {
throw error;
}
print("will retry attempt #"+checkTries);
return __checkShouldRun(checkTries++);
}
}
var checkShouldRun = function() {
var controller = {};
while ( Object.keys(controller).length===0 ) {
//printjson(controller);
controller = __checkShouldRun(0);
//printjson(controller);
}
if ( controller.shouldRun===1 ) {
return true;
}
print("shouldRun != 1 - test will terminate");
printjson(controller);
return false;
}
var makeDoc = function() {
return { "x" : Math.floor(Math.random()*10000), "ts" : new Date() };
}
while ( checkShouldRun() ) {
var insertCall = function(insertTries, doc) {
try {
return try_db( function(db) {
var result = db.foo.insert( doc );
return result;
});
} catch (error) {
print("error attempting to insert into 'foo' collection");
// TODO - this logic will possibly insert the same doc multiple
// time, we need to check the exact error and also the writeResult
// the call to insert() may have successfully inserted the document
// but an error is thrown from like getlasterror (when connecting to
// older MongoDB version)
printjson(error);
if ( insertTries > MAX_RETRIES ) {
throw error;
}
print("will retry attempt #"+insertTries);
return insertCall(insertTries++,doc);
}
}
var doc = makeDoc();
var result = {};
while ( Object.keys(result).length===0 ) {
result = insertCall(0, doc);
}
numDocsShouldBe++;
if ( result.writeError ) {
printjson(result);
} else {
numDocsActuallyInserted += result.nInserted
}
sleep(1000);
if ( numDocsActuallyInserted%60===0 ) { //every 60 seconds check doc count
var count = 0;
try {
try_db( function(db) {
count = db.foo.count();
});
} catch (error) {
print("error calling foo.count()");
printjson(error);
throw error;
}
print("db.foo.count()=" +count +
" numDocsShouldBe=" + numDocsShouldBe +
" numDocsActuallyInserted=" + numDocsActuallyInserted);
}
}