summaryrefslogtreecommitdiff
path: root/jstests/parallel/del.js
blob: 903bb983d1f5c1ad9d4a2fb4d5d80f5690f8c45f (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
load('jstests/libs/parallelTester.js');

N = 1000;
HOST = db.getMongo().host;

a = db.getSisterDB("fooa");
b = db.getSisterDB("foob");
a.dropDatabase();
b.dropDatabase();

var kCursorKilledErrorCodes = [
    ErrorCodes.OperationFailed,
    ErrorCodes.QueryPlanKilled,
    ErrorCodes.CursorNotFound,
];

function del1(dbname, host, max, kCursorKilledErrorCodes) {
    try {
        var m = new Mongo(host);
        var db = m.getDB("foo" + dbname);
        var t = db.del;

        while (!db.del_parallel.count()) {
            var r = Math.random();
            var n = Math.floor(Math.random() * max);
            if (r < .9) {
                t.insert({x: n});
            } else if (r < .98) {
                t.remove({x: n});
            } else if (r < .99) {
                t.remove({x: {$lt: n}});
            } else {
                t.remove({x: {$gt: n}});
            }
            if (r > .9999)
                print(t.count());
        }

        return {ok: 1};
    } catch (e) {
        if (kCursorKilledErrorCodes.includes(e.code)) {
            // It is expected that the cursor may have been killed due to the database being
            // dropped concurrently.
            return {ok: 1};
        }

        throw e;
    }
}

function del2(dbname, host, max, kCursorKilledErrorCodes) {
    try {
        var m = new Mongo(host);
        var db = m.getDB("foo" + dbname);
        var t = db.del;

        while (!db.del_parallel.count()) {
            var r = Math.random();
            var n = Math.floor(Math.random() * max);
            var s = Math.random() > .5 ? 1 : -1;

            if (r < .5) {
                t.findOne({x: n});
            } else if (r < .75) {
                t.find({x: {$lt: n}}).sort({x: s}).itcount();
            } else {
                t.find({x: {$gt: n}}).sort({x: s}).itcount();
            }
        }

        return {ok: 1};
    } catch (e) {
        if (kCursorKilledErrorCodes.includes(e.code)) {
            // It is expected that the cursor may have been killed due to the database being
            // dropped concurrently.
            return {ok: 1};
        }

        throw e;
    }
}

all = [];

all.push(fork(del1, "a", HOST, N, kCursorKilledErrorCodes));
all.push(fork(del2, "a", HOST, N, kCursorKilledErrorCodes));
all.push(fork(del1, "b", HOST, N, kCursorKilledErrorCodes));
all.push(fork(del2, "b", HOST, N, kCursorKilledErrorCodes));

for (i = 0; i < all.length; i++)
    all[i].start();

for (i = 0; i < 10; i++) {
    sleep(2000);
    print("dropping");
    a.dropDatabase();
    b.dropDatabase();
}

a.del_parallel.save({done: 1});
b.del_parallel.save({done: 1});

for (i = 0; i < all.length; i++) {
    assert.commandWorked(all[i].returnData());
}