summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/opcounters_write_cmd.js
blob: 47a1bc6351592e0e8b058a1be543f59a729deeed (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
// Test that opcounters get incremented properly.

var mongo = new Mongo(db.getMongo().host);
mongo.forceWriteMode("commands");
var newdb = mongo.getDB(db.toString());

var t = newdb.opcounters;
var isMongos = ("isdbgrid" == newdb.runCommand("ismaster").msg);
var opCounters;
var res;

assert(t.getDB().getMongo().useWriteCommands(), "test is not running with write commands")

//
// Count ops attempted in write commands in mongod and mongos
//

//
// 1. Insert.
//
// - unordered insert of N:
//     counted as N ops, regardless of errors
// - ordered insert of N:
//     counted as K + 1 ops, where K is number of docs successfully inserted,
//     adding the failed attempt
//

t.drop();

// Single insert, no error.
opCounters = newdb.serverStatus().opcounters;
res = t.insert({_id:0});
assert.writeOK(res);
assert.eq(opCounters.insert + 1, newdb.serverStatus().opcounters.insert);

// Bulk insert, no error.
opCounters = newdb.serverStatus().opcounters;
res = t.insert([{_id:1},{_id:2}])
assert.writeOK(res);
assert.eq(opCounters.insert + 2, newdb.serverStatus().opcounters.insert);

// Single insert, with error.
opCounters = newdb.serverStatus().opcounters;
res = t.insert({_id:0})
assert.writeError(res);
assert.eq(opCounters.insert + 1, newdb.serverStatus().opcounters.insert);

// Bulk insert, with error, ordered.
opCounters = newdb.serverStatus().opcounters;
res = t.insert([{_id:3},{_id:3},{_id:4}])
assert.writeError(res);
assert.eq(opCounters.insert + 2, newdb.serverStatus().opcounters.insert);

// Bulk insert, with error, unordered.
var continueOnErrorFlag = 1;
opCounters = newdb.serverStatus().opcounters;
res = t.insert([{_id:5},{_id:5},{_id:6}], continueOnErrorFlag)
assert.writeError(res);
assert.eq(opCounters.insert + 3, newdb.serverStatus().opcounters.insert);

//
// 2. Update.
//

t.drop();
t.insert({_id:0});

// Update, no error.
opCounters = newdb.serverStatus().opcounters;
res = t.update({_id:0}, {$set:{a:1}});
assert.writeOK(res);
assert.eq(opCounters.update + 1, newdb.serverStatus().opcounters.update);

// Update, with error.
opCounters = newdb.serverStatus().opcounters;
res = t.update({_id:0}, {$set:{_id:1}});
assert.writeError(res);
assert.eq(opCounters.update + 1, newdb.serverStatus().opcounters.update);

//
// 3. Delete.
//

t.drop();
t.insert([{_id:0},{_id:1}]);

// Delete, no error.
opCounters = newdb.serverStatus().opcounters;
res = t.remove({_id:0});
assert.writeOK(res);
assert.eq(opCounters.delete + 1, newdb.serverStatus().opcounters.delete);

// Delete, with error.
opCounters = newdb.serverStatus().opcounters;
res = t.remove({_id:{$invalidOp:1}});
assert.writeError(res);
assert.eq(opCounters.delete + 1, newdb.serverStatus().opcounters.delete);

//
// 4. Query.
//
// - mongod: counted as 1 op, regardless of errors
// - mongos: counted as 1 op if successful, else 0
//

t.drop();
t.insert({_id:0});

// Query, no error.
opCounters = newdb.serverStatus().opcounters;
t.findOne();
assert.eq(opCounters.query + 1, newdb.serverStatus().opcounters.query);

// Query, with error.
opCounters = newdb.serverStatus().opcounters;
assert.throws(function() { t.findOne({_id:{$invalidOp:1}}) });
assert.eq(opCounters.query + (isMongos ? 0 : 1), newdb.serverStatus().opcounters.query);

//
// 5. Getmore.
//
// - counted as 1 op per getmore issued, regardless of errors
//

t.drop();
t.insert([{_id:0},{_id:1},{_id:2}]);

// Getmore, no error.
opCounters = newdb.serverStatus().opcounters;
t.find().batchSize(2).toArray(); // 3 documents, batchSize=2 => 1 query + 1 getmore
assert.eq(opCounters.query + 1, newdb.serverStatus().opcounters.query);
assert.eq(opCounters.getmore + 1, newdb.serverStatus().opcounters.getmore);

// Getmore, with error (TODO implement when SERVER-5813 is resolved).

//
// 6. Command.
//
// - unrecognized commands not counted
// - recognized commands counted as 1 op, regardless of errors
// - some (recognized) commands can suppress command counting (i.e. aren't counted as commands)
//

t.drop();
t.insert({_id:0})

// Command, recognized, no error.
opCounters = newdb.serverStatus().opcounters;
assert.eq(opCounters.command + 1, newdb.serverStatus().opcounters.command); // "serverStatus" counted

// Command, recognized, with error.
opCounters = newdb.serverStatus().opcounters;
res = t.runCommand("count", {query:{$invalidOp:1}});
assert.eq(0, res.ok);
assert.eq(opCounters.command + 2,
          newdb.serverStatus().opcounters.command); // "serverStatus", "count" counted

// Command, unrecognized.
opCounters = newdb.serverStatus().opcounters;
res = t.runCommand("command that doesn't exist");
assert.eq(0, res.ok);
//assert.eq(opCounters.command + 1, newdb.serverStatus().opcounters.command); // "serverStatus" counted
// TODO Replace below with above when SERVER-9038 is resolved (mongos counts unrecognized commands)
assert.eq(opCounters.command + (isMongos ? 2 : 1), newdb.serverStatus().opcounters.command);

// Command, recognized, counting suppressed (TODO implement when SERVER-9038 is resolved).