cluster_util.go 3.1 KB

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