Created
January 30, 2014 19:39
-
-
Save normgraham/8717122 to your computer and use it in GitHub Desktop.
Identify missing and non-matching indexes in a replica set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var diffIndexes = function() { | |
| var replsetInfo = new Array(); | |
| var maxIterations = 0; | |
| rs.slaveOk(); | |
| rs.status().members.forEach(function(member) { | |
| if (member.stateStr == "PRIMARY" || member.stateStr == "SECONDARY") { | |
| var memberInfo = { | |
| name : member.name, | |
| cursor : 0, | |
| nextCursor : 0, | |
| indexes : new Array() | |
| } | |
| var connection = new Mongo(member.name); | |
| connection.setSlaveOk(); | |
| // get the indexes from all the databases | |
| connection.getDB("test").adminCommand({"listDatabases":1}).databases.forEach(function(db) { | |
| connection.getDB(db.name).getCollection("system.indexes").find().forEach(function(doc) { | |
| delete doc.v; | |
| memberInfo.indexes.push(doc); | |
| }); | |
| }); | |
| // sort the indexes by namespace and key so we can compare them later | |
| memberInfo.indexes = memberInfo.indexes.sort(function(a,b) { | |
| if (a.ns < b.ns) return -1; | |
| if (a.ns > b.ns) return 1; | |
| if (JSON.stringify(a.key) < JSON.stringify(b.key)) return -1; | |
| if (JSON.stringify(a.key) > JSON.stringify(b.key)) return 1; | |
| return 0; | |
| }); | |
| if (maxIterations < memberInfo.indexes.length) | |
| maxIterations = memberInfo.indexes.length; | |
| replsetInfo.push(memberInfo); | |
| } | |
| }); | |
| for (var i = 0; i < maxIterations; i++) { | |
| // advance the cursors | |
| replsetInfo.forEach(function (memberInfo) { | |
| memberInfo.cursor = memberInfo.nextCursor; | |
| }); | |
| // look for index variances at these cursors in the index arrays | |
| var indexesMatch = true; | |
| var a = replsetInfo[0].indexes[replsetInfo[0].cursor]; | |
| for (var j = 1; j < replsetInfo.length && indexesMatch; j++) { | |
| var b = replsetInfo[j].indexes[replsetInfo[j].cursor]; | |
| if (!(JSON.stringify(a) == JSON.stringify(b))) | |
| indexesMatch = false; | |
| } | |
| // update nextCursors for members that aren't missing an index | |
| replsetInfo.forEach(function (theMember) { | |
| var indexIsMissing = false; | |
| var theIndex = theMember.indexes[theMember.cursor]; | |
| replsetInfo.forEach(function (otherMember) { | |
| if (theMember.name != otherMember.name) { | |
| var otherIndex = otherMember.indexes[otherMember.cursor]; | |
| if (theIndex.ns > otherIndex.ns || | |
| (theIndex.ns == otherIndex.ns && JSON.stringify(theIndex.key) > JSON.stringify(otherIndex.key))) { | |
| indexIsMissing = true; | |
| } | |
| } | |
| }); | |
| if (!indexIsMissing && theMember.cursor + 1 < theMember.indexes.length) | |
| theMember.nextCursor = theMember.cursor + 1; | |
| }); | |
| // print the differences | |
| if (!indexesMatch) { | |
| print("--------------------------------------------------"); | |
| replsetInfo.forEach(function (memberInfo) { | |
| print(memberInfo.name); | |
| if (memberInfo.nextCursor == memberInfo.cursor) | |
| print(" ==== Index is Missing ===="); | |
| else | |
| printjson(memberInfo.indexes[memberInfo.cursor]); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment