summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/bulk_shard_insert.js
blob: 4ce7f555f365e0a4a4b936cf34538b7e1535d18d (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
// Test bulk inserts with sharding

// Setup randomized test
var seed = new Date().getTime()
// seed = 0

Random.srand( seed )
print( "Seeded with " + seed )


var st = new ShardingTest({ name : jsTestName(), shards : 4, chunkSize: 1 })

// Turn off balancer initially
st.setBalancer( false )

// Setup sharded collection
var mongos = st.s0
var db = mongos.getDB( jsTestName() )
var coll = db.coll
st.shardColl( coll, { _id : 1 }, false )

// Insert lots of bulk documents
var numDocs = 1000000

var bulkSize = Math.floor( Random.rand() * 1000 ) + 2
bulkSize = 4000
var docSize = 128 /* bytes */
print( "\n\n\nBulk size is " + bulkSize )

var data = "x"
while( Object.bsonsize({ x : data }) < docSize ){
    data += data
}

print( "\n\n\nDocument size is " + Object.bsonsize({ x : data }) )

var docsInserted = 0;
var balancerOn = false;

while( docsInserted < numDocs ){
    
    var currBulkSize = ( numDocs - docsInserted > bulkSize ) ? bulkSize : ( numDocs - docsInserted )
    
    var bulk = []
    for( var i = 0; i < currBulkSize; i++ ){
        bulk.push({hi: "there", at: docsInserted, i: i, x: data});
    }
    
    assert.writeOK(coll.insert( bulk ));
    
    if( Math.floor( docsInserted / 10000 ) != Math.floor( ( docsInserted + currBulkSize ) / 10000 ) ){
        print( "Inserted " + (docsInserted + currBulkSize) + " documents." )
        st.printShardingStatus()
    }
                
    docsInserted += currBulkSize
    
    if( docsInserted > numDocs / 2 && ! balancerOn ){
        print( "Turning on balancer after half documents inserted." )
        st.setBalancer( true )
        balancerOn = true;
    }
}

// Check we inserted all the documents
st.printShardingStatus()

var count = coll.find().count()
var itcount = count //coll.find().itcount()

print( "Inserted " + docsInserted + " count : " + count + " itcount : " + itcount )

st.setBalancer( true )
sleep( 10000 )

var count = coll.find().count()
var itcount = coll.find().itcount()

print( "Inserted " + docsInserted + " count : " + count + " itcount : " + itcount )


// SERVER-3645
// assert.eq( docsInserted, count )
assert.eq( docsInserted, itcount )