summaryrefslogtreecommitdiff
path: root/jstests/replsets/drop_collections_two_phase_write_concern.js
blob: e7b60eb18fb0bdca380f215d5cab7e4c0858072c (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
/**
 * Test to ensure that an operation with a majority write concern waits for drop pending
 * collections, with optimes preceding or equal to the operation's optime, to be reaped.
 */

(function() {
'use strict';

load('jstests/libs/check_log.js');
load('jstests/replsets/libs/two_phase_drops.js');  // For TwoPhaseDropCollectionTest.

// Alias to logging function in two_phase_drops.js
const testLog = TwoPhaseDropCollectionTest._testLog;

/**
 * Ensures that the operation fails with a write concern timeout.
 */
function assertTimeout(result) {
    assert.writeErrorWithCode(result, ErrorCodes.WriteConcernFailed);
    assert(result.hasWriteConcernError(), tojson(result));
    assert(result.getWriteConcernError().errInfo.wtimeout, tojson(result));
}

// Set up a two phase drop test.
let testName = 'drop_collection_two_phase_write_concern';
let dbName = testName;
let collName = 'collToDrop';
let twoPhaseDropTest = new TwoPhaseDropCollectionTest(testName, dbName);

// Initialize replica set.
let replTest = twoPhaseDropTest.initReplSet();

// Check for 'system.drop' two phase drop support.
if (!twoPhaseDropTest.supportsDropPendingNamespaces()) {
    jsTestLog('Drop pending namespaces not supported by storage engine. Skipping test.');
    twoPhaseDropTest.stop();
    return;
}

// Create the collection that will be dropped.
twoPhaseDropTest.createCollection(collName);

const primary = replTest.getPrimary();
const primaryDB = primary.getDB(dbName);
const collForInserts = primaryDB.getCollection('collForInserts');
const writeConcernForSuccessfulOp = {
    w: 'majority',
    wtimeout: replTest.kDefaultTimeoutMS
};
assert.writeOK(collForInserts.insert({_id: 0}, {writeConcern: writeConcernForSuccessfulOp}));

// PREPARE collection drop.
twoPhaseDropTest.prepareDropCollection(collName);

const writeConcernForTimedOutOp = {
    w: 'majority',
    wtimeout: 10000
};
assertTimeout(collForInserts.insert({_id: 1}, {writeConcern: writeConcernForTimedOutOp}));

// Prevent drop collection reaper from making progress after resuming oplog application.
assert.commandWorked(primary.adminCommand(
    {configureFailPoint: 'dropPendingCollectionReaperHang', mode: 'alwaysOn'}));

try {
    // Ensure that drop pending collection is not removed after resuming oplog application.
    testLog('Restarting oplog application on the secondary node.');
    twoPhaseDropTest.resumeOplogApplication(twoPhaseDropTest.replTest.getSecondary());

    // Ensure that we've hit the failpoint before moving on.
    checkLog.contains(primary, 'fail point dropPendingCollectionReaperHang enabled');

    // While the drop pending collection reaper is blocked, an operation waiting on a majority
    // write concern should time out.
    assertTimeout(collForInserts.insert({_id: 2}, {writeConcern: writeConcernForTimedOutOp}));
} finally {
    assert.commandWorked(
        primary.adminCommand({configureFailPoint: 'dropPendingCollectionReaperHang', mode: 'off'}));
}

// After the reaper is unblocked, an operation waiting on a majority write concern should run
// complete successfully.
assert.writeOK(collForInserts.insert({_id: 3}, {writeConcern: writeConcernForSuccessfulOp}));
assert.eq(4, collForInserts.find().itcount());

// COMMIT collection drop.
twoPhaseDropTest.commitDropCollection(collName);

twoPhaseDropTest.stop();
}());