summaryrefslogtreecommitdiff
path: root/jstests/sharding/mr_shard_version.js
blob: 481feb7f2681c482ab2cc08042f8d5f83a03d1ab (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
// Test for SERVER-4158 (version changes during mapreduce)
(function() {

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

//Stop balancer, since it'll just get in the way of these
st.stopBalancer();

var coll = st.s.getCollection( jsTest.name() + ".coll" );

var numDocs = 50000;
var numKeys = 1000;
var numTests = 3;

var bulk = coll.initializeUnorderedBulkOp();
for( var i = 0; i < numDocs; i++ ){
    bulk.insert({ _id: i, key: "" + ( i % numKeys ), value: i % numKeys });
}
assert.writeOK(bulk.execute());

assert.eq( numDocs, coll.find().itcount() );

var halfId = coll.find().itcount() / 2;

// Shard collection in half
st.shardColl( coll, { _id : 1 }, { _id : halfId } );

st.printShardingStatus();

jsTest.log( "Collection now initialized with keys and values..." );

jsTest.log( "Starting migrations..." );

var migrateOp = { op : "command", ns : "admin", command : { moveChunk : "" + coll } };

var checkMigrate = function(){ print( "Result of migrate : " ); printjson( this ); };

var ops = {};
for( var i = 0; i < st._connections.length; i++ ){
    for( var j = 0; j < 2; j++ ){
        ops[ "" + (i * 2 + j) ] = { op : "command", ns : "admin", 
                                    command : { moveChunk : "" + coll, 
                                                     find : { _id : ( j == 0 ? 0 : halfId ) },
                                                       to : st._connections[i].shardName },
                                    check : checkMigrate };
    }
}

var bid = benchStart({ ops : ops,
                       host : st.s.host,
                       parallel : 1,
                       handleErrors : false });

jsTest.log( "Starting m/r..." );
                       
var map = function(){ emit( this.key, this.value ); };
var reduce = function(k, values){ 
    var total = 0;
    for( var i = 0; i < values.length; i++ ) total += values[i];
    return total;
};

var outputColl = st.s.getCollection( jsTest.name() + ".mrOutput" );

jsTest.log( "Output coll : " + outputColl );

for( var t = 0; t < numTests; t++ ){

    var results = coll.mapReduce( map, reduce, { out : { replace : outputColl.getName() } });
    
    // Assert that the results are actually correct, all keys have values of (numDocs / numKeys) x key
    var output = outputColl.find().sort({ _id : 1 }).toArray();
    
    // printjson( output )
    
    assert.eq( output.length, numKeys );
    printjson( output );
    for( var i = 0; i < output.length; i++ )
        assert.eq( parseInt( output[i]._id ) * ( numDocs / numKeys ), output[i].value );
    
}

jsTest.log( "Finishing parallel migrations..." );

printjson( benchFinish( bid ) );

st.stop();

})();