-
Notifications
You must be signed in to change notification settings - Fork 3
/
member.go
368 lines (319 loc) · 8.01 KB
/
member.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2016 The etcd-operator Authors
// Copyright 2018 The rss-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"errors"
"fmt"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
)
var (
logger = GetLogger("member")
)
type Member struct {
Name string
// Kubernetes namespace this member runs in.
Namespace string
SEQ uint64
Failures uint64
Online bool
AppPrimary bool
AppRunning bool
AppFailed bool
SecurePeer bool
SecureClient bool
}
func (m *Member) Addr() string {
return fmt.Sprintf("%s.%s.%s.svc", m.Name, clusterNameFromMemberName(m.Name), m.Namespace)
}
// ClientURL is the client URL for this member
func (m *Member) ClientURL() string {
return fmt.Sprintf("%s://%s:2379", m.clientScheme(), m.Addr())
}
func (m *Member) clientScheme() string {
if m.SecureClient {
return "https"
}
return "http"
}
func (m *Member) peerScheme() string {
if m.SecurePeer {
return "https"
}
return "http"
}
func (m *Member) String() string {
if !m.Online {
return fmt.Sprintf("%v~", m.Name)
} else if m.AppFailed {
return fmt.Sprintf("%v!", m.Name)
} else if m.AppRunning {
return fmt.Sprintf("%v:%v", m.Name, m.SEQ)
} else if m.SEQ > 0 {
return fmt.Sprintf("%v?%v", m.Name, m.SEQ)
}
return m.Name
}
func (m *Member) ListenClientURL() string {
return fmt.Sprintf("%s://0.0.0.0:2379", m.clientScheme())
}
func (m *Member) ListenPeerURL() string {
return fmt.Sprintf("%s://0.0.0.0:2380", m.peerScheme())
}
func (m *Member) PeerURL() string {
return fmt.Sprintf("%s://%s:2380", m.peerScheme(), m.Addr())
}
type MemberSet map[string]*Member
func NewMemberSet(ms ...*Member) MemberSet {
res := MemberSet{}
for _, m := range ms {
res[m.Name] = m
}
return res
}
// the set of all members of s1 that are not members of s2
func (ms MemberSet) Diff(other MemberSet) MemberSet {
diff := MemberSet{}
for n, m := range ms {
if _, ok := other[n]; !ok {
diff[n] = m
}
}
return diff
}
func (ms MemberSet) DiffExtended(other MemberSet) MemberSet {
diff := MemberSet{}
for n, m := range ms {
if _, ok := other[n]; !ok {
diff[n] = m
} else if other[n].Online != m.Online {
diff[n] = m
}
}
return diff
}
func (ms MemberSet) Copy() MemberSet {
c := MemberSet{}
for n, m := range ms {
c[n] = m.Copy()
}
return c
}
func (m *Member) Restore(last *Member) {
// Restore app state
m.AppPrimary = last.AppPrimary
m.AppRunning = last.AppRunning
m.AppFailed = last.AppFailed
m.Failures = last.Failures
m.SEQ = last.SEQ
}
func (m *Member) Copy() *Member {
c := Member{}
c.Name = m.Name
c.Online = m.Online
c.AppPrimary = m.AppPrimary
c.AppRunning = m.AppRunning
c.AppFailed = m.AppFailed
c.Failures = m.Failures
c.SEQ = m.SEQ
return &c
}
func (m Member) IsEqual(other Member) bool {
if m.Online != other.Online {
// logger.Warnf("Pod %v Online %v/%v", m.Name, m.Online, other.Online)
return false
} else if m.SEQ != other.SEQ {
// logger.Warnf("Pod %v SEQ %v/%v", m.Name, m.SEQ, other.SEQ)
return false
} else if m.AppRunning != other.AppRunning {
// logger.Warnf("Pod %v running %v/%v", m.Name, m.AppRunning, other.AppRunning)
return false
} else if m.AppPrimary != other.AppPrimary {
// logger.Warnf("Pod %v primary %v/%v", m.Name, m.AppPrimary, other.AppPrimary)
return false
} else if m.AppFailed != other.AppFailed {
// logger.Warnf("Pod %v failed %v/%v", m.Name, m.AppFailed, other.AppFailed)
return false
}
return true
}
func (m *Member) Offline() {
if m.Online {
logger.Warnf("Pod %v offline", m.Name)
}
m.Online = false
m.AppPrimary = false
m.AppRunning = false
m.AppFailed = false
m.SEQ = 0
}
func (ms MemberSet) Reconcile(running MemberSet, max int32) (MemberSet, error) {
// The only thing we take from 'running' is new members and the value of .Online
result := ms.Copy()
for n, appeared := range running {
if _, ok := result[n]; !ok {
result[n] = appeared.Copy()
}
}
lostMembers := result.Diff(running)
newMembers := running.DiffExtended(ms)
if lostMembers.Size() > 0 || newMembers.Size() > 0 {
logger.Infof("Updating membership: new=%s, lost=%s", newMembers, lostMembers)
}
for _, m := range newMembers {
logger.Infof("Pod %s available", m.Name)
result[m.Name].Online = true
}
for _, m := range lostMembers {
// If we're scaling down, there is no need to restore lost members
if _, ok := result[m.Name]; !ok && running.Size() <= max {
result[m.Name] = m.Copy()
result[m.Name].Offline()
} else {
result[m.Name].Offline()
}
}
// if running.AppPrimaries() < ms.AppPrimaries() && running.AppPrimaries() < max/2+1 {
// logger.Warnf("Quorum lost")
// // return running, fmt.Errorf("Quorum lost")
// }
return result, nil
}
// IsEqual tells whether two member sets are equal by checking
// - they have the same set of members and member equality are judged by Name only.
func (ms MemberSet) IsEqual(other MemberSet) bool {
if ms.Size() != other.Size() {
return false
}
for n := range ms {
if _, ok := other[n]; !ok {
return false
} else if !ms[n].IsEqual(*other[n]) {
return false
}
}
return true
}
func (ms MemberSet) Size() int32 {
return int32(len(ms))
}
func (ms MemberSet) AppPrimaries() int32 {
count := int32(0)
for _, m := range ms {
if m.Online && m.AppPrimary && m.AppRunning {
count += 1
}
}
return count
}
func (ms MemberSet) AppMembers() int32 {
count := int32(0)
for _, m := range ms {
if m.Online && m.AppRunning {
count += 1
}
}
return count
}
func (ms MemberSet) ActiveMembers() int32 {
count := int32(0)
for _, m := range ms {
if m.Online {
count += 1
}
}
return count
}
func (ms MemberSet) InActiveMembers() int32 {
count := int32(0)
for _, m := range ms {
if !m.Online {
count += 1
}
}
return count
}
func (ms MemberSet) String() string {
var mstring []string
for _, m := range ms {
mstring = append(mstring, m.String())
}
sort.Strings(mstring)
return fmt.Sprintf("[%v]", strings.Join(mstring, ", "))
}
func (ms MemberSet) PickOne() *Member {
for _, m := range ms {
return m
}
panic("empty")
}
func (ms MemberSet) PeerURLPairs() []string {
ps := make([]string, 0)
for _, m := range ms {
ps = append(ps, fmt.Sprintf("%s=%s", m.Name, m.PeerURL()))
}
return ps
}
func (ms MemberSet) Add(m *Member) {
ms[m.Name] = m
}
func (ms MemberSet) Remove(name string) {
delete(ms, name)
}
func (ms MemberSet) ClientURLs() []string {
endpoints := make([]string, 0, len(ms))
for _, m := range ms {
endpoints = append(endpoints, m.ClientURL())
}
return endpoints
}
func GetCounterFromMemberName(name string) (int, error) {
i := strings.LastIndex(name, "-")
if i == -1 || i+1 >= len(name) {
return 0, fmt.Errorf("name (%s) does not contain '-' or anything after '-'", name)
}
c, err := strconv.Atoi(name[i+1:])
if err != nil {
return 0, fmt.Errorf("could not atoi %s: %v", name[i+1:], err)
}
return c, nil
}
var validPeerURL = regexp.MustCompile(`^\w+:\/\/[\w\.\-]+(:\d+)?$`)
func MemberNameFromPeerURL(pu string) (string, error) {
// url.Parse has very loose validation. We do our own validation.
if !validPeerURL.MatchString(pu) {
return "", errors.New("invalid PeerURL format")
}
u, err := url.Parse(pu)
if err != nil {
return "", err
}
path := strings.Split(u.Host, ":")[0]
name := strings.Split(path, ".")[0]
return name, err
}
func CreateMemberName(clusterName string, member int) string {
return fmt.Sprintf("%s-%04d", clusterName, member)
}
func clusterNameFromMemberName(mn string) string {
i := strings.LastIndex(mn, "-")
if i == -1 {
panic(fmt.Sprintf("unexpected member name: %s", mn))
}
return mn[:i]
}