summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/failcommand_failpoint_not_parallel.js
blob: 7911e0ddfabb116156ce390824de3fd15bc53594 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(function() {
"use strict";

load("jstests/libs/parallelTester.js");

const conn = MongoRunner.runMongod();
assert.neq(null, conn);
const kDbName = "test_failcommand_noparallel";
const db = conn.getDB(kDbName);

// Test times when closing connection.
// Use distinct because it is rarely used by internal operations, making it less likely unrelated
// activity triggers the failpoint.
assert.commandWorked(db.adminCommand({
    configureFailPoint: "failCommand",
    mode: {times: 2},
    data: {
        closeConnection: true,
        failCommands: ["distinct"],
    }
}));
assert.throws(() => db.runCommand({distinct: "c", key: "_id"}));
assert.throws(() => db.runCommand({distinct: "c", key: "_id"}));
assert.commandWorked(db.runCommand({distinct: "c", key: "_id"}));
assert.commandWorked(db.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

// Test the blockConnection patterns.
jsTest.log("Test validation of blockConnection fields");
{
    // 'blockTimeMS' is required when 'blockConnection' is true.
    assert.commandWorked(db.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {
            blockConnection: true,
            failCommands: ["hello"],
        }
    }));
    assert.commandFailedWithCode(db.runCommand({hello: 1}), ErrorCodes.InvalidOptions);

    // 'blockTimeMS' must be non-negative.
    assert.commandWorked(db.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {
            blockConnection: true,
            blockTimeMS: -100,
            failCommands: ["hello"],
        }
    }));
    assert.commandFailedWithCode(db.runCommand({hello: 1}), ErrorCodes.InvalidOptions);

    assert.commandWorked(db.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
}

// Insert a test document.
assert.commandWorked(db.runCommand({
    insert: "c",
    documents: [{_id: 'block_test', run_id: 0}],
}));

/**
 * Returns the "run_id" of the test document.
 */
function checkRunId() {
    const ret = db.runCommand({find: "c", filter: {_id: 'block_test'}});
    assert.commandWorked(ret);

    const doc = ret["cursor"]["firstBatch"][0];
    return doc["run_id"];
}

/**
 * Runs update to increment the "run_id" of the test document by one.
 */
function incrementRunId() {
    assert.commandWorked(db.runCommand({
        update: "c",
        updates: [{q: {_id: 'block_test'}, u: {$inc: {run_id: 1}}}],
    }));
}

/**
 * Starts and returns a thread for updating the test document by incrementing the
 * "run_id" by one.
 */
function startIncrementRunIdThread() {
    const latch = new CountDownLatch(1);
    let thread = new Thread(function(connStr, dbName, latch) {
        jsTest.log("Sending update");

        const client = new Mongo(connStr);
        const db = client.getDB(dbName);
        latch.countDown();
        assert.commandWorked(db.runCommand({
            update: "c",
            updates: [{q: {_id: 'block_test'}, u: {$inc: {run_id: 1}}}],
        }));

        jsTest.log("Successfully applied update");
    }, conn.name, kDbName, latch);
    thread.start();
    latch.await();
    return thread;
}

assert.eq(checkRunId(), 0);
const kLargeBlockTimeMS = 60 * 1000;

jsTest.log("Test that only commands listed in failCommands block");
{
    assert.commandWorked(db.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {
            blockConnection: true,
            blockTimeMS: kLargeBlockTimeMS,
            failCommands: ["update"],
        }
    }));
    let thread = startIncrementRunIdThread();

    // Check that other commands get through.
    assert.commandWorked(db.runCommand({hello: 1}));
    assert.eq(checkRunId(), 0);

    // Wait for the blocked update to get through.
    thread.join();
    assert.soon(() => {
        return checkRunId() == 1;
    });

    assert.commandWorked(db.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
}

jsTest.log("Test command changes");
{
    assert.commandWorked(db.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {
            blockConnection: true,
            blockTimeMS: kLargeBlockTimeMS,
            failCommands: ["update", "insert"],
        }
    }));
    assert.eq(checkRunId(), 1);

    // Drop update from the command list and verify that the update gets through.
    assert.commandWorked(db.adminCommand({
        configureFailPoint: "failCommand",
        mode: "alwaysOn",
        data: {
            blockConnection: true,
            blockTimeMS: kLargeBlockTimeMS,
            failCommands: ["insert"],
        }
    }));
    incrementRunId();
    assert.eq(checkRunId(), 2);

    assert.commandWorked(db.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
}

MongoRunner.stopMongod(conn);
}());