summaryrefslogtreecommitdiff
path: root/jstests/replsets/plan_cache_slaveok.js
blob: 65d2d36cd187def34c861888db87290a2cfe3b6d (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
// Verify that the plan cache commands can be run on secondaries, but only
// if slave ok is explicitly set.

var name = "plan_cache_slaveok";

function assertPlanCacheCommandsSucceed(db) {
    // .listQueryShapes()
    assert.commandWorked(db.runCommand({
        planCacheListQueryShapes: name
    }));

    // .getPlansByQuery()
    assert.commandWorked(db.runCommand({
        planCacheListPlans: name,
        query: {a: 1}
    }));

    // .clear()
    assert.commandWorked(db.runCommand({
        planCacheClear: name,
        query: {a: 1}
    }));
}

function assertPlanCacheCommandsFail(db) {
    // .listQueryShapes()
    assert.commandFailed(db.runCommand({
        planCacheListQueryShapes: name
    }));

    // .getPlansByQuery()
    assert.commandFailed(db.runCommand({
        planCacheListPlans: name,
        query: {a: 1}
    }));

    // .clear()
    assert.commandFailed(db.runCommand({
        planCacheClear: name,
        query: {a: 1}
    }));
}

print("Start replica set with two nodes");
var replTest = new ReplSetTest({name: name, nodes: 2});
var nodes = replTest.startSet();
replTest.initiate();
var primary = replTest.getMaster();

// Insert a document and let it sync to the secondary.
print("Initial sync");
primary.getDB("test")[name].insert({a: 1});
replTest.awaitReplication();

// Check that the document is present on the primary.
assert.eq(1, primary.getDB("test")[name].findOne({a: 1})["a"]);

// Make sure the plan cache commands succeed on the primary.
assertPlanCacheCommandsSucceed(primary.getDB("test"));

// With slave ok false, the commands should fail on the secondary.
var secondary = replTest.getSecondary();
secondary.getDB("test").getMongo().setSlaveOk(false);
assertPlanCacheCommandsFail(secondary.getDB("test"));

// With slave ok true, the commands should succeed on the secondary.
secondary.getDB("test").getMongo().setSlaveOk(true);
assertPlanCacheCommandsSucceed(secondary.getDB("test"));