summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/background_validation_with_ddl_ops.js
blob: 40d9adc979b5f9a14d5a5e7384dd40e3d6580943 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
 * Tests that an in-progress background validation gets interrupted when certain DDL operations are
 * executed.
 *
 * Dropping an index, the collection or database that are part of the ongoing background validation
 * should cause the validation to be interrupted. Renaming the collection across databases should
 * also interrupt, but same database collection renames should not. Operations that are not part of
 * the ongoing background validation, like creating new indexes or inserting documents should not
 * cause the validation to be interrupted.
 *
 * @tags: [requires_persistence]
 */
(function() {
"use strict";

const dbName = "background_validation_with_ddl_ops";
const dbNameRename = "background_validation_with_ddl_ops_rename";
const collName = "test";

let testDb, testColl;

const setFailpoint = () => {
    assert.commandWorked(testDb.adminCommand(
        {configureFailPoint: "hangDuringYieldingLocksForValidation", mode: "alwaysOn"}));
};

const unsetFailpoint = () => {
    assert.commandWorked(testDb.adminCommand(
        {configureFailPoint: "hangDuringYieldingLocksForValidation", mode: "off"}));
};

const waitUntilFailpoint = () => {
    checkLog.contains(testDb.getMongo(),
                      "Hanging on fail point 'hangDuringYieldingLocksForValidation'");
};

const resetCollection = () => {
    // Clear the log to get rid of any existing fail point logging that will be used to hang on.
    testDb = db.getSiblingDB(dbName);
    assert.commandWorked(testDb.adminCommand({clearLog: 'global'}));

    testColl = testDb.getCollection(collName);
    testColl.drop();
    db.getSiblingDB(dbNameRename).getCollection(collName).drop();

    assert.commandWorked(testColl.createIndex({x: 1}));

    // Insert 10,000 documents because validation will yield every 4096 entries fetched.
    const docsToInsert = 10000;
    var bulk = testColl.initializeUnorderedBulkOp();
    for (var i = 0; i < docsToInsert; i++) {
        bulk.insert({x: i});
    }
    assert.commandWorked(bulk.execute());

    assert.commandWorked(testDb.fsyncLock());
    assert.commandWorked(testDb.fsyncUnlock());
};

/**
 * Collection validation fails due to dropped index that was being validated.
 */
resetCollection();

let awaitFailedValidationDueToIndexDrop;
try {
    setFailpoint();
    awaitFailedValidationDueToIndexDrop = startParallelShell(function() {
        assert.commandFailedWithCode(db.getSiblingDB("background_validation_with_ddl_ops")
                                         .runCommand({validate: "test", background: true}),
                                     ErrorCodes.Interrupted);
    });

    waitUntilFailpoint();
    assert.commandWorked(testColl.dropIndex({x: 1}));
} finally {
    unsetFailpoint();
}

awaitFailedValidationDueToIndexDrop();

/**
 * Collection validation fails due to dropped collection that was being validated.
 */
resetCollection();

let awaitFailedValidationDueToCollectionDrop;
try {
    setFailpoint();
    awaitFailedValidationDueToCollectionDrop = startParallelShell(function() {
        assert.commandFailedWithCode(db.getSiblingDB("background_validation_with_ddl_ops")
                                         .runCommand({validate: "test", background: true}),
                                     ErrorCodes.Interrupted);
    });

    waitUntilFailpoint();
    assert.eq(true, testColl.drop());
} finally {
    unsetFailpoint();
}

awaitFailedValidationDueToCollectionDrop();

/**
 * Collection validation fails due to being renamed across databases while being validated.
 */
resetCollection();

let awaitFailedValidationDueToCrossDBCollectionRename;
try {
    setFailpoint();
    awaitFailedValidationDueToCrossDBCollectionRename = startParallelShell(function() {
        assert.commandFailedWithCode(db.getSiblingDB("background_validation_with_ddl_ops")
                                         .runCommand({validate: "test", background: true}),
                                     ErrorCodes.Interrupted);
    });

    waitUntilFailpoint();
    assert.commandWorked(testDb.adminCommand({
        renameCollection: dbName + "." + collName,
        to: dbNameRename + "." + collName,
        dropTarget: true
    }));
} finally {
    unsetFailpoint();
}

awaitFailedValidationDueToCrossDBCollectionRename();

/**
 * Collection validation fails due to database being dropped.
 */
resetCollection();

let awaitFailedValidationDueToDatabaseDrop;
try {
    setFailpoint();
    awaitFailedValidationDueToDatabaseDrop = startParallelShell(function() {
        assert.commandFailedWithCode(db.getSiblingDB("background_validation_with_ddl_ops")
                                         .runCommand({validate: "test", background: true}),
                                     ErrorCodes.Interrupted);
    });

    waitUntilFailpoint();
    assert.commandWorked(testDb.dropDatabase());
} finally {
    unsetFailpoint();
}

awaitFailedValidationDueToDatabaseDrop();

/**
 * Collection validation succeeds when running operations that do not affect ongoing background
 * validation.
 */
resetCollection();

let awaitPassedValidation;
try {
    setFailpoint();
    awaitPassedValidation = startParallelShell(function() {
        assert.commandWorked(db.getSiblingDB("background_validation_with_ddl_ops")
                                 .runCommand({validate: "test", background: true}));
    });

    waitUntilFailpoint();

    // Run a bunch of operations that shouldn't make background validation fail.
    assert.commandWorked(testColl.createIndex({y: 1}));
    assert.commandWorked(testColl.insert({x: 1, y: 1}));
    assert.commandWorked(testDb.createCollection("newCollection"));
    assert.commandWorked(testDb.adminCommand({
        renameCollection: dbName + "." +
            "newCollection",
        to: dbName + ".renamedCollection",
        dropTarget: true
    }));
    assert.commandWorked(testColl.dropIndex({y: 1}));

    // Rename the collection being validated across the same database.
    assert.commandWorked(testDb.adminCommand({
        renameCollection: dbName + "." + collName,
        to: dbName + ".testRenamed",
        dropTarget: true
    }));
} finally {
    unsetFailpoint();
}

awaitPassedValidation();
}());