machines.go 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. // machineNum returns the number of machines in the cluster
  3. func machineNum() int {
  4. response, _ := etcdStore.RawGet("_etcd/machines")
  5. return len(response)
  6. }
  7. // getMachines gets the current machines in the cluster
  8. func getMachines() []string {
  9. peers := raftServer.Peers()
  10. machines := make([]string, len(peers)+1)
  11. leader, _ := nameToEtcdURL(raftServer.Leader())
  12. i := 0
  13. if leader != "" {
  14. // Add leader at the first of the machines list
  15. // Add server itself to the machine list
  16. // Since peer map does not contain the server itself
  17. if leader == info.EtcdURL {
  18. machines[i] = info.EtcdURL
  19. i++
  20. } else {
  21. machines[i] = leader
  22. i++
  23. machines[i] = info.EtcdURL
  24. i++
  25. }
  26. }
  27. // Add all peers to the slice
  28. for peerName, _ := range peers {
  29. if machine, ok := nameToEtcdURL(peerName); ok {
  30. // do not add leader twice
  31. if machine != leader {
  32. machines[i] = machine
  33. i++
  34. }
  35. }
  36. }
  37. return machines
  38. }