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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
//
// Utilities related to background operations while other operations are working
//
/**
* Allows synchronization between background ops and the test operations
*/
var waitForLock = function( mongo, name ){
var ts = new ObjectId()
var lockColl = mongo.getCollection( "config.testLocks" )
lockColl.update({ _id : name, state : 0 }, { $set : { state : 0 } }, true)
//
// Wait until we can set the state to 1 with our id
//
var startTime = new Date().getTime()
assert.soon( function() {
lockColl.update({ _id : name, state : 0 }, { $set : { ts : ts, state : 1 } })
var gleObj = lockColl.getDB().getLastErrorObj()
if( new Date().getTime() - startTime > 20 * 1000 ){
print( "Waiting for..." )
printjson( gleObj )
printjson( lockColl.findOne() )
printjson( ts )
}
return gleObj.n == 1 || gleObj.updatedExisting
}, "could not acquire lock", 30 * 1000, 100 )
print( "Acquired lock " + tojson( { _id : name, ts : ts } ) + " curr : " +
tojson( lockColl.findOne({ _id : name }) ) )
// Set the state back to 0
var unlock = function(){
print( "Releasing lock " + tojson( { _id : name, ts : ts } ) + " curr : " +
tojson( lockColl.findOne({ _id : name }) ) )
lockColl.update({ _id : name, ts : ts }, { $set : { state : 0 } })
}
// Return an object we can invoke unlock on
return { unlock : unlock }
}
/**
* Allows a test or background op to say it's finished
*/
var setFinished = function( mongo, name, finished ){
if( finished || finished == undefined )
mongo.getCollection( "config.testFinished" ).update({ _id : name }, { _id : name }, true )
else
mongo.getCollection( "config.testFinished" ).remove({ _id : name })
}
/**
* Checks whether a test or background op is finished
*/
var isFinished = function( mongo, name ){
return mongo.getCollection( "config.testFinished" ).findOne({ _id : name }) != null
}
/**
* Sets the result of a background op
*/
var setResult = function( mongo, name, result, err ){
mongo.getCollection( "config.testResult" ).update({ _id : name }, { _id : name, result : result, err : err }, true )
}
/**
* Gets the result for a background op
*/
var getResult = function( mongo, name ){
return mongo.getCollection( "config.testResult" ).findOne({ _id : name })
}
/**
* Overrides the parallel shell code in mongo
*/
function startParallelShell( jsCode, port ){
var x;
if ( port ) {
x = startMongoProgramNoConnect( "mongo" , "--port" , port , "--eval" , jsCode );
} else {
x = startMongoProgramNoConnect( "mongo" , "--eval" , jsCode , db ? db.getMongo().host : null );
}
return function(){
jsTestLog( "Waiting for shell " + x + "..." )
waitProgram( x );
jsTestLog( "Shell " + x + " finished." )
};
}
startParallelOps = function( mongo, proc, args, context ){
var procName = proc.name + "-" + new ObjectId()
var seed = new ObjectId( new ObjectId().valueOf().split("").reverse().join("") )
.getTimestamp().getTime()
// Make sure we aren't finished before we start
setFinished( mongo, procName, false )
setResult( mongo, procName, undefined, undefined )
// TODO: Make this a context of its own
var procContext = { procName : procName,
seed : seed,
waitForLock : waitForLock,
setFinished : setFinished,
isFinished : isFinished,
setResult : setResult,
setup : function( context, stored ){
waitForLock = function(){
return context.waitForLock( db.getMongo(), context.procName )
}
setFinished = function( finished ){
return context.setFinished( db.getMongo(), context.procName, finished )
}
isFinished = function(){
return context.isFinished( db.getMongo(), context.procName )
}
setResult = function( result, err ){
return context.setResult( db.getMongo(), context.procName, result, err )
}
}}
var bootstrapper = function( stored ){
var procContext = stored.procContext
procContext.setup( procContext, stored )
var contexts = stored.contexts
eval( "contexts = " + contexts )
for( var i = 0; i < contexts.length; i++ ){
if( typeof( contexts[i] ) != "undefined" ){
// Evaluate all contexts
contexts[i]( procContext )
}
}
var operation = stored.operation
eval( "operation = " + operation )
var args = stored.args
eval( "args = " + args )
result = undefined
err = undefined
try{
result = operation.apply( null, args )
}
catch( e ){
err = e
}
setResult( result, err )
}
var contexts = [ RandomFunctionContext, context ]
var testDataColl = mongo.getCollection( "config.parallelTest" )
testDataColl.insert({ _id : procName,
bootstrapper : tojson( bootstrapper ),
operation : tojson( proc ),
args : tojson( args ),
procContext : procContext,
contexts : tojson( contexts ) })
assert.eq( null, testDataColl.getDB().getLastError() )
var bootstrapStartup =
"{ var procName = '" + procName + "'; " +
"var stored = db.getMongo().getCollection( '" + testDataColl + "' )" +
".findOne({ _id : procName }); " +
"var bootstrapper = stored.bootstrapper; " +
"eval( 'bootstrapper = ' + bootstrapper ); " +
"bootstrapper( stored ); " +
"}"
var oldDB = db
db = mongo.getDB( "test" )
jsTest.log( "Starting " + proc.name + " operations..." )
var rawJoin = startParallelShell( bootstrapStartup )
db = oldDB
var join = function(){
setFinished( mongo, procName, true )
rawJoin();
result = getResult( mongo, procName )
assert.neq( result, null )
if( result.err ) throw "Error in parallel ops " + procName + " : "
+ tojson( result.err )
else return result.result
}
join.isFinished = function(){
return isFinished( mongo, procName )
}
join.setFinished = function( finished ){
return setFinished( mongo, procName, finished )
}
join.waitForLock = function( name ){
return waitForLock( mongo, name )
}
return join
}
var RandomFunctionContext = function( context ){
Random.srand( context.seed );
Random.randBool = function(){ return Random.rand() > 0.5 }
Random.randInt = function( min, max ){
if( max == undefined ){
max = min
min = 0
}
return min + Math.floor( Random.rand() * max )
}
Random.randShardKey = function(){
var numFields = 2 //Random.randInt(1, 3)
var key = {}
for( var i = 0; i < numFields; i++ ){
var field = String.fromCharCode( "a".charCodeAt() + i )
key[ field ] = 1
}
return key
}
Random.randShardKeyValue = function( shardKey ){
var keyValue = {}
for( field in shardKey ){
keyValue[ field ] = Random.randInt(1, 100)
}
return keyValue
}
Random.randCluster = function(){
var numShards = 2 //Random.randInt( 1, 10 )
var rs = false //Random.randBool()
var st = new ShardingTest({ shards : numShards,
mongos : 4,
other : { separateConfig : true, rs : rs } })
return st
}
}
//
// Some utility operations
//
function moveOps( collName, options ){
options = options || {}
var admin = db.getMongo().getDB( "admin" )
var config = db.getMongo().getDB( "config" )
var shards = config.shards.find().toArray()
var shardKey = config.collections.findOne({ _id : collName }).key
while( ! isFinished() ){
var findKey = Random.randShardKeyValue( shardKey )
var toShard = shards[ Random.randInt( shards.length ) ]._id
try {
printjson( admin.runCommand({ moveChunk : collName,
find : findKey,
to : toShard }) )
}
catch( e ){
printjson( e )
}
sleep( 1000 )
}
jsTest.log( "Stopping moveOps..." )
}
function splitOps( collName, options ){
options = options || {}
var admin = db.getMongo().getDB( "admin" )
var config = db.getMongo().getDB( "config" )
var shards = config.shards.find().toArray()
var shardKey = config.collections.findOne({ _id : collName }).key
while( ! isFinished() ){
var middleKey = Random.randShardKeyValue( shardKey )
try {
printjson( admin.runCommand({ split : collName,
middle : middleKey }) )
}
catch( e ){
printjson( e )
}
sleep( 1000 )
}
jsTest.log( "Stopping splitOps..." )
}
|