@@ -2135,12 +2135,14 @@ export default class Collection extends ShellApiWithMongoClass {
2135
2135
@returnsPromise
2136
2136
@topologies ( [ Topologies . Sharded ] )
2137
2137
@apiVersions ( [ ] )
2138
- async getShardDistribution ( ) : Promise < CommandResult > {
2138
+ async getShardDistribution ( ) : Promise <
2139
+ CommandResult < GetShardDistributionResult >
2140
+ > {
2139
2141
this . _emitCollectionApiCall ( 'getShardDistribution' , { } ) ;
2140
2142
2141
2143
await getConfigDB ( this . _database ) ; // Warns if not connected to mongos
2142
2144
2143
- const result = { } as Document ;
2145
+ const result = { } as GetShardDistributionResult ;
2144
2146
const config = this . _mongo . getDB ( 'config' ) ;
2145
2147
2146
2148
const collStats = await (
@@ -2179,17 +2181,24 @@ export default class Collection extends ShellApiWithMongoClass {
2179
2181
. findOne ( { _id : extractedShardStats . shard } ) ,
2180
2182
config . getCollection ( 'chunks' ) . countDocuments ( countChunksQuery ) ,
2181
2183
] ) ;
2184
+
2185
+ // Since 6.0, there can be orphan documents indicated by numOrphanDocs.
2186
+ // These orphan documents need to be accounted for in the size calculation.
2187
+ const orphanDocumentsSize =
2188
+ ( extractedShardStats . storageStats . numOrphanDocs ?? 0 ) *
2189
+ ( extractedShardStats . storageStats . avgObjSize ?? 0 ) ;
2190
+ const ownedSize =
2191
+ extractedShardStats . storageStats . size - orphanDocumentsSize ;
2192
+
2182
2193
const shardStats = {
2183
2194
shardId : shard ,
2184
2195
host : host !== null ? host . host : null ,
2185
- size : extractedShardStats . storageStats . size ,
2196
+ size : ownedSize ,
2186
2197
count : extractedShardStats . storageStats . count ,
2187
2198
numChunks : numChunks ,
2188
2199
avgObjSize : extractedShardStats . storageStats . avgObjSize ,
2189
2200
} ;
2190
2201
2191
- const key = `Shard ${ shardStats . shardId } at ${ shardStats . host } ` ;
2192
-
2193
2202
// In sharded timeseries collections we do not have a count
2194
2203
// so we intentionally pass NaN as a result to the client.
2195
2204
const shardStatsCount : number = shardStats . count ?? NaN ;
@@ -2203,15 +2212,15 @@ export default class Collection extends ShellApiWithMongoClass {
2203
2212
? 0
2204
2213
: Math . floor ( shardStatsCount / shardStats . numChunks ) ;
2205
2214
2206
- result [ key ] = {
2215
+ result [ `Shard ${ shardStats . shardId } at ${ shardStats . host } ` ] = {
2207
2216
data : dataFormat ( coerceToJSNumber ( shardStats . size ) ) ,
2208
2217
docs : shardStatsCount ,
2209
2218
chunks : shardStats . numChunks ,
2210
2219
'estimated data per chunk' : dataFormat ( estimatedChunkDataPerChunk ) ,
2211
2220
'estimated docs per chunk' : estimatedDocsPerChunk ,
2212
2221
} ;
2213
2222
2214
- totals . size += coerceToJSNumber ( shardStats . size ) ;
2223
+ totals . size += coerceToJSNumber ( ownedSize ) ;
2215
2224
totals . count += coerceToJSNumber ( shardStatsCount ) ;
2216
2225
totals . numChunks += coerceToJSNumber ( shardStats . numChunks ) ;
2217
2226
@@ -2224,7 +2233,7 @@ export default class Collection extends ShellApiWithMongoClass {
2224
2233
data : dataFormat ( totals . size ) ,
2225
2234
docs : totals . count ,
2226
2235
chunks : totals . numChunks ,
2227
- } as Document ;
2236
+ } as GetShardDistributionResult [ 'Totals' ] ;
2228
2237
2229
2238
for ( const shardStats of conciseShardsStats ) {
2230
2239
const estDataPercent =
@@ -2243,7 +2252,8 @@ export default class Collection extends ShellApiWithMongoClass {
2243
2252
] ;
2244
2253
}
2245
2254
result . Totals = totalValue ;
2246
- return new CommandResult ( 'StatsResult' , result ) ;
2255
+
2256
+ return new CommandResult < GetShardDistributionResult > ( 'StatsResult' , result ) ;
2247
2257
}
2248
2258
2249
2259
@serverVersions ( [ '3.1.0' , ServerVersions . latest ] )
@@ -2467,3 +2477,24 @@ export default class Collection extends ShellApiWithMongoClass {
2467
2477
) ;
2468
2478
}
2469
2479
}
2480
+
2481
+ export type GetShardDistributionResult = {
2482
+ Totals : {
2483
+ data : string ;
2484
+ docs : number ;
2485
+ chunks : number ;
2486
+ } & {
2487
+ [ individualShardDistribution : `Shard ${string } `] : [
2488
+ `${number } % data`,
2489
+ `${number } % docs in cluster`,
2490
+ `${string } avg obj size on shard`
2491
+ ] ;
2492
+ } ;
2493
+ [ individualShardResult : `Shard ${string } at ${string } `] : {
2494
+ data : string ;
2495
+ docs : number ;
2496
+ chunks : number ;
2497
+ 'estimated data per chunk' : string ;
2498
+ 'estimated docs per chunk' : number ;
2499
+ } ;
2500
+ } ;
0 commit comments