wait.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // We know the master of the first set (pri=1), but not of the second.
  2. var settings = {}
  3. var rs1cfg = {_id: "rs1",
  4. members: [{_id: 1, host: "127.0.0.1:40011", priority: 1},
  5. {_id: 2, host: "127.0.0.1:40012", priority: 0},
  6. {_id: 3, host: "127.0.0.1:40013", priority: 0}]}
  7. var rs2cfg = {_id: "rs2",
  8. members: [{_id: 1, host: "127.0.0.1:40021", priority: 1},
  9. {_id: 2, host: "127.0.0.1:40022", priority: 1},
  10. {_id: 3, host: "127.0.0.1:40023", priority: 0}]}
  11. var rs3cfg = {_id: "rs3",
  12. members: [{_id: 1, host: "127.0.0.1:40031", priority: 1},
  13. {_id: 2, host: "127.0.0.1:40032", priority: 1},
  14. {_id: 3, host: "127.0.0.1:40033", priority: 1}],
  15. settings: settings}
  16. for (var i = 0; i != 60; i++) {
  17. try {
  18. rs1a = new Mongo("127.0.0.1:40011").getDB("admin")
  19. rs2a = new Mongo("127.0.0.1:40021").getDB("admin")
  20. rs3a = new Mongo("127.0.0.1:40031").getDB("admin")
  21. rs3a.auth("root", "rapadura")
  22. db1 = new Mongo("127.0.0.1:40001").getDB("admin")
  23. db2 = new Mongo("127.0.0.1:40002").getDB("admin")
  24. break
  25. } catch(err) {
  26. print("Can't connect yet...")
  27. }
  28. sleep(1000)
  29. }
  30. function countHealthy(rs) {
  31. var status = rs.runCommand({replSetGetStatus: 1})
  32. var count = 0
  33. var primary = 0
  34. if (typeof status.members != "undefined") {
  35. for (var i = 0; i != status.members.length; i++) {
  36. var m = status.members[i]
  37. if (m.health == 1 && (m.state == 1 || m.state == 2)) {
  38. count += 1
  39. if (m.state == 1) {
  40. primary = 1
  41. }
  42. }
  43. }
  44. }
  45. if (primary == 0) {
  46. count = 0
  47. }
  48. return count
  49. }
  50. var totalRSMembers = rs1cfg.members.length + rs2cfg.members.length + rs3cfg.members.length
  51. for (var i = 0; i != 90; i++) {
  52. var count = countHealthy(rs1a) + countHealthy(rs2a) + countHealthy(rs3a)
  53. print("Replica sets have", count, "healthy nodes.")
  54. if (count == totalRSMembers) {
  55. quit(0)
  56. }
  57. sleep(1000)
  58. }
  59. print("Replica sets didn't sync up properly.")
  60. quit(12)
  61. // vim:ts=4:sw=4:et