-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAllChunkInfo.js
50 lines (44 loc) · 1.92 KB
/
AllChunkInfo.js
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
// This is a simple function, which takes just two arguments:
// ns: a string representing the sharded namespace to be examined
// est: a boolean to determine whether or not to use the estimate option (recommended generally)
// It is called from the mongos like so:
// AllChunkInfo("database.collection", true);
// Currently the output is CSV, will add options for other output later
sh.printAllChunkInfo = function(ns, est) {
var configDB = db.getSiblingDB("config");
var chunks = configDB.chunks.find({ns: ns}).sort({min: 1});
var key = configDB.collections.findOne({_id: ns}).key;
var total = { chunks: 0, objs: 0, size: 0, empty: 0 };
var shards = {};
configDB.shards.find().toArray().forEach( function (shard) {
shards[shard._id] = { chunks: 0, objs: 0, size: 0, empty: 0 };
} );
print("ChunkID,Shard,ChunkSize,ObjectsInChunk");
chunks.forEach( function printChunkInfo(chunk) {
var res = db.getSiblingDB(chunk.ns.split(".")[0]).runCommand({ datasize: chunk.ns, keyPattern: key, min: chunk.min, max: chunk.max, estimate: est });
print(chunk._id + "," + chunk.shard + "," + res.size + "," + res.numObjects);
(function(stats) {
for (stat in stats) {
stats[stat].chunks++;
stats[stat].objs += res.numObjects;
stats[stat].size += res.size;
if (res.size == 0) stats[stat].empty++;
}
})( [ total, shards[chunk.shard] ] );
} );
function printStats(s, indent) {
print(indent + "Total Chunks: " + s.chunks + " (" + s.empty + " empty)");
print(indent + "Total Size: " + s.size + " bytes");
print(indent + "Average Chunk Size: " + (s.size/s.chunks) + " bytes");
print(indent + "Average Non-empty Chunk Size: " + (s.size/(s.chunks-s.empty)) + " bytes");
}
print("");
print("*********** Summary Information ***********");
printStats(total, "");
print("");
print("*********** Per-Shard Information ***********");
for (shard in shards) {
print("Shard " + shard + ":");
printStats(shards[shard], " ");
}
}