cluster_util.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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, rt http.RoundTripper, timeout time.Duration) bool {
  29. rcl, err := getClusterFromRemotePeers(getRemotePeerURLs(cl, member), timeout, false, rt)
  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. // Each request has a 10-second timeout. Because the upper limit of TTL is 5s,
  49. // 10 second is enough for building connection and finishing request.
  50. func GetClusterFromRemotePeers(urls []string, rt http.RoundTripper) (*cluster, error) {
  51. return getClusterFromRemotePeers(urls, 10*time.Second, true, rt)
  52. }
  53. // If logerr is true, it prints out more error messages.
  54. func getClusterFromRemotePeers(urls []string, timeout time.Duration, logerr bool, rt http.RoundTripper) (*cluster, error) {
  55. cc := &http.Client{
  56. Transport: rt,
  57. Timeout: timeout,
  58. }
  59. for _, u := range urls {
  60. resp, err := cc.Get(u + "/members")
  61. if err != nil {
  62. if logerr {
  63. plog.Warningf("could not get cluster response from %s: %v", u, err)
  64. }
  65. continue
  66. }
  67. b, err := ioutil.ReadAll(resp.Body)
  68. resp.Body.Close()
  69. if err != nil {
  70. if logerr {
  71. plog.Warningf("could not read the body of cluster response: %v", err)
  72. }
  73. continue
  74. }
  75. var membs []*Member
  76. if err = json.Unmarshal(b, &membs); err != nil {
  77. if logerr {
  78. plog.Warningf("could not unmarshal cluster response: %v", err)
  79. }
  80. continue
  81. }
  82. id, err := types.IDFromString(resp.Header.Get("X-Etcd-Cluster-ID"))
  83. if err != nil {
  84. if logerr {
  85. plog.Warningf("could not parse the cluster ID from cluster res: %v", err)
  86. }
  87. continue
  88. }
  89. return newClusterFromMembers("", id, membs), nil
  90. }
  91. return nil, fmt.Errorf("could not retrieve cluster information from the given urls")
  92. }
  93. // getRemotePeerURLs returns peer urls of remote members in the cluster. The
  94. // returned list is sorted in ascending lexicographical order.
  95. func getRemotePeerURLs(cl Cluster, local string) []string {
  96. us := make([]string, 0)
  97. for _, m := range cl.Members() {
  98. if m.Name == local {
  99. continue
  100. }
  101. us = append(us, m.PeerURLs...)
  102. }
  103. sort.Strings(us)
  104. return us
  105. }
  106. // getVersions returns the versions of the members in the given cluster.
  107. // The key of the returned map is the member's ID. The value of the returned map
  108. // is the semver versions string, including server and cluster.
  109. // If it fails to get the version of a member, the key will be nil.
  110. func getVersions(cl Cluster, local types.ID, rt http.RoundTripper) map[string]*version.Versions {
  111. members := cl.Members()
  112. vers := make(map[string]*version.Versions)
  113. for _, m := range members {
  114. if m.ID == local {
  115. cv := "not_decided"
  116. if cl.Version() != nil {
  117. cv = cl.Version().String()
  118. }
  119. vers[m.ID.String()] = &version.Versions{Server: version.Version, Cluster: cv}
  120. continue
  121. }
  122. ver, err := getVersion(m, rt)
  123. if err != nil {
  124. plog.Warningf("cannot get the version of member %s (%v)", m.ID, err)
  125. vers[m.ID.String()] = nil
  126. } else {
  127. vers[m.ID.String()] = ver
  128. }
  129. }
  130. return vers
  131. }
  132. // decideClusterVersion decides the cluster version based on the versions map.
  133. // The returned version is the min server version in the map, or nil if the min
  134. // version in unknown.
  135. func decideClusterVersion(vers map[string]*version.Versions) *semver.Version {
  136. var cv *semver.Version
  137. lv := semver.Must(semver.NewVersion(version.Version))
  138. for mid, ver := range vers {
  139. if ver == nil {
  140. return nil
  141. }
  142. v, err := semver.NewVersion(ver.Server)
  143. if err != nil {
  144. plog.Errorf("cannot understand the version of member %s (%v)", mid, err)
  145. return nil
  146. }
  147. if lv.LessThan(*v) {
  148. plog.Warningf("the local etcd version %s is not up-to-date", lv.String())
  149. plog.Warningf("member %s has a higher version %s", mid, ver.Server)
  150. }
  151. if cv == nil {
  152. cv = v
  153. } else if v.LessThan(*cv) {
  154. cv = v
  155. }
  156. }
  157. return cv
  158. }
  159. // isCompatibleWithCluster return true if the local member has a compatible version with
  160. // the current running cluster.
  161. // The version is considered as compatible when at least one of the other members in the cluster has a
  162. // cluster version in the range of [MinClusterVersion, Version] and no known members has a cluster version
  163. // out of the range.
  164. // We set this rule since when the local member joins, another member might be offline.
  165. func isCompatibleWithCluster(cl Cluster, local types.ID, rt http.RoundTripper) bool {
  166. vers := getVersions(cl, local, rt)
  167. minV := semver.Must(semver.NewVersion(version.MinClusterVersion))
  168. maxV := semver.Must(semver.NewVersion(version.Version))
  169. maxV = &semver.Version{
  170. Major: maxV.Major,
  171. Minor: maxV.Minor,
  172. }
  173. return isCompatibleWithVers(vers, local, minV, maxV)
  174. }
  175. func isCompatibleWithVers(vers map[string]*version.Versions, local types.ID, minV, maxV *semver.Version) bool {
  176. var ok bool
  177. for id, v := range vers {
  178. // ignore comparison with local version
  179. if id == local.String() {
  180. continue
  181. }
  182. if v == nil {
  183. continue
  184. }
  185. clusterv, err := semver.NewVersion(v.Cluster)
  186. if err != nil {
  187. plog.Errorf("cannot understand the cluster version of member %s (%v)", id, err)
  188. continue
  189. }
  190. if clusterv.LessThan(*minV) {
  191. plog.Warningf("the running cluster version(%v) is lower than the minimal cluster version(%v) supported", clusterv.String(), minV.String())
  192. return false
  193. }
  194. if maxV.LessThan(*clusterv) {
  195. plog.Warningf("the running cluster version(%v) is higher than the maximum cluster version(%v) supported", clusterv.String(), maxV.String())
  196. return false
  197. }
  198. ok = true
  199. }
  200. return ok
  201. }
  202. // getVersion returns the Versions of the given member via its
  203. // peerURLs. Returns the last error if it fails to get the version.
  204. func getVersion(m *Member, rt http.RoundTripper) (*version.Versions, error) {
  205. cc := &http.Client{
  206. Transport: rt,
  207. }
  208. var (
  209. err error
  210. resp *http.Response
  211. )
  212. for _, u := range m.PeerURLs {
  213. resp, err = cc.Get(u + "/version")
  214. if err != nil {
  215. plog.Warningf("failed to reach the peerURL(%s) of member %s (%v)", u, m.ID, err)
  216. continue
  217. }
  218. // etcd 2.0 does not have version endpoint on peer url.
  219. if resp.StatusCode == http.StatusNotFound {
  220. resp.Body.Close()
  221. return &version.Versions{
  222. Server: "2.0.0",
  223. Cluster: "2.0.0",
  224. }, nil
  225. }
  226. var b []byte
  227. b, err = ioutil.ReadAll(resp.Body)
  228. resp.Body.Close()
  229. if err != nil {
  230. plog.Warningf("failed to read out the response body from the peerURL(%s) of member %s (%v)", u, m.ID, err)
  231. continue
  232. }
  233. var vers version.Versions
  234. if err = json.Unmarshal(b, &vers); err != nil {
  235. plog.Warningf("failed to unmarshal the response body got from the peerURL(%s) of member %s (%v)", u, m.ID, err)
  236. continue
  237. }
  238. return &vers, nil
  239. }
  240. return nil, err
  241. }
  242. func MustDetectDowngrade(cv *semver.Version) {
  243. lv := semver.Must(semver.NewVersion(version.Version))
  244. // only keep major.minor version for comparison against cluster version
  245. lv = &semver.Version{Major: lv.Major, Minor: lv.Minor}
  246. if cv != nil && lv.LessThan(*cv) {
  247. plog.Fatalf("cluster cannot be downgraded (current version: %s is lower than determined cluster version: %s).", version.Version, version.Cluster(cv.String()))
  248. }
  249. }