summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/update_now_clustertime_replset.js
blob: 65503d7d5c3c45a529f2b81b9957dd4a8a73218f (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * Tests that the $$NOW and $$CLUSTER_TIME system variables can be used when performing updates on a
 * replica set.
 *
 * Tag this test as 'requires_find_command' to prevent it from running in the legacy passthroughs.
 * The 'requires_replication' tag prevents the test from running on variants with storage options
 * which cannot support a replica set.
 * @tags: [requires_find_command, requires_replication]
 */
(function() {
    "use strict";

    const rst = new ReplSetTest({name: jsTestName(), nodes: 1});
    rst.startSet();
    rst.initiate();

    const db = rst.getPrimary().getDB(jsTestName());
    const otherColl = db.other;
    const coll = db.test;
    otherColl.drop();
    coll.drop();

    // Insert N docs, with the _id field set to the current Date. We sleep for a short period
    // between insertions, such that the Date value increases for each successive document.
    let bulk = coll.initializeUnorderedBulkOp();
    const _idStart = new Date();
    const numDocs = 10;
    for (let i = 0; i < numDocs; ++i) {
        bulk.insert({_id: new Date(), insertClusterTime: new Timestamp(0, 0)});
        if (i < numDocs - 1) {
            sleep(100);
        }
    }
    const _idEnd = new Date();

    assert.commandWorked(bulk.execute());

    // Test that $$NOW and $$CLUSTER_TIME are available and remain constant across all updated
    // documents.
    let writeResult =
        assert.commandWorked(coll.update({$where: "sleep(10); return true"},
                                         [{$addFields: {now: "$$NOW", ctime: "$$CLUSTER_TIME"}}],
                                         {multi: true}));

    assert.eq(writeResult.nMatched, numDocs);
    assert.eq(writeResult.nModified, numDocs);

    let results = coll.find().toArray();
    assert.eq(results.length, numDocs);
    assert(results[0].now instanceof Date);
    assert(results[0].ctime instanceof Timestamp);
    for (let result of results) {
        assert.eq(result.now, results[0].now);
        assert.eq(result.ctime, results[0].ctime);
    }

    // Test that $$NOW and $$CLUSTER_TIME advance between updates but remain constant across all
    // updates in a given batch.
    writeResult = assert.commandWorked(db.runCommand({
        update: coll.getName(),
        updates: [
            {
              q: {$where: "sleep(10); return true"},
              u: [{$addFields: {now2: "$$NOW", ctime2: "$$CLUSTER_TIME"}}],
              multi: true
            },
            {
              q: {$where: "sleep(10); return true"},
              u: [{$addFields: {now3: "$$NOW", ctime3: "$$CLUSTER_TIME"}}],
              multi: true
            }
        ]
    }));

    assert.eq(writeResult.n, numDocs * 2);
    assert.eq(writeResult.nModified, numDocs * 2);

    results = coll.find().toArray();
    assert.eq(results.length, numDocs);
    assert(results[0].now2 instanceof Date);
    assert(results[0].ctime2 instanceof Timestamp);
    for (let result of results) {
        // The now2 and ctime2 fields are greater than the values from the previous update.
        assert.gt(result.now2, result.now);
        assert.gt(result.ctime2, result.ctime);
        // The now2 and ctime2 fields are the same across all documents.
        assert.eq(result.now2, results[0].now2);
        assert.eq(result.ctime2, results[0].ctime2);
        // The now2 and ctime2 fields are the same as now3 and ctime3 across all documents.
        assert.eq(result.now2, result.now3);
        assert.eq(result.ctime2, result.ctime3);
    }

    // Test that $$NOW and $$CLUSTER_TIME can be used in the query portion of an update.
    const _idMidpoint = new Date(_idStart.getTime() + (_idEnd.getTime() - _idStart.getTime()) / 2);
    writeResult =
        assert.commandWorked(coll.update({
            $expr: {
                $and: [
                    {$lt: ["$_id", {$min: [_idMidpoint, "$$NOW"]}]},
                    {$gt: ["$$CLUSTER_TIME", "$insertClusterTime"]}
                ]
            }
        },
                                         [{$addFields: {now4: "$$NOW", ctime4: "$$CLUSTER_TIME"}}],
                                         {multi: true}));

    assert.lt(writeResult.nMatched, numDocs);
    assert.lt(writeResult.nModified, numDocs);

    results = coll.find().sort({_id: 1}).toArray();
    assert.eq(results.length, numDocs);
    assert(results[0].now4 instanceof Date);
    assert(results[0].ctime4 instanceof Timestamp);
    for (let result of results) {
        if (result._id.getTime() < _idMidpoint.getTime()) {
            assert.eq(result.now4, results[0].now4);
            assert.eq(result.ctime4, results[0].ctime4);
            assert.gt(result.now4, result.now3);
            assert.gt(result.ctime4, result.ctime3);
        } else {
            assert.eq(result.now4, undefined);
            assert.eq(result.ctime4, undefined);
        }
    }

    // Test that we can explain() an update command that uses $$NOW and $$CLUSTER_TIME.
    assert.commandWorked(
        coll.explain().update(
            {
              $expr: {
                  $and: [
                      {$lt: ["$_id", {$min: [_idMidpoint, "$$NOW"]}]},
                      {$gt: ["$$CLUSTER_TIME", "$insertClusterTime"]}
                  ]
              }
            },
            [{$addFields: {explainDoesNotWrite1: "$$NOW", explainDoesNotWrite2: "$$CLUSTER_TIME"}}],
            {multi: true}));

    // Test that $$NOW and $$CLUSTER_TIME can be used when issuing updates via the Bulk API, and
    // remain constant across all updates within a single bulk operation.
    // TODO SERVER-41174: Note that if the bulk update operation exceeds the maximum BSON command
    // size, it may issue two or more separate update commands. $$NOW and $$CLUSTER_TIME will be
    // constant within each update command, but not across commands.
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({$where: "sleep(10); return true"}).update([
        {$addFields: {now5: "$$NOW", ctime5: "$$CLUSTER_TIME"}}
    ]);
    bulk.find({$where: "sleep(10); return true"}).update([
        {$addFields: {now6: "$$NOW", ctime6: "$$CLUSTER_TIME"}}
    ]);
    writeResult = assert.commandWorked(bulk.execute());

    assert.eq(writeResult.nMatched, numDocs * 2);
    assert.eq(writeResult.nModified, numDocs * 2);

    results = coll.find().toArray();
    assert.eq(results.length, numDocs);
    assert(results[0].now5 instanceof Date);
    assert(results[0].ctime5 instanceof Timestamp);
    for (let result of results) {
        // The now5 and ctime5 fields are the same across all documents.
        assert.eq(result.now5, results[0].now5);
        assert.eq(result.ctime5, results[0].ctime5);
        // The now5 and ctime5 fields are the same as now6 and ctime6 across all documents.
        assert.eq(result.now5, result.now6);
        assert.eq(result.ctime5, result.ctime6);
    }

    // Test that $$NOW and $$CLUSTER_TIME can be used in a findAndModify query and update.
    let returnedDoc = coll.findAndModify({
        query: {
            $expr: {
                $and: [
                    {$lt: ["$_id", {$min: [_idMidpoint, "$$NOW"]}]},
                    {$gt: ["$$CLUSTER_TIME", "$insertClusterTime"]}
                ]
            }
        },
        update: [{$addFields: {nowFAM: "$$NOW", ctimeFAM: "$$CLUSTER_TIME"}}],
        sort: {_id: 1},
        new: true
    });
    assert(returnedDoc.nowFAM instanceof Date);
    assert(returnedDoc.ctimeFAM instanceof Timestamp);
    assert.gt(returnedDoc.nowFAM, returnedDoc.now4);
    assert.gt(returnedDoc.ctimeFAM, returnedDoc.ctime4);

    results = coll.find({nowFAM: {$exists: true}, ctimeFAM: {$exists: true}}).toArray();
    assert.eq(results.length, 1);
    assert.docEq(results[0], returnedDoc);

    // Test that $$NOW and $$CLUSTER_TIME can be used in a findAndModify upsert.
    returnedDoc = coll.findAndModify({
        query: {fieldDoesNotExist: {$exists: true}},
        update:
            [{$addFields: {_id: "$$NOW", nowFAMUpsert: "$$NOW", ctimeFAMUpsert: "$$CLUSTER_TIME"}}],
        sort: {_id: 1},
        upsert: true,
        new: true
    });
    assert(returnedDoc.nowFAMUpsert instanceof Date);
    assert(returnedDoc.ctimeFAMUpsert instanceof Timestamp);

    assert.eq(coll.find().itcount(), numDocs + 1);
    results = coll.find({nowFAMUpsert: {$exists: true}, ctimeFAMUpsert: {$exists: true}}).toArray();
    assert.eq(results.length, 1);
    assert.docEq(results[0], returnedDoc);

    // Test that $$NOW and $$CLUSTER_TIME can be used in a findAndModify delete.
    returnedDoc = coll.findAndModify({
        query: {
            nowFAMUpsert: {$exists: true},
            ctimeFAMUpsert: {$exists: true},
            $expr: {
                $and: [
                    {$lt: ["$nowFAMUpsert", "$$NOW"]},
                    {$gt: ["$$CLUSTER_TIME", "$ctimeFAMUpsert"]}
                ]
            }
        },
        sort: {_id: 1},
        remove: true
    });
    assert.eq(coll.find({nowFAMUpsert: {$exists: true}}).itcount(), 0);
    assert.eq(coll.find().itcount(), numDocs);
    assert.neq(returnedDoc, null);

    // Test that we can explain() a findAndModify command that uses $$NOW and $$CLUSTER_TIME.
    assert.commandWorked(coll.explain().findAndModify({
        query: {
            $expr: {
                $and: [
                    {$lt: ["$_id", {$min: [_idMidpoint, "$$NOW"]}]},
                    {$gt: ["$$CLUSTER_TIME", "$insertClusterTime"]}
                ]
            }
        },
        update:
            [{$addFields: {explainDoesNotWrite1: "$$NOW", explainDoesNotWrite2: "$$CLUSTER_TIME"}}],
        sort: {_id: 1},
        new: true
    }));

    // Test that we can use $$NOW and $$CLUSTER_TIME in an update via a $merge aggregation. We first
    // use $merge to copy the current contents of 'coll' into 'otherColl'.
    assert.commandWorked(db.createCollection(otherColl.getName()));
    assert.doesNotThrow(() => coll.aggregate([
        {$merge: {into: otherColl.getName(), whenMatched: "fail", whenNotMatched: "insert"}}
    ]));
    // Run an aggregation which adds $$NOW and $$CLUSTER_TIME fields into the pipeline document,
    // then do the same to the documents in the output collection via a pipeline update.
    assert.doesNotThrow(() => coll.aggregate([
        {$addFields: {aggNow: "$$NOW", aggCT: "$$CLUSTER_TIME"}},
        {
          $merge: {
              into: otherColl.getName(),
              let : {aggNow: "$aggNow", aggCT: "$aggCT"},
              whenMatched: [{
                  $addFields: {
                      aggNow: "$$aggNow",
                      aggCT: "$$aggCT",
                      mergeNow: "$$NOW",
                      mergeCT: "$$CLUSTER_TIME"
                  }
              }],
              whenNotMatched: "fail"
          }
        }
    ]));
    // Verify that the agg pipeline's $$NOW and $$CLUSTER_TIME match the $merge update pipeline's.
    results = otherColl.find().toArray();
    assert.eq(results.length, numDocs);
    assert(results[0].mergeNow instanceof Date);
    assert(results[0].mergeCT instanceof Timestamp);
    for (let result of results) {
        // The mergeNow and mergeCT fields are greater than the values from the previous updates.
        assert.gt(result.mergeNow, result.now5);
        assert.gt(result.mergeCT, result.ctime5);
        // The mergeNow and mergeCT fields are the same across all documents.
        assert.eq(result.mergeNow, results[0].mergeNow);
        assert.eq(result.mergeCT, results[0].mergeCT);
        // The mergeNow and mergeCT fields are the same as aggNow and aggCT across all documents.
        assert.eq(result.mergeNow, result.aggNow);
        assert.eq(result.mergeCT, result.aggCT);
    }

    rst.stopSet();
}());