summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/timeseries_insert_after_cycle_primary.js
blob: 44c922a6b794ff3c563e54d131c4d631681f2c8f (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
/**
 * Tests that time-series inserts are properly handled when a node steps down from primary and then
 * later steps back up.
 *
 * @tags: [
 *     requires_replication,
 * ]
 */
(function() {
'use strict';

load("jstests/core/timeseries/libs/timeseries.js");

const replTest = new ReplSetTest({nodes: 2});
replTest.startSet();
replTest.initiate();

const dbName = 'test';
const numColls = 3;

const testDB = function() {
    return replTest.getPrimary().getDB(dbName);
};

const coll = function(num) {
    return testDB()[jsTestName() + '_' + num];
};

const bucketsColl = function(num) {
    return testDB()['system.buckets.' + coll(num).getName()];
};

const timeFieldName = 'time';
const metaFieldName = 'meta';

const createColl = function(num) {
    assert.commandWorked(testDB().createCollection(
        coll(num).getName(), {timeseries: {timeField: timeFieldName, metaField: metaFieldName}}));
};

for (let i = 0; i < numColls; i++) {
    createColl(i);
}

const docs = [
    {_id: 0, [timeFieldName]: ISODate(), [metaFieldName]: 0},
    {_id: 1, [timeFieldName]: ISODate(), [metaFieldName]: 0},
];

for (let i = 0; i < numColls; i++) {
    assert.commandWorked(coll(i).insert(docs[0]));
}

replTest.stepUp(replTest.getSecondary());

// Manually update the bucket for collection 1.
assert.commandWorked(bucketsColl(1).update({}, {$set: {meta: 1}}));
assert.commandWorked(bucketsColl(1).update({}, {$set: {meta: 0}}));

// Drop, recreate, and reinsert the bucket for collection 2.
assert(coll(2).drop());
createColl(2);
assert.commandWorked(coll(2).insert(docs[0]));

// Step back up the original primary.
replTest.stepUp(replTest.getSecondary());

for (let i = 0; i < numColls; i++) {
    assert.commandWorked(coll(i).insert(docs[1]));
}

const checkColl = function(num, numBuckets) {
    jsTestLog('Checking collection ' + num);
    assert.docEq(docs, coll(num).find().sort({_id: 1}).toArray());
    const buckets = bucketsColl(num).find().toArray();
    assert.eq(buckets.length,
              numBuckets,
              'Expected ' + numBuckets + ' bucket(s) but found: ' + tojson(buckets));
};

// For collection 0, the original bucket should still be usable.
checkColl(0, 1);
if (TimeseriesTest.timeseriesScalabilityImprovementsEnabled(testDB())) {
    // We expect the buckets to be reopened by the new primary when inserting further measurements.
    checkColl(1, 1);
    checkColl(2, 1);
} else {
    // For collections 1 and 2, the original bucket should have been closed.
    checkColl(1, 2);
    checkColl(2, 2);
}

replTest.stopSet();
})();