summaryrefslogtreecommitdiff
path: root/jstests/sharding/query/view_rewrite.js
blob: dae49dc2b0bbb6201322493e76f80cbea8eb90d2 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * Tests that query options are not dropped by mongos when a query against a view is rewritten as an
 * aggregation against the underlying collection.
 */
(function() {
"use strict";

load("jstests/libs/profiler.js");  // For profilerHasSingleMatchingEntryOrThrow.

const st = new ShardingTest({
    name: "view_rewrite",
    shards: 2,
    other: {
        rs0: {
            nodes:
                [{rsConfig: {priority: 1}}, {rsConfig: {priority: 0, tags: {"tag": "secondary"}}}]
        },
        rs1: {
            nodes:
                [{rsConfig: {priority: 1}}, {rsConfig: {priority: 0, tags: {"tag": "secondary"}}}]
        },
        enableBalancer: false
    }
});

const mongos = st.s0;
const config = mongos.getDB("config");
const mongosDB = mongos.getDB("view_rewrite");
const coll = mongosDB.getCollection("coll");

assert.commandWorked(config.adminCommand({enableSharding: mongosDB.getName()}));
st.ensurePrimaryShard(mongosDB.getName(), "view_rewrite-rs0");

const rs0Secondary = st.rs0.getSecondary();
const rs1Primary = st.rs1.getPrimary();
const rs1Secondary = st.rs1.getSecondary();

assert.commandWorked(config.adminCommand({shardCollection: coll.getFullName(), key: {a: 1}}));
assert.commandWorked(mongos.adminCommand({split: coll.getFullName(), middle: {a: 5}}));
assert.commandWorked(
    mongosDB.adminCommand({moveChunk: coll.getFullName(), find: {a: 5}, to: "view_rewrite-rs1"}));

for (let i = 0; i < 10; ++i) {
    assert.commandWorked(coll.insert({a: i}));
}

assert.commandWorked(mongosDB.createView("view", coll.getName(), []));
const view = mongosDB.getCollection("view");

//
// Confirms that queries run against views on mongos result in execution of a rewritten
// aggregation that contains all expected query options.
//
function confirmOptionsInProfiler(shardPrimary) {
    assert.commandWorked(shardPrimary.setProfilingLevel(2));

    // Aggregation
    assert.commandWorked(mongosDB.runCommand({
        aggregate: "view",
        pipeline: [],
        comment: "agg_rewrite",
        maxTimeMS: 5 * 60 * 1000,
        readConcern: {level: "linearizable"},
        cursor: {}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardPrimary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "agg_rewrite",
            "command.maxTimeMS": {"$exists": true},
            "command.readConcern": {level: "linearizable"},
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    // Find
    assert.commandWorked(mongosDB.runCommand({
        find: "view",
        comment: "find_rewrite",
        maxTimeMS: 5 * 60 * 1000,
        readConcern: {level: "linearizable"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardPrimary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "find_rewrite",
            "command.maxTimeMS": {"$exists": true},
            "command.readConcern": {level: "linearizable"},
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    // Count
    assert.commandWorked(mongosDB.runCommand({
        count: "view",
        comment: "count_rewrite",
        maxTimeMS: 5 * 60 * 1000,
        readConcern: {level: "linearizable"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardPrimary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "count_rewrite",
            "command.maxTimeMS": {"$exists": true},
            "command.readConcern": {level: "linearizable"},
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    // Distinct
    assert.commandWorked(mongosDB.runCommand({
        distinct: "view",
        key: "a",
        comment: "distinct_rewrite",
        maxTimeMS: 5 * 60 * 1000,
        readConcern: {level: "linearizable"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardPrimary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "distinct_rewrite",
            "command.maxTimeMS": {"$exists": true},
            "command.readConcern": {level: "linearizable"},
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    assert.commandWorked(shardPrimary.setProfilingLevel(0));
    shardPrimary.system.profile.drop();
}

//
// Confirms that queries run against views on mongos are executed against a tagged secondary, as
// per readPreference setting.
//
function confirmReadPreference(shardSecondary) {
    assert.commandWorked(shardSecondary.setProfilingLevel(2));

    // Aggregation
    assert.commandWorked(mongosDB.runCommand({
        query: {aggregate: "view", pipeline: [], comment: "agg_readPref", cursor: {}},
        $readPreference: {mode: "nearest", tags: [{tag: "secondary"}]},
        readConcern: {level: "local"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardSecondary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "agg_readPref",
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    // Find
    assert.commandWorked(mongosDB.runCommand({
        query: {find: "view", comment: "find_readPref", maxTimeMS: 5 * 60 * 1000},
        $readPreference: {mode: "nearest", tags: [{tag: "secondary"}]},
        readConcern: {level: "local"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardSecondary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "find_readPref",
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    // Count
    assert.commandWorked(mongosDB.runCommand({
        query: {count: "view", comment: "count_readPref"},
        $readPreference: {mode: "nearest", tags: [{tag: "secondary"}]},
        readConcern: {level: "local"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardSecondary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "count_readPref",
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    // Distinct
    assert.commandWorked(mongosDB.runCommand({
        query: {distinct: "view", key: "a", comment: "distinct_readPref"},
        $readPreference: {mode: "nearest", tags: [{tag: "secondary"}]},
        readConcern: {level: "local"}
    }));

    profilerHasSingleMatchingEntryOrThrow({
        profileDB: shardSecondary,
        filter: {
            "ns": coll.getFullName(),
            "command.aggregate": coll.getName(),
            "command.comment": "distinct_readPref",
            "command.pipeline.$mergeCursors": {"$exists": false},
            "nreturned": {"$exists": true}
        }
    });

    assert.commandWorked(shardSecondary.setProfilingLevel(0));
}

confirmOptionsInProfiler(st.rs1.getPrimary().getDB(mongosDB.getName()));

confirmReadPreference(st.rs0.getSecondary().getDB(mongosDB.getName()));
confirmReadPreference(st.rs1.getSecondary().getDB(mongosDB.getName()));

st.stop();
})();