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