Skip to content

Commit a24d4e6

Browse files
silviasuhuMongoDB Bot
authored andcommitted
SERVER-88400 shardedDataDistribution aggregation stage must not return null fields for timeseries (#32896)
GitOrigin-RevId: 0d4d8b564f0ceaf0d4be922a2b2b1665578831f8
1 parent 700131f commit a24d4e6

File tree

3 files changed

+97
-24
lines changed

3 files changed

+97
-24
lines changed

etc/backports_required_for_multiversion_tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ last-continuous:
577577
ticket: SERVER-73641
578578
- test_file: jstests/change_streams/change_streams_namespace_match_expressions.js
579579
ticket: SERVER-100489
580+
- test_file: jstests/sharding/sharded_data_distribution.js
581+
ticket: SERVER-88400
580582
suites: null
581583
last-lts:
582584
all:
@@ -1206,4 +1208,6 @@ last-lts:
12061208
ticket: SERVER-73641
12071209
- test_file: jstests/change_streams/change_streams_namespace_match_expressions.js
12081210
ticket: SERVER-100489
1211+
- test_file: jstests/sharding/sharded_data_distribution.js
1212+
ticket: SERVER-88400
12091213
suites: null

jstests/sharding/sharded_data_distribution.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,21 @@ const testDb = mongos.getDB("test");
8383
const barDb = mongos.getDB("bar");
8484
const fooColl = testDb.getCollection("foo");
8585
const bazColl = barDb.getCollection("baz");
86+
const timeseriesColl = testDb.getCollection("timeseriesColl");
8687

8788
st.adminCommand({enablesharding: testDb.getName(), primaryShard: st.shard1.shardName});
8889
st.adminCommand({shardcollection: ns1, key: {skey: 1}});
90+
st.adminCommand(
91+
{shardcollection: timeseriesColl.getFullName(), timeseries: {timeField: "ts"}, key: {ts: 1}});
8992
st.adminCommand({enablesharding: barDb.getName(), primaryShard: st.shard1.shardName});
9093
st.adminCommand({shardcollection: ns2, key: {skey: 1}});
9194

95+
assert.commandWorked(timeseriesColl.insertOne({
96+
"metadata": {"sensorId": 5578, "type": "temperature"},
97+
"ts": ISODate("2021-05-18T00:00:00.000Z"),
98+
"temp": 12
99+
}));
100+
92101
// Insert data to validate the aggregation stage
93102
for (let i = 0; i < 6; i++) {
94103
assert.commandWorked(fooColl.insert({skey: i}));
@@ -168,6 +177,23 @@ assert.neq(
168177
0,
169178
adminDb.aggregate([{$shardedDataDistribution: {}}, {$match: {shards: {$size: 2}}}]).itcount());
170179

180+
// Test that verifies that the fields returned for timeseries collections are correct.
181+
assert.eq(1,
182+
adminDb
183+
.aggregate([
184+
{$shardedDataDistribution: {}},
185+
{$match: {ns: "test.system.buckets.timeseriesColl"}},
186+
{
187+
$match: {
188+
$and: [
189+
{"shards.numOwnedDocuments": {$eq: 1}},
190+
{"shards.ownedSizeBytes": {$gt: 0}},
191+
{"shards.orphanedSizeBytes": {$eq: 0}}
192+
]
193+
}
194+
}
195+
])
196+
.itcount());
171197
st.stop();
172198

173199
// Test that verifies the behavior in unsharded deployments

src/mongo/db/pipeline/document_source_sharded_data_distribution.cpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,81 @@ list<intrusive_ptr<DocumentSource>> DocumentSourceShardedDataDistribution::creat
7171
"shard": 1,
7272
"storageStats.count": 1,
7373
"storageStats.numOrphanDocs": 1,
74-
"storageStats.avgObjSize": 1
74+
"storageStats.avgObjSize": 1,
75+
"timeseries": {$ifNull: ["$storageStats.timeseries", null]}
7576
}
7677
})");
78+
79+
// Compute the `numOrphanedDocs` and `numOwnedDocuments` fields.
80+
// Note that, for timeseries collections, these fields will report the number of buckets
81+
// instead of the number of documents. We've decided to keep the field names as they are to
82+
// avoid the downstream impact of having to check different fields depending on the collection
83+
// time.
7784
static const BSONObj kGroupObj = fromjson(R"({
7885
$group: {
7986
_id: "$ns",
8087
shards: {
8188
$push: {
82-
$let: {
83-
vars: {
84-
nOwnedDocs: {
85-
$subtract: [
86-
"$storageStats.count",
87-
"$storageStats.numOrphanDocs"
88-
]
89-
}
89+
$cond: {
90+
if: {
91+
$eq: ["$timeseries", null]
9092
},
91-
in: {
92-
shardName: "$shard",
93-
numOrphanedDocs: "$storageStats.numOrphanDocs",
94-
numOwnedDocuments: "$$nOwnedDocs",
95-
ownedSizeBytes: {
96-
$multiply: [
97-
"$storageStats.avgObjSize",
98-
"$$nOwnedDocs"
99-
]
100-
},
101-
orphanedSizeBytes: {
102-
$multiply: [
103-
"$storageStats.avgObjSize",
104-
"$storageStats.numOrphanDocs"
105-
]
93+
then: {
94+
$let: {
95+
vars: {
96+
nOwnedDocs: {
97+
$subtract: [
98+
"$storageStats.count",
99+
"$storageStats.numOrphanDocs"
100+
]
101+
}
102+
},
103+
in: {
104+
shardName: "$shard",
105+
numOrphanedDocs: "$storageStats.numOrphanDocs",
106+
numOwnedDocuments: "$$nOwnedDocs",
107+
ownedSizeBytes: {
108+
$multiply: [
109+
"$storageStats.avgObjSize",
110+
"$$nOwnedDocs"
111+
]
112+
},
113+
orphanedSizeBytes: {
114+
$multiply: [
115+
"$storageStats.avgObjSize",
116+
"$storageStats.numOrphanDocs"
117+
]
118+
}
119+
}
120+
}
121+
},
122+
else: {
123+
$let: {
124+
vars: {
125+
nOwnedDocs: {
126+
$subtract: [
127+
"$timeseries.bucketCount",
128+
"$storageStats.numOrphanDocs"
129+
]
130+
}
131+
},
132+
in: {
133+
shardName: "$shard",
134+
numOrphanedDocs: "$storageStats.numOrphanDocs",
135+
numOwnedDocuments: "$$nOwnedDocs",
136+
ownedSizeBytes: {
137+
$multiply: [
138+
"$timeseries.avgBucketSize",
139+
"$$nOwnedDocs"
140+
]
141+
},
142+
orphanedSizeBytes: {
143+
$multiply: [
144+
"$timeseries.avgBucketSize",
145+
"$storageStats.numOrphanDocs"
146+
]
147+
}
148+
}
106149
}
107150
}
108151
}

0 commit comments

Comments
 (0)