cluster_util.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "log"
  20. "net/http"
  21. "sort"
  22. "time"
  23. "github.com/coreos/etcd/pkg/types"
  24. )
  25. // isMemberBootstrapped tries to check if the given member has been bootstrapped
  26. // in the given cluster.
  27. func isMemberBootstrapped(cl *Cluster, member string, tr *http.Transport) bool {
  28. us := getOtherPeerURLs(cl, member)
  29. rcl, err := getClusterFromPeers(us, false, tr)
  30. if err != nil {
  31. return false
  32. }
  33. id := cl.MemberByName(member).ID
  34. m := rcl.Member(id)
  35. if m == nil {
  36. return false
  37. }
  38. if len(m.ClientURLs) > 0 {
  39. return true
  40. }
  41. return false
  42. }
  43. // GetClusterFromPeers takes a set of URLs representing etcd peers, and
  44. // attempts to construct a Cluster by accessing the members endpoint on one of
  45. // these URLs. The first URL to provide a response is used. If no URLs provide
  46. // a response, or a Cluster cannot be successfully created from a received
  47. // response, an error is returned.
  48. func GetClusterFromPeers(urls []string, tr *http.Transport) (*Cluster, error) {
  49. return getClusterFromPeers(urls, true, tr)
  50. }
  51. // If logerr is true, it prints out more error messages.
  52. func getClusterFromPeers(urls []string, logerr bool, tr *http.Transport) (*Cluster, error) {
  53. cc := &http.Client{
  54. Transport: tr,
  55. Timeout: time.Second,
  56. }
  57. for _, u := range urls {
  58. resp, err := cc.Get(u + "/members")
  59. if err != nil {
  60. if logerr {
  61. log.Printf("etcdserver: could not get cluster response from %s: %v", u, err)
  62. }
  63. continue
  64. }
  65. b, err := ioutil.ReadAll(resp.Body)
  66. if err != nil {
  67. if logerr {
  68. log.Printf("etcdserver: could not read the body of cluster response: %v", err)
  69. }
  70. continue
  71. }
  72. var membs []*Member
  73. if err := json.Unmarshal(b, &membs); err != nil {
  74. if logerr {
  75. log.Printf("etcdserver: could not unmarshal cluster response: %v", err)
  76. }
  77. continue
  78. }
  79. id, err := types.IDFromString(resp.Header.Get("X-Etcd-Cluster-ID"))
  80. if err != nil {
  81. if logerr {
  82. log.Printf("etcdserver: could not parse the cluster ID from cluster res: %v", err)
  83. }
  84. continue
  85. }
  86. return NewClusterFromMembers("", id, membs), nil
  87. }
  88. return nil, fmt.Errorf("etcdserver: could not retrieve cluster information from the given urls")
  89. }
  90. // getOtherPeerURLs returns peer urls of other members in the cluster. The
  91. // returned list is sorted in ascending lexicographical order.
  92. func getOtherPeerURLs(cl ClusterInfo, self string) []string {
  93. us := make([]string, 0)
  94. for _, m := range cl.Members() {
  95. if m.Name == self {
  96. continue
  97. }
  98. us = append(us, m.PeerURLs...)
  99. }
  100. sort.Strings(us)
  101. return us
  102. }