cluster_util.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2015 The etcd Authors
  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/etcdserver/membership"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/version"
  25. "github.com/coreos/go-semver/semver"
  26. )
  27. // isMemberBootstrapped tries to check if the given member has been bootstrapped
  28. // in the given cluster.
  29. func isMemberBootstrapped(cl *membership.RaftCluster, member string, rt http.RoundTripper, timeout time.Duration) bool {
  30. rcl, err := getClusterFromRemotePeers(getRemotePeerURLs(cl, member), timeout, false, rt)
  31. if err != nil {
  32. return false
  33. }
  34. id := cl.MemberByName(member).ID
  35. m := rcl.Member(id)
  36. if m == nil {
  37. return false
  38. }
  39. if len(m.ClientURLs) > 0 {
  40. return true
  41. }
  42. return false
  43. }
  44. // GetClusterFromRemotePeers takes a set of URLs representing etcd peers, and
  45. // attempts to construct a Cluster by accessing the members endpoint on one of
  46. // these URLs. The first URL to provide a response is used. If no URLs provide
  47. // a response, or a Cluster cannot be successfully created from a received
  48. // response, an error is returned.
  49. // Each request has a 10-second timeout. Because the upper limit of TTL is 5s,
  50. // 10 second is enough for building connection and finishing request.
  51. func GetClusterFromRemotePeers(urls []string, rt http.RoundTripper) (*membership.RaftCluster, error) {
  52. return getClusterFromRemotePeers(urls, 10*time.Second, true, rt)
  53. }
  54. // If logerr is true, it prints out more error messages.
  55. func getClusterFromRemotePeers(urls []string, timeout time.Duration, logerr bool, rt http.RoundTripper) (*membership.RaftCluster, error) {
  56. cc := &http.Client{
  57. Transport: rt,
  58. Timeout: timeout,
  59. }
  60. for _, u := range urls {
  61. resp, err := cc.Get(u + "/members")
  62. if err != nil {
  63. if logerr {
  64. plog.Warningf("could not get cluster response from %s: %v", u, err)
  65. }
  66. continue
  67. }
  68. b, err := ioutil.ReadAll(resp.Body)
  69. resp.Body.Close()
  70. if err != nil {
  71. if logerr {
  72. plog.Warningf("could not read the body of cluster response: %v", err)
  73. }
  74. continue
  75. }
  76. var membs []*membership.Member
  77. if err = json.Unmarshal(b, &membs); err != nil {
  78. if logerr {
  79. plog.Warningf("could not unmarshal cluster response: %v", err)
  80. }
  81. continue
  82. }
  83. id, err := types.IDFromString(resp.Header.Get("X-Etcd-Cluster-ID"))
  84. if err != nil {
  85. if logerr {
  86. plog.Warningf("could not parse the cluster ID from cluster res: %v", err)
  87. }
  88. continue
  89. }
  90. // check the length of membership members
  91. // if the membership members are present then prepare and return raft cluster
  92. // if membership members are not present then the raft cluster formed will be
  93. // an invalid empty cluster hence return failed to get raft cluster member(s) from the given urls error
  94. if len(membs) > 0 {
  95. return membership.NewClusterFromMembers("", id, membs), nil
  96. }
  97. return nil, fmt.Errorf("failed to get raft cluster member(s) from the given urls.")
  98. }
  99. return nil, fmt.Errorf("could not retrieve cluster information from the given urls")
  100. }
  101. // getRemotePeerURLs returns peer urls of remote members in the cluster. The
  102. // returned list is sorted in ascending lexicographical order.
  103. func getRemotePeerURLs(cl *membership.RaftCluster, local string) []string {
  104. us := make([]string, 0)
  105. for _, m := range cl.Members() {
  106. if m.Name == local {
  107. continue
  108. }
  109. us = append(us, m.PeerURLs...)
  110. }
  111. sort.Strings(us)
  112. return us
  113. }
  114. // getVersions returns the versions of the members in the given cluster.
  115. // The key of the returned map is the member's ID. The value of the returned map
  116. // is the semver versions string, including server and cluster.
  117. // If it fails to get the version of a member, the key will be nil.
  118. func getVersions(cl *membership.RaftCluster, local types.ID, rt http.RoundTripper) map[string]*version.Versions {
  119. members := cl.Members()
  120. vers := make(map[string]*version.Versions)
  121. for _, m := range members {
  122. if m.ID == local {
  123. cv := "not_decided"
  124. if cl.Version() != nil {
  125. cv = cl.Version().String()
  126. }
  127. vers[m.ID.String()] = &version.Versions{Server: version.Version, Cluster: cv}
  128. continue
  129. }
  130. ver, err := getVersion(m, rt)
  131. if err != nil {
  132. plog.Warningf("cannot get the version of member %s (%v)", m.ID, err)
  133. vers[m.ID.String()] = nil
  134. } else {
  135. vers[m.ID.String()] = ver
  136. }
  137. }
  138. return vers
  139. }
  140. // decideClusterVersion decides the cluster version based on the versions map.
  141. // The returned version is the min server version in the map, or nil if the min
  142. // version in unknown.
  143. func decideClusterVersion(vers map[string]*version.Versions) *semver.Version {
  144. var cv *semver.Version
  145. lv := semver.Must(semver.NewVersion(version.Version))
  146. for mid, ver := range vers {
  147. if ver == nil {
  148. return nil
  149. }
  150. v, err := semver.NewVersion(ver.Server)
  151. if err != nil {
  152. plog.Errorf("cannot understand the version of member %s (%v)", mid, err)
  153. return nil
  154. }
  155. if lv.LessThan(*v) {
  156. plog.Warningf("the local etcd version %s is not up-to-date", lv.String())
  157. plog.Warningf("member %s has a higher version %s", mid, ver.Server)
  158. }
  159. if cv == nil {
  160. cv = v
  161. } else if v.LessThan(*cv) {
  162. cv = v
  163. }
  164. }
  165. return cv
  166. }
  167. // isCompatibleWithCluster return true if the local member has a compatible version with
  168. // the current running cluster.
  169. // The version is considered as compatible when at least one of the other members in the cluster has a
  170. // cluster version in the range of [MinClusterVersion, Version] and no known members has a cluster version
  171. // out of the range.
  172. // We set this rule since when the local member joins, another member might be offline.
  173. func isCompatibleWithCluster(cl *membership.RaftCluster, local types.ID, rt http.RoundTripper) bool {
  174. vers := getVersions(cl, local, rt)
  175. minV := semver.Must(semver.NewVersion(version.MinClusterVersion))
  176. maxV := semver.Must(semver.NewVersion(version.Version))
  177. maxV = &semver.Version{
  178. Major: maxV.Major,
  179. Minor: maxV.Minor,
  180. }
  181. return isCompatibleWithVers(vers, local, minV, maxV)
  182. }
  183. func isCompatibleWithVers(vers map[string]*version.Versions, local types.ID, minV, maxV *semver.Version) bool {
  184. var ok bool
  185. for id, v := range vers {
  186. // ignore comparison with local version
  187. if id == local.String() {
  188. continue
  189. }
  190. if v == nil {
  191. continue
  192. }
  193. clusterv, err := semver.NewVersion(v.Cluster)
  194. if err != nil {
  195. plog.Errorf("cannot understand the cluster version of member %s (%v)", id, err)
  196. continue
  197. }
  198. if clusterv.LessThan(*minV) {
  199. plog.Warningf("the running cluster version(%v) is lower than the minimal cluster version(%v) supported", clusterv.String(), minV.String())
  200. return false
  201. }
  202. if maxV.LessThan(*clusterv) {
  203. plog.Warningf("the running cluster version(%v) is higher than the maximum cluster version(%v) supported", clusterv.String(), maxV.String())
  204. return false
  205. }
  206. ok = true
  207. }
  208. return ok
  209. }
  210. // getVersion returns the Versions of the given member via its
  211. // peerURLs. Returns the last error if it fails to get the version.
  212. func getVersion(m *membership.Member, rt http.RoundTripper) (*version.Versions, error) {
  213. cc := &http.Client{
  214. Transport: rt,
  215. }
  216. var (
  217. err error
  218. resp *http.Response
  219. )
  220. for _, u := range m.PeerURLs {
  221. resp, err = cc.Get(u + "/version")
  222. if err != nil {
  223. plog.Warningf("failed to reach the peerURL(%s) of member %s (%v)", u, m.ID, err)
  224. continue
  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. }