cluster_util.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. "net/http"
  20. "sort"
  21. "time"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/version"
  25. )
  26. // isMemberBootstrapped tries to check if the given member has been bootstrapped
  27. // in the given cluster.
  28. func isMemberBootstrapped(cl *cluster, member string, tr *http.Transport) bool {
  29. rcl, err := getClusterFromRemotePeers(getRemotePeerURLs(cl, member), 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. // GetClusterFromRemotePeers 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 GetClusterFromRemotePeers(urls []string, tr *http.Transport) (*cluster, error) {
  49. return getClusterFromRemotePeers(urls, true, tr)
  50. }
  51. // If logerr is true, it prints out more error messages.
  52. func getClusterFromRemotePeers(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. plog.Warningf("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. plog.Warningf("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. plog.Warningf("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. plog.Warningf("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("could not retrieve cluster information from the given urls")
  89. }
  90. // getRemotePeerURLs returns peer urls of remote members in the cluster. The
  91. // returned list is sorted in ascending lexicographical order.
  92. func getRemotePeerURLs(cl Cluster, local string) []string {
  93. us := make([]string, 0)
  94. for _, m := range cl.Members() {
  95. if m.Name == local {
  96. continue
  97. }
  98. us = append(us, m.PeerURLs...)
  99. }
  100. sort.Strings(us)
  101. return us
  102. }
  103. // getVersions returns the versions of the members in the given cluster.
  104. // The key of the returned map is the member's ID. The value of the returned map
  105. // is the semver versions string, including server and cluster.
  106. // If it fails to get the version of a member, the key will be nil.
  107. func getVersions(cl Cluster, local types.ID, tr *http.Transport) map[string]*version.Versions {
  108. members := cl.Members()
  109. vers := make(map[string]*version.Versions)
  110. for _, m := range members {
  111. if m.ID == local {
  112. cv := "not_decided"
  113. if cl.Version() != nil {
  114. cv = cl.Version().String()
  115. }
  116. vers[m.ID.String()] = &version.Versions{Server: version.Version, Cluster: cv}
  117. continue
  118. }
  119. ver, err := getVersion(m, tr)
  120. if err != nil {
  121. plog.Warningf("cannot get the version of member %s (%v)", m.ID, err)
  122. vers[m.ID.String()] = nil
  123. } else {
  124. vers[m.ID.String()] = ver
  125. }
  126. }
  127. return vers
  128. }
  129. // decideClusterVersion decides the cluster version based on the versions map.
  130. // The returned version is the min server version in the map, or nil if the min
  131. // version in unknown.
  132. func decideClusterVersion(vers map[string]*version.Versions) *semver.Version {
  133. var cv *semver.Version
  134. lv := semver.Must(semver.NewVersion(version.Version))
  135. for mid, ver := range vers {
  136. if ver == nil {
  137. return nil
  138. }
  139. v, err := semver.NewVersion(ver.Server)
  140. if err != nil {
  141. plog.Errorf("cannot understand the version of member %s (%v)", mid, err)
  142. return nil
  143. }
  144. if lv.LessThan(*v) {
  145. plog.Warningf("the etcd version %s is not up-to-date", lv.String())
  146. plog.Warningf("member %s has a higher version %s", mid, ver)
  147. }
  148. if cv == nil {
  149. cv = v
  150. } else if v.LessThan(*cv) {
  151. cv = v
  152. }
  153. }
  154. return cv
  155. }
  156. // isCompatibleWithCluster return true if the local member has a compitable version with
  157. // the current running cluster.
  158. // The version is considered as compitable when at least one of the other members in the cluster has a
  159. // cluster version in the range of [MinClusterVersion, Version] and no known members has a cluster version
  160. // out of the range.
  161. // We set this rule since when the local member joins, another member might be offline.
  162. func isCompatibleWithCluster(cl Cluster, local types.ID, tr *http.Transport) bool {
  163. vers := getVersions(cl, local, tr)
  164. minV := semver.Must(semver.NewVersion(version.MinClusterVersion))
  165. maxV := semver.Must(semver.NewVersion(version.Version))
  166. maxV = &semver.Version{
  167. Major: maxV.Major,
  168. Minor: maxV.Minor,
  169. }
  170. return isCompatibleWithVers(vers, local, minV, maxV)
  171. }
  172. func isCompatibleWithVers(vers map[string]*version.Versions, local types.ID, minV, maxV *semver.Version) bool {
  173. var ok bool
  174. for id, v := range vers {
  175. // ignore comparasion with local version
  176. if id == local.String() {
  177. continue
  178. }
  179. if v == nil {
  180. continue
  181. }
  182. clusterv, err := semver.NewVersion(v.Cluster)
  183. if err != nil {
  184. plog.Errorf("cannot understand the cluster version of member %s (%v)", id, err)
  185. continue
  186. }
  187. if clusterv.LessThan(*minV) {
  188. plog.Warningf("the running cluster version(%v) is lower than the minimal cluster version(%v) supported", clusterv.String(), minV.String())
  189. return false
  190. }
  191. if maxV.LessThan(*clusterv) {
  192. plog.Warningf("the running cluster version(%v) is higher than the maximum cluster version(%v) supported", clusterv.String(), maxV.String())
  193. return false
  194. }
  195. ok = true
  196. }
  197. return ok
  198. }
  199. // getVersion returns the Versions of the given member via its
  200. // peerURLs. Returns the last error if it fails to get the version.
  201. func getVersion(m *Member, tr *http.Transport) (*version.Versions, error) {
  202. cc := &http.Client{
  203. Transport: tr,
  204. Timeout: time.Second,
  205. }
  206. var (
  207. err error
  208. resp *http.Response
  209. )
  210. for _, u := range m.PeerURLs {
  211. resp, err = cc.Get(u + "/version")
  212. if err != nil {
  213. plog.Warningf("failed to reach the peerURL(%s) of member %s (%v)", u, m.ID, err)
  214. continue
  215. }
  216. // etcd 2.0 does not have version endpoint on peer url.
  217. if resp.StatusCode == http.StatusNotFound {
  218. resp.Body.Close()
  219. return &version.Versions{
  220. Server: "2.0.0",
  221. Cluster: "2.0.0",
  222. }, nil
  223. }
  224. b, err := ioutil.ReadAll(resp.Body)
  225. resp.Body.Close()
  226. if err != nil {
  227. plog.Warningf("failed to read out the response body from the peerURL(%s) of member %s (%v)", u, m.ID, err)
  228. continue
  229. }
  230. var vers version.Versions
  231. if err := json.Unmarshal(b, &vers); err != nil {
  232. plog.Warningf("failed to unmarshal the response body got from the peerURL(%s) of member %s (%v)", u, m.ID, err)
  233. continue
  234. }
  235. return &vers, nil
  236. }
  237. return nil, err
  238. }