summaryrefslogtreecommitdiff
path: root/jstests/sharding/bulk_shard_insert.js
blob: ae4626b8fdb2c31a44f19e0372c9bacaa5ceea55 (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
// Test bulk inserts running alonside the auto-balancer. Ensures that they do not conflict with each
// other.
(function() {
    'use strict';

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

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

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

    // 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 = 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.startBalancer();
            balancerOn = true;
        }
    }

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

    var count = coll.find().count();
    var itcount = coll.find().itcount();
    print("Inserted " + docsInserted + " count : " + count + " itcount : " + itcount);
    assert.eq(docsInserted, itcount);

    st.stop();
})();