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
|
/**
* Tests that the cluster collStats command returns timeseries statistics in the expected format.
*
* For legacy collStats command:
* {
* ....,
* "ns" : ...,
* ....,
* "timeseries" : {
* .... (sums the shards' field values)
* }
* ....,
* "shards" : {
* <shardName> {
* "timeseries" : {
* .... (single shard's field values)
* }
* ....
* }
* }
* ....
* }
*
* For aggregate $collStats stage:
* [
* {
* ....,
* "ns" : ...,
* "shard" : ...,
* "latencyStats" : {
* ....
* },
* "storageStats" : {
* ...,
* "timeseries" : {
* ...,
* },
* },
* "count" : {
* ....
* },
* "queryExecStats" : {
* ....
* },
* },
* {
* .... (Other shard's result)
* },
* ...
* ]
*
* @tags: [
* requires_fcv_71
* ]
*/
(function() {
load("jstests/core/timeseries/libs/timeseries.js");
const numShards = 2;
const st = new ShardingTest({shards: numShards});
if (!TimeseriesTest.shardedtimeseriesCollectionsEnabled(st.shard0)) {
jsTestLog("Skipping test because the sharded time-series collection feature flag is disabled");
st.stop();
return;
}
const dbName = 'testDB';
const collName = 'testColl';
const mongosDB = st.s.getDB(dbName);
const mongosColl = mongosDB.getCollection(collName);
const timeField = 'tm';
const metaField = 'mt';
const viewNs = `${dbName}.${collName}`;
const bucketNs = `${dbName}.system.buckets.${collName}`;
// Create a timeseries collection.
assert.commandWorked(mongosDB.createCollection(
collName, {timeseries: {timeField: timeField, metaField: metaField}}));
// Populate the timeseries collection with some data. More interesting test case, and populates the
// statistics results.
const numberDoc = 20;
for (let i = 0; i < numberDoc; i++) {
assert.commandWorked(mongosColl.insert({[timeField]: ISODate(), [metaField]: i}));
}
assert.eq(mongosColl.find().itcount(), numberDoc);
// The cluster collStats command should pull the shard's 'timeseries' data to the top level of the
// command results.
let clusterCollStatsResult = assert.commandWorked(mongosDB.runCommand({collStats: collName}));
jsTestLog("Cluster collStats command result: " + tojson(clusterCollStatsResult));
assert(clusterCollStatsResult.timeseries,
"Expected a top-level 'timeseries' field but didn't find one: " +
tojson(clusterCollStatsResult));
// Check that the top-level 'timeseries' fields match the primary shard's, that the stats were
// correctly pulled up.
const primaryShard = st.getPrimaryShard(dbName);
const otherShard = st.getOther(primaryShard);
assert(
clusterCollStatsResult.shards[primaryShard.shardName].timeseries,
"Expected a shard 'timeseries' field but didn't find one: " + tojson(clusterCollStatsResult));
assert.docEq(clusterCollStatsResult.timeseries,
clusterCollStatsResult.shards[primaryShard.shardName].timeseries);
// Shard the timeseries collection
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
assert.commandWorked(mongosColl.createIndex({[metaField]: 1}));
assert.commandWorked(st.s.adminCommand({
shardCollection: viewNs,
key: {[metaField]: 1},
}));
// Force splitting numShards chunks.
const splitPoint = {
meta: numberDoc / numShards
};
assert.commandWorked(st.s.adminCommand({split: bucketNs, middle: splitPoint}));
// Ensure that currently both chunks reside on the primary shard.
let counts = st.chunkCounts(`system.buckets.${collName}`, dbName);
assert.eq(numShards, counts[primaryShard.shardName]);
// Move one of the chunks into the second shard.
assert.commandWorked(st.s.adminCommand(
{movechunk: bucketNs, find: splitPoint, to: otherShard.name, _waitForDelete: true}));
// Ensure that each shard owns one chunk.
counts = st.chunkCounts(`system.buckets.${collName}`, dbName);
assert.eq(1, counts[primaryShard.shardName], counts);
assert.eq(1, counts[otherShard.shardName], counts);
// Do some insertion after moving chunks.
for (let i = 0; i < numberDoc; i++) {
assert.commandWorked(mongosColl.insert({[timeField]: ISODate(), [metaField]: i}));
}
assert.eq(mongosColl.find().itcount(), numberDoc * 2);
function checkAllFieldsAreInResult(result) {
assert(result.hasOwnProperty("latencyStats"), result);
assert(result.hasOwnProperty("storageStats"), result);
assert(result.hasOwnProperty("count"), result);
assert(result.hasOwnProperty("queryExecStats"), result);
}
function assertTimeseriesAggregationCorrectness(total, shards) {
assert(shards.every(x => x.bucketNs === total.bucketNs));
assert.eq(total.bucketCount,
shards.map(x => x.bucketCount).reduce((x, y) => x + y, 0 /* initial value */));
if (total.bucketCount > 0) {
assert.eq(total.avgBucketSize,
Math.floor(shards.map(x => x.avgBucketSize * x.bucketCount)
.reduce((x, y) => x + y, 0 /* initial value */) /
total.bucketCount));
} else {
assert.eq(total.avgBucketSize, 0);
}
assert.eq(total.numBucketInserts,
shards.map(x => x.numBucketInserts).reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numBucketUpdates,
shards.map(x => x.numBucketUpdates).reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numBucketsOpenedDueToMetadata,
shards.map(x => x.numBucketsOpenedDueToMetadata)
.reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numBucketsClosedDueToCount,
shards.map(x => x.numBucketsClosedDueToCount)
.reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numBucketsClosedDueToSize,
shards.map(x => x.numBucketsClosedDueToSize)
.reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numBucketsClosedDueToTimeForward,
shards.map(x => x.numBucketsClosedDueToTimeForward)
.reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numBucketsClosedDueToTimeBackward,
shards.map(x => x.numBucketsClosedDueToTimeBackward)
.reduce((x, y) => x + y, 0) /* initial value */);
assert.eq(total.numBucketsClosedDueToMemoryThreshold,
shards.map(x => x.numBucketsClosedDueToMemoryThreshold)
.reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numCommits,
shards.map(x => x.numCommits).reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(total.numWaits,
shards.map(x => x.numWaits).reduce((x, y) => x + y, 0 /* initial value */));
assert.eq(
total.numMeasurementsCommitted,
shards.map(x => x.numMeasurementsCommitted).reduce((x, y) => x + y, 0 /* initial value */));
assert(total.bucketCount > 0);
assert(total.avgBucketSize > 0);
assert(total.numBucketInserts > 0);
assert(total.numCommits > 0);
assert(total.numMeasurementsCommitted > 0);
}
function verifyClusterCollStatsResult(
clusterCollStatsResult, sumTimeseriesStatsAcrossShards, isAggregation) {
if (isAggregation) {
// $collStats should output one document per shard.
assert.eq(clusterCollStatsResult.length,
numShards,
"Expected " + numShards +
"documents to be returned: " + tojson(clusterCollStatsResult));
checkAllFieldsAreInResult(clusterCollStatsResult[0]);
checkAllFieldsAreInResult(clusterCollStatsResult[1]);
}
assert(sumTimeseriesStatsAcrossShards,
"Expected an aggregated 'timeseries' field but didn't find one: " +
tojson(clusterCollStatsResult));
const primaryShardStats = isAggregation
? clusterCollStatsResult[0].storageStats.timeseries
: clusterCollStatsResult.shards[primaryShard.shardName].timeseries;
const otherShardStats = isAggregation
? clusterCollStatsResult[1].storageStats.timeseries
: clusterCollStatsResult.shards[otherShard.shardName].timeseries;
// Check that the top-level 'timeseries' fields match the sum of two shard's, that the stats
// were correctly aggregated.
assert(primaryShardStats,
"Expected a shard 'timeseries' field on shard " + primaryShard.shardName +
" but didn't find one: " + tojson(clusterCollStatsResult));
assert(otherShardStats,
"Expected a shard 'timeseries' field on shard " + otherShard.shardName +
" but didn't find one: " + tojson(clusterCollStatsResult));
assertTimeseriesAggregationCorrectness(sumTimeseriesStatsAcrossShards,
[primaryShardStats, otherShardStats]);
}
// Tests that the output of the collStats command returns results from both the shards and
// includes all the expected fields.
clusterCollStatsResult = assert.commandWorked(mongosDB.runCommand({collStats: collName}));
jsTestLog("Sharded cluster collStats command result: " + tojson(clusterCollStatsResult));
const sumTimeseriesStatsAcrossShards = clusterCollStatsResult.timeseries;
verifyClusterCollStatsResult(
clusterCollStatsResult, sumTimeseriesStatsAcrossShards, false // isAggregation
);
// Tests that the output of the $collStats stage returns results from both the shards and includes
// all the expected fields.
clusterCollStatsResult =
mongosColl
.aggregate(
[{$collStats: {latencyStats: {}, storageStats: {}, count: {}, queryExecStats: {}}}])
.toArray();
jsTestLog("Sharded cluster collStats aggregation result: " + tojson(clusterCollStatsResult));
// Use the same sumTimeseriesStatsAcrossShards value as the collStats command since
// aggregation does not sum up timeseries stats results. This will also verify that the results
// output by collStats in find and aggregation are the same.
verifyClusterCollStatsResult(
clusterCollStatsResult, sumTimeseriesStatsAcrossShards, true // isAggregation
);
st.stop();
})();
|