summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_insert_getlasterror_w2.js
blob: 8df1b9caa8f514c31243119c63cfefd37eafa7e6 (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
// replica set as solo shard
// TODO: Add assertion code that catches hang

(function() {
    "use strict";

    var numDocs = 2000;
    var baseName = "shard_insert_getlasterror_w2";
    var testDBName = baseName;
    var testCollName = 'coll';
    var replNodes = 3;

    // ~1KB string
    var textString = '';
    for (var i = 0; i < 40; i++) {
        textString += 'abcdefghijklmnopqrstuvwxyz';
    }

    // Spin up a sharded cluster, but do not add the shards
    var shardingTestConfig = {
        name : baseName,
        mongos : 1,
        shards : 1,
        rs : { nodes : replNodes },
        other : { manualAddShard : true }
    };
    var shardingTest = new ShardingTest(shardingTestConfig);

    // Get connection to the individual shard
    var replSet1 = shardingTest.rs0;

    // Add data to it
    var testDBReplSet1 = replSet1.getPrimary().getDB(testDBName);
    var bulk = testDBReplSet1.foo.initializeUnorderedBulkOp();
    for (var i = 0; i < numDocs; i++) {
        bulk.insert({ x: i, text: textString });
    }
    assert.writeOK(bulk.execute());

    // Get connection to mongos for the cluster
    var mongosConn = shardingTest.s;
    var testDB = mongosConn.getDB(testDBName);

    // Add replSet1 as only shard
    mongosConn.adminCommand({ addshard : replSet1.getURL() });

    // Enable sharding on test db and its collection foo
    assert.commandWorked(mongosConn.getDB('admin').runCommand({ enablesharding : testDBName }));
    testDB[testCollName].ensureIndex({ x : 1 });
    assert.commandWorked(mongosConn.getDB('admin').
                         runCommand({ shardcollection : testDBName + '.' + testCollName,
                                      key : { x : 1 }}));

    // Test case where GLE should return an error
    testDB.foo.insert({_id:'a', x:1});
    assert.writeError(testDB.foo.insert({ _id: 'a', x: 1 },
                                    { writeConcern: { w: 2, wtimeout: 30000 }}));

    // Add more data
    bulk = testDB.foo.initializeUnorderedBulkOp();
    for (var i = numDocs; i < 2 * numDocs; i++) {
        bulk.insert({ x: i, text: textString });
    }
    assert.writeOK(bulk.execute({ w: replNodes, wtimeout: 30000 }));

    // Take down two nodes and make sure slaveOk reads still work
    replSet1.stop(1);
    replSet1.stop(2);
    testDB.getMongo().adminCommand({ setParameter : 1, logLevel : 1 });
    testDB.getMongo().setSlaveOk();
    print("trying some queries");
    assert.soon(function() { try {
                testDB.foo.find().next();
            }
            catch(e) {
                print(e);
                return false;
            }
            return true;
        }, "Queries took too long to complete correctly.",
        2 * 60 * 1000);
    
    // Shutdown cluster
    shardingTest.stop();

    print('shard_insert_getlasterror_w2.js SUCCESS');
})();