Skip to content

Instantly share code, notes, and snippets.

@jasoares
Last active May 15, 2017 17:26
Show Gist options
  • Save jasoares/df0a3d65c79f18c27000 to your computer and use it in GitHub Desktop.
Save jasoares/df0a3d65c79f18c27000 to your computer and use it in GitHub Desktop.
MongoDB Shard distribution and balancer scripts
var hugeCollections = db.chunks.aggregate([{ '$group': { _id: { ns: '$ns', shard: '$shard' }, count: { '$sum': 1 } } }, { '$match': { count: { '$gt': 100 } } }, { '$group': { _id: { ns: '$_id.ns' }, count: { '$max': '$count' } } }, { '$project': { _id: 0, coll: '$_id.ns', shard: '$_id.shard', count: '$count' } }, { '$sort': { count: -1 } }]); while(hugeCollections.hasNext()) { var coll = hugeCollections.next()['coll']; sh.enableBalancing(coll); }
var allCollections = db.collections.find(); while(allCollections.hasNext()) { var coll = allCollections.next()['_id']; sh.disableBalancing(coll); }
var allCollections = db.collections.find(); while(allCollections.hasNext()) { var coll = allCollections.next()['_id']; if(db.getSiblingDB('config').collections.findOne({ _id: coll }).noBalance == false) { print(coll + ': ' + db.getSiblingDB('config').collections.findOne({ _id: coll }).noBalance); } }
db.chunks.aggregate([{ '$match': { _id: /db_name/ } }, { '$group': { _id: { ns: '$ns', shard: '$shard' }, count: { '$sum': 1 } } }, { '$sort': { '_id.ns': 1, '_id.shard': 1 } }])
var unbalancedCollections = db.chunks.aggregate([{ '$group': { _id: { ns: '$ns', shard: '$shard' }, count: { '$sum': 1 } } }, { '$group': { _id: '$_id.ns', total: { '$sum': '$count' }, max: { '$max': '$count' } } }, { '$project': { _id: 1, total: 1, max: 1, perc: { '$divide': [ '$max', '$total'] } } }, { '$match': { '$and': [{ total: { '$gt': 5 * 8 } }, { perc: { '$gt': 0.3 } }] } }]);
while(unbalancedCollections.hasNext()) { var nsInfo = unbalancedCollections.next(); var total = nsInfo['total']; var max = nsInfo['max']; var perc = nsInfo['perc']; var coll = nsInfo['_id']; print("Shard Distribution for " + coll + "\t | max: " + max + " | total:" + total + " | Perc: " + perc + "\n"); var shardCounts = db.chunks.aggregate([{ '$match': { ns: coll } }, { '$group': { _id: { ns: '$ns', shard: '$shard' }, count: { '$sum': 1 } } }, { '$sort': { '_id.shard': 1 } }]); while(shardCounts.hasNext()) { var shardCount = shardCounts.next(); print(shardCount['_id']['shard'] + ": " + shardCount['count']); } };
while(unbalancedCollections.hasNext()) { var coll = unbalancedCollections.next()['coll']; sh.enableBalancing(coll); }
db.changelog.aggregate([{ '$sort': { time: -1 } }, { '$match': { '$and': [{ 'details.note': 'aborted' }] }}, { '$group': { _id: { ns: '$ns', from: '$details.from', to: '$details.to', error: '$details.errmsg' }, time: { '$first': '$time' } } }, { '$sort': { '_id.ns': 1, time: -1 } }]).pretty()
db.changelog.find({ what: /moveChunk/ }, { time: 1, ns: 1, what: 1, 'details.from': 1, 'details.to': 1, 'details.note': 1, 'details.errmsg': 1, _id: 0 }).sort({ time: -1 }).limit(3).pretty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment