summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/view_catalog_deadlock_with_rename.js
blob: ec3aa9d21fd64351ab9a3fc10b190287e621c51e (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
/**
 * This test demonstrates the deadlock we found in SERVER-40994: Rename locks
 * the source and target based on their resourceId order, and a delete op on an
 * non-existent collection (which also triggers a view catalog reload) locks the
 * same two namespaces in different order.
 *
 * The fix is to always lock 'system.views' collection in the end.
 */
(function() {
'use strict';

const conn = MongoRunner.runMongod();
const db = conn.getDB('test');

assert.commandWorked(db.runCommand({insert: 'a', documents: [{x: 1}]}));
assert.commandWorked(db.runCommand({insert: 'b', documents: [{x: 1}]}));

assert.commandWorked(db.createView('viewA', 'a', []));

// Will cause a view catalog reload.
assert.commandWorked(db.runCommand(
    {insert: 'system.views', documents: [{_id: 'test.viewB', viewOn: 'b', pipeline: []}]}));

const renameSystemViews = startParallelShell(function() {
    // This used to first lock 'test.system.views' and then 'test.aaabb' in X mode.
    assert.commandWorked(
        db.adminCommand({renameCollection: 'test.system.views', to: 'test.aaabb'}));
}, conn.port);

// This triggers view catalog reload. Therefore it first locked 'test.aaabb' in IX mode and then
// 'test.system.views' in IS mode.
assert.commandWorked(db.runCommand({delete: 'aaabb', deletes: [{q: {x: 2}, limit: 1}]}));

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