summaryrefslogtreecommitdiff
path: root/jstests/replsets/drop_collections_two_phase_apply_ops_convert_to_capped.js
blob: 26a018a863f89ebb414690e66cff5e5d17b2432b (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
/**
 * Test to ensure that it is OK to run convertToCapped using applyOps on a drop-pending collection.
 */

(function() {
"use strict";

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

// Set up a two phase drop test.
let testName = "drop_collection_two_phase_apply_ops_convert_to_capped";
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);

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

try {
    // Converting a drop-pending collection to a capped collection returns NamespaceNotFound.
    const dropPendingColl = twoPhaseDropTest.collectionIsPendingDrop(collName);
    const dropPendingCollName = dropPendingColl.name;
    const primary = replTest.getPrimary();
    const convertToCappedCmdWithName = {
        convertToCapped: dropPendingCollName,
        size: 100000,
    };
    TwoPhaseDropCollectionTest._testLog(
        'Attempting to convert collection with system.drop namespace: ' +
        tojson(convertToCappedCmdWithName));
    assert.commandFailedWithCode(primary.getDB(dbName).runCommand(convertToCappedCmdWithName),
                                 ErrorCodes.NamespaceNotFound);
    let dropPendingCollInfo = twoPhaseDropTest.collectionIsPendingDrop(collName);
    assert(dropPendingCollInfo,
           'convertToCapped using collection name ' + dropPendingCollName +
               ' affected drop-pending collection state unexpectedly');
    assert(!dropPendingCollInfo.options.capped);
    assert(!twoPhaseDropTest.collectionExists(collName));

    // Converting a drop-pending collection to a capped collection using applyOps with
    // system.drop namespace.
    const cmdNs = dbName + '.$cmd';
    const applyOpsCmdWithName = {
        applyOps: [{op: 'c', ns: cmdNs, o: {convertToCapped: dropPendingCollName, size: 100000}}]
    };
    TwoPhaseDropCollectionTest._testLog(
        'Attempting to convert collection using applyOps with system.drop namespace: ' +
        tojson(applyOpsCmdWithName));
    // NamespaceNotFound is ignored, but the drop-pending collection shouldn't be affected.
    assert.commandWorked(primary.adminCommand(applyOpsCmdWithName));
    assert(twoPhaseDropTest.collectionIsPendingDrop(collName),
           'applyOps using collection name ' + dropPendingCollName +
               ' affected drop-pending collection state unexpectedly');

    // Converting a drop-pending collection to a capped collection using applyOps with UUID.
    const dropPendingCollUuid = dropPendingColl.info.uuid;
    const applyOpsCmdWithUuid = {
        applyOps: [{
            op: 'c',
            ns: cmdNs,
            ui: dropPendingCollUuid,
            o: {convertToCapped: 'ignored_collection_name', size: 100000}
        }]
    };
    TwoPhaseDropCollectionTest._testLog(
        'Attempting to convert collection using applyOps with UUID: ' +
        tojson(applyOpsCmdWithUuid));
    // NamespaceNotFound is ignored, but the drop-pending collection shouldn't be affected.
    assert.commandWorked(primary.adminCommand(applyOpsCmdWithName));
    dropPendingCollInfo = twoPhaseDropTest.collectionIsPendingDrop(collName);
    assert(dropPendingCollInfo,
           'applyOps using UUID ' + dropPendingCollUuid +
               ' affected drop-pending collection state unexpectedly');
    assert(!dropPendingCollInfo.options.capped);
    assert.eq(dropPendingCollUuid.hex(),
              dropPendingCollInfo.info.uuid.hex(),
              'drop pending collection UUID does not match UUID of original collection: ' +
                  tojson(dropPendingCollInfo));
    assert(!twoPhaseDropTest.collectionExists(collName));

    // COMMIT collection drop.
    twoPhaseDropTest.commitDropCollection(collName);
} finally {
    twoPhaseDropTest.stop();
}
}());