summaryrefslogtreecommitdiff
path: root/jstests/sharding/mongos_validate_backoff.js
blob: ef057c04ef2db3378d9a73a516b20f0d003b9f1f (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
// Ensures that single mongos shard-key errors are fast, but slow down when many are triggered  
(function() {
'use strict';

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

var mongos = st.s0;
var admin = mongos.getDB("admin");
var coll = mongos.getCollection("foo.bar");

assert.commandWorked(admin.runCommand({ enableSharding : coll.getDB() + "" }));

coll.ensureIndex({ shardKey : 1 });
assert.commandWorked(admin.runCommand({ shardCollection : coll + "", key : { shardKey : 1 } }));

var timeBadInsert = function() {
    var start = new Date().getTime();

    // Bad insert, no shard key
    assert.writeError(coll.insert({ hello : "world" }));

    var end = new Date().getTime();

    return end - start;
};

// We need to work at least twice in order to check resetting the counter
var successNeeded = 2;
var success = 0;

// Loop over this test a few times, to ensure that the error counters get reset if we don't have
// bad inserts over a long enough time.
for (var test = 0; test < 5; test++) {
    var firstWait = timeBadInsert();
    var lastWait = 0;

    for(var i = 0; i < 20; i++) {
        printjson(lastWait = timeBadInsert());
    }
    
    // As a heuristic test, we want to make sure that the error wait after sleeping is much less
    // than the error wait after a lot of errors.
    if (lastWait > firstWait * 2 * 2) {
        success++;
    }
    
    if (success >= successNeeded) {
        break;
    }

    // Abort if we've failed too many times
    assert.lt(test, 4);

    // Sleeping for long enough to reset our exponential counter
    sleep(3000);
}

st.stop();

})();