cluster_util.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // isBootstrapped tries to check if the given member has been bootstrapped
  26. // in the given cluster.
  27. func isBootstrapped(cfg *ServerConfig) bool {
  28. cl := cfg.Cluster
  29. member := cfg.Name
  30. us := getOtherPeerURLs(cl, member)
  31. rcl, err := getClusterFromPeers(us, false, cfg.Transport)
  32. if err != nil {
  33. return false
  34. }
  35. id := cl.MemberByName(member).ID
  36. m := rcl.Member(id)
  37. if m == nil {
  38. return false
  39. }
  40. if len(m.ClientURLs) > 0 {
  41. return true
  42. }
  43. return false
  44. }
  45. // GetClusterFromPeers takes a set of URLs representing etcd peers, and
  46. // attempts to construct a Cluster by accessing the members endpoint on one of
  47. // these URLs. The first URL to provide a response is used. If no URLs provide
  48. // a response, or a Cluster cannot be successfully created from a received
  49. // response, an error is returned.
  50. func GetClusterFromPeers(urls []string, tr *http.Transport) (*Cluster, error) {
  51. return getClusterFromPeers(urls, true, tr)
  52. }
  53. // If logerr is true, it prints out more error messages.
  54. func getClusterFromPeers(urls []string, logerr bool, tr *http.Transport) (*Cluster, error) {
  55. cc := &http.Client{
  56. Transport: tr,
  57. Timeout: time.Second,
  58. }
  59. for _, u := range urls {
  60. resp, err := cc.Get(u + "/members")
  61. if err != nil {
  62. if logerr {
  63. log.Printf("etcdserver: could not get cluster response from %s: %v", u, err)
  64. }
  65. continue
  66. }
  67. b, err := ioutil.ReadAll(resp.Body)
  68. if err != nil {
  69. if logerr {
  70. log.Printf("etcdserver: could not read the body of cluster response: %v", err)
  71. }
  72. continue
  73. }
  74. var membs []*Member
  75. if err := json.Unmarshal(b, &membs); err != nil {
  76. if logerr {
  77. log.Printf("etcdserver: could not unmarshal cluster response: %v", err)
  78. }
  79. continue
  80. }
  81. id, err := types.IDFromString(resp.Header.Get("X-Etcd-Cluster-ID"))
  82. if err != nil {
  83. if logerr {
  84. log.Printf("etcdserver: could not parse the cluster ID from cluster res: %v", err)
  85. }
  86. continue
  87. }
  88. return NewClusterFromMembers("", id, membs), nil
  89. }
  90. return nil, fmt.Errorf("etcdserver: could not retrieve cluster information from the given urls")
  91. }
  92. // getOtherPeerURLs returns peer urls of other members in the cluster. The
  93. // returned list is sorted in ascending lexicographical order.
  94. func getOtherPeerURLs(cl ClusterInfo, self string) []string {
  95. us := make([]string, 0)
  96. for _, m := range cl.Members() {
  97. if m.Name == self {
  98. continue
  99. }
  100. us = append(us, m.PeerURLs...)
  101. }
  102. sort.Strings(us)
  103. return us
  104. }