summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/serverstatus_index_stats.js
blob: 5374848b4626437109e677eb53fc3fc6254e8025 (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
/**
 * Tests that serverStatus contains an indexStats section. This section reports globally-aggregated
 * statistics about features in use by indexes and how often they are used.
 *
 * @tags: [
 *   requires_persistence,
 *   requires_replication,
 * ]
 */
(function() {
"use strict";

const assertStats = (db, assertFn) => {
    const stats = db.serverStatus().indexStats;
    try {
        assertFn(stats);
    } catch (e) {
        print("result: " + tojson(stats));
        throw e;
    }
};

// If new features are added, they must also be added to this list or the test will fail.
const knownFeatures = [
    "2d",
    "2dsphere",
    "2dsphere_bucket",
    "collation",
    "columnstore",
    "compound",
    "hashed",
    "id",
    "normal",
    "partial",
    "single",
    "sparse",
    "text",
    "ttl",
    "unique",
    "wildcard",
];

const assertZeroCounts = (db) => {
    assertStats(db, (featureStats) => {
        assert.eq(featureStats.count, 0);
        for (const [feature, stats] of Object.entries(featureStats.features)) {
            assert.contains(feature, knownFeatures, "unknown feature reported by indexStats");
            assert.eq(0, stats.count, feature);
        }
    });
};

const assertZeroAccess = (db) => {
    assertStats(db, (featureStats) => {
        for (const [feature, stats] of Object.entries(featureStats.features)) {
            assert.contains(feature, knownFeatures, "unknown feature reported by indexStats");
            assert.eq(0, stats.accesses, feature);
        }
    });
};

const assertCountIncrease = (last, current, inc) => {
    assert.eq(last.count + inc, current.count, "incorrect index count");
};

const assertFeatureCountIncrease = (last, current, feature, inc) => {
    assert.eq(last.features[feature].count + inc,
              current.features[feature].count,
              "incorrect feature count for " + feature);
};

const assertFeatureAccessIncrease = (last, current, feature, inc) => {
    assert.eq(last.features[feature].accesses + inc,
              current.features[feature].accesses,
              "incorrect feature accesses for " + feature);
};

const replSet = new ReplSetTest({nodes: 1});
replSet.startSet();
replSet.initiate();

let primary = replSet.getPrimary();
let db = primary.getDB('test');

assertZeroCounts(db);
assertZeroAccess(db);

let lastStats = db.serverStatus().indexStats;

assert.commandWorked(db.testColl.createIndex({twoD: '2d', b: 1}, {unique: true, sparse: true}));
assert.commandWorked(db.testColl.insert({twoD: [0, 0], b: 1}));
assert.eq(1, db.testColl.find({twoD: {$geoNear: [0, 0]}}).itcount());
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 2);
    assertFeatureCountIncrease(lastStats, stats, '2d', 1);
    assertFeatureCountIncrease(lastStats, stats, 'compound', 1);
    // The index build implicitly created the collection, which also builds an _id index.
    assertFeatureCountIncrease(lastStats, stats, 'id', 1);
    assertFeatureCountIncrease(lastStats, stats, 'sparse', 1);
    // Note that the _id index is not included in this unique counter. This is due to a quirk in the
    // _id index spec that does not actually have a unique:true property.
    assertFeatureCountIncrease(lastStats, stats, 'unique', 1);

    assertFeatureAccessIncrease(lastStats, stats, '2d', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'compound', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'id', 0);
    assertFeatureAccessIncrease(lastStats, stats, 'sparse', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'unique', 1);
});

lastStats = db.serverStatus().indexStats;

assert.commandWorked(db.testColl.createIndex({sphere: '2dsphere'}));
assert.commandWorked(db.testColl.insert({sphere: {type: "Point", coordinates: [0, 0]}}));
assert.eq(1,
          db.testColl
              .aggregate([{
                  $geoNear: {
                      near: {type: "Point", coordinates: [1, 1]},
                      key: 'sphere',
                      distanceField: 'dist',
                  }
              }])
              .itcount());
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 1);
    assertFeatureCountIncrease(lastStats, stats, '2dsphere', 1);
    assertFeatureCountIncrease(lastStats, stats, 'single', 1);

    assertFeatureAccessIncrease(lastStats, stats, '2dsphere', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'single', 1);
});

lastStats = db.serverStatus().indexStats;

assert.commandWorked(
    db.testColl.createIndex({hashed: 'hashed', p: 1}, {partialFilterExpression: {p: 1}}));
assert.commandWorked(db.testColl.insert({hashed: 1, p: 1}));
assert.eq(1, db.testColl.find({hashed: 1}).hint({hashed: 'hashed', p: 1}).itcount());
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 1);
    assertFeatureCountIncrease(lastStats, stats, 'compound', 1);
    assertFeatureCountIncrease(lastStats, stats, 'hashed', 1);
    assertFeatureCountIncrease(lastStats, stats, 'partial', 1);

    assertFeatureAccessIncrease(lastStats, stats, 'compound', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'hashed', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'partial', 1);
});

lastStats = db.serverStatus().indexStats;

assert.commandWorked(
    db.testColl.createIndex({a: 1}, {expireAfterSeconds: 3600, collation: {locale: 'en'}}));
let now = new Date();
assert.commandWorked(db.testColl.insert({a: now}));
assert.eq(1, db.testColl.find({a: now}).itcount());
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 1);
    assertFeatureCountIncrease(lastStats, stats, 'collation', 1);
    assertFeatureCountIncrease(lastStats, stats, 'normal', 1);
    assertFeatureCountIncrease(lastStats, stats, 'single', 1);
    assertFeatureCountIncrease(lastStats, stats, 'ttl', 1);

    assertFeatureAccessIncrease(lastStats, stats, 'collation', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'normal', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'single', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'ttl', 1);
});

lastStats = db.serverStatus().indexStats;

assert.commandWorked(db.testColl.createIndex({text: 'text'}));
assert.commandWorked(db.testColl.insert({text: "a string"}));
assert.eq(1, db.testColl.find({$text: {$search: "string"}}).itcount());
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 1);
    // Text indexes are internally compound, but that should not be reflected in the stats.
    assertFeatureCountIncrease(lastStats, stats, 'compound', 0);
    assertFeatureCountIncrease(lastStats, stats, 'text', 1);

    assertFeatureAccessIncrease(lastStats, stats, 'compound', 0);
    assertFeatureAccessIncrease(lastStats, stats, 'text', 1);
});

lastStats = db.serverStatus().indexStats;

assert.commandWorked(db.testColl.createIndex({'wild.$**': 1}));
assert.commandWorked(db.testColl.insert({wild: {a: 1}}));
assert.eq(1, db.testColl.find({'wild.a': 1}).itcount());
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 1);
    assertFeatureCountIncrease(lastStats, stats, 'single', 1);
    assertFeatureCountIncrease(lastStats, stats, 'wildcard', 1);

    assertFeatureAccessIncrease(lastStats, stats, 'single', 1);
    assertFeatureAccessIncrease(lastStats, stats, 'wildcard', 1);
});

lastStats = db.serverStatus().indexStats;

const timeSeriesMetricIndexesEnabled =
    assert.commandWorked(db.adminCommand({getParameter: 1, featureFlagTimeseriesMetricIndexes: 1}))
        .featureFlagTimeseriesMetricIndexes.value;
if (timeSeriesMetricIndexesEnabled) {
    assert.commandWorked(db.createCollection('ts', {timeseries: {timeField: 't'}}));
    assert.commandWorked(db.ts.createIndex({loc: '2dsphere'}));
    assert.commandWorked(db.ts.insert({t: new Date(), loc: [0, 0]}));
    assert.eq(1,
              db.ts
                  .aggregate([{
                                 $geoNear: {
                                     near: [1, 1],
                                     key: 'loc',
                                     distanceField: 'dist',
                                 }
                             }],
                             {hint: 'loc_2dsphere'})
                  .itcount());
    assertStats(db, (stats) => {
        // Includes _id index built for system.views.
        assertCountIncrease(lastStats, stats, 2);
        assertFeatureCountIncrease(lastStats, stats, 'id', 1);
        assertFeatureCountIncrease(lastStats, stats, 'single', 1);
        assertFeatureCountIncrease(lastStats, stats, '2dsphere_bucket', 1);

        assertFeatureAccessIncrease(lastStats, stats, 'id', 0);
        assertFeatureAccessIncrease(lastStats, stats, 'single', 1);
        assertFeatureAccessIncrease(lastStats, stats, '2dsphere_bucket', 1);
    });
}

lastStats = db.serverStatus().indexStats;

const columnstoreIndexesEnabled =
    assert.commandWorked(db.adminCommand({getParameter: 1, featureFlagColumnstoreIndexes: 1}))
        .featureFlagColumnstoreIndexes.value;
if (columnstoreIndexesEnabled) {
    // TODO SERVER-61644 (or sooner) should support accessing/using index and seeing that reflected.
    assert.commandWorked(db.testColl.createIndex({'$**': 'columnstore'}));
    assertStats(db, (stats) => {
        assertCountIncrease(lastStats, stats, 1);
        assertFeatureCountIncrease(lastStats, stats, 'columnstore', 1);

        assertFeatureAccessIncrease(lastStats, stats, 'id', 0);
        assertFeatureAccessIncrease(lastStats, stats, 'columnstore', 0);
    });
}

lastStats = db.serverStatus().indexStats;

// After restarting the server, we expect all of the access counters to reset to zero, but that the
// feature counters remain the same as before startup.
replSet.stopSet(undefined, /* restart */ true);
replSet.startSet({}, /* restart */ true);
primary = replSet.getPrimary();
db = primary.getDB('test');

assertZeroAccess(db);
assertStats(db, (stats) => {
    assertCountIncrease(lastStats, stats, 0);

    const features = stats.features;
    for (const [feature, _] of Object.entries(features)) {
        assert.contains(feature, knownFeatures);
        assertFeatureCountIncrease(lastStats, stats, feature, 0);
    }
});

assert.commandWorked(db.dropDatabase());
assertZeroCounts(db);

replSet.stopSet();
})();