-
Notifications
You must be signed in to change notification settings - Fork 12
/
member_map.go
349 lines (281 loc) · 8.17 KB
/
member_map.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
/*
* Copyright 2018 De-labtory
*
* 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
*
* https://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 swim
import (
"errors"
"math/rand"
"net"
"strconv"
"sync"
"time"
"github.com/DE-labtory/iLogger"
"github.com/DE-labtory/swim/pb"
)
var ErrEmptyMemberID = errors.New("MemberID is empty")
var ErrCreatingSuspicion = errors.New("error occurred while creating suspicion")
var ErrMemberUnknownState = errors.New("error member is in unknown state")
// Status of members
type Status int
const (
// Unknown status of a member
Unknown Status = iota
// Alive status
Alive
// Suspicious status of whether a member is dead or not
Suspected
// Dead status
Dead
)
func (s Status) toInt() int32 {
return int32(s)
}
type SuspicionConfig struct {
// k is the maximum number of independent confirmation's we'd like to see
// this value is for making timer to drive @min value
k int
// min is the minimum timer value
min time.Duration
// max is the maximum timer value
max time.Duration
}
type MemberID struct {
ID string
}
func NewMemberID(s string) MemberID {
return MemberID{
ID: s,
}
}
// Struct of Member
type Member struct {
// Id of member
ID MemberID
// Ip address
Addr net.IP
// Port
Port uint16
// Current member status from my point of view
Status Status
// Time last status change happened
LastStatusChange time.Time
// Incarnation helps to keep the most fresh information about member status in the system
// which tells that suspect member confirming that it is alive, and only when suspect
// got suspicion message, that member can increments incarnation
Incarnation uint32
// Suspicion manages the suspect timer and helps to accelerate the timeout
// as member self got more independent confirmations that a target member is suspect.
Suspicion *Suspicion
}
type MemberMessage struct {
ID string
Addr net.IP
Port uint16
Incarnation uint32
}
type AliveMessage struct {
MemberMessage
}
// Convert member addr and port to string
func (m *Member) Address() string {
return net.JoinHostPort(m.Addr.String(), strconv.Itoa(int(m.Port)))
}
// Get
func (m *Member) GetID() MemberID {
return m.ID
}
func (m *Member) GetIDString() string {
return m.ID.ID
}
type MemberMap struct {
lock sync.RWMutex
members map[MemberID]*Member
suspicionConfig *SuspicionConfig
}
// Suspect message struct
type SuspectMessage struct {
MemberMessage
ConfirmerID string
}
func NewMemberMap(config *SuspicionConfig) *MemberMap {
return &MemberMap{
members: make(map[MemberID]*Member),
lock: sync.RWMutex{},
suspicionConfig: config,
}
}
// Select K random member (length of returning member can be lower than k).
func (m *MemberMap) SelectKRandomMemberID(k int) []Member {
m.lock.Lock()
defer m.lock.Unlock()
// Filter non-alive member
members := make([]Member, 0)
for _, member := range m.members {
if member.Status == Alive {
members = append(members, *member)
}
}
selectedMembers := make([]Member, 0)
// Select K random members
s := rand.NewSource(time.Now().Unix())
r := rand.New(s) // initialize local pseudorandom generator
for i := 0; i < k; i++ {
// When K is larger than members
if len(members) == 0 {
break
}
index := r.Intn(len(members))
selectedMembers = append(selectedMembers, members[index])
members = append(members[:index], members[index+1:]...)
}
return selectedMembers
}
func (m *MemberMap) GetMembers() []Member {
m.lock.Lock()
defer m.lock.Unlock()
members := make([]Member, 0)
for _, member := range m.members {
members = append(members, *member)
}
return members
}
// Suspect handle suspectMessage, if this function update member states
// return true otherwise false
func (m *MemberMap) Suspect(msg SuspectMessage) (bool, error) {
m.lock.Lock()
defer m.lock.Unlock()
// if member id is empty, return empty memberID err
if msg.ID == "" {
return false, ErrEmptyMemberID
}
member, ok := m.members[MemberID{msg.ID}]
if !ok {
return false, nil
}
confirmer := msg.ConfirmerID
incarnation := msg.Incarnation
// if exist member incarnation is bigger than message, drop the message
if member.Incarnation > incarnation {
return false, nil
}
switch member.Status {
case Alive:
return m.suspectWhenAlive(member, confirmer, incarnation)
case Suspected:
return m.suspectWhenSuspect(member, confirmer, incarnation)
case Dead:
return m.suspectWhenDead()
}
return false, ErrMemberUnknownState
}
func (m *MemberMap) suspectWhenDead() (bool, error) {
return false, nil
}
func (m *MemberMap) suspectWhenAlive(member *Member, confirmer string, incarnation uint32) (bool, error) {
config := m.suspicionConfig
suspicion, err := NewSuspicion(MemberID{confirmer}, config.k, config.min, config.max, getSuspicionCallback(m, member))
if err != nil {
return false, ErrCreatingSuspicion
}
member.Status = Suspected
member.Incarnation = incarnation
member.LastStatusChange = time.Now()
member.Suspicion = suspicion
return true, nil
}
func (m *MemberMap) suspectWhenSuspect(member *Member, confirmer string, incarnation uint32) (bool, error) {
config := m.suspicionConfig
if member.Suspicion == nil {
suspicion, err := NewSuspicion(MemberID{confirmer}, config.k, config.min, config.max, getSuspicionCallback(m, member))
if err != nil {
return false, ErrCreatingSuspicion
}
member.Suspicion = suspicion
member.Incarnation = incarnation
return true, nil
}
member.Incarnation = incarnation
member.Suspicion.Confirm(MemberID{confirmer})
return true, nil
}
// Process Alive message
//
// Return 2 parameter bool, error bool means Changed in MemberList
//
// 1. If aliveMessage Id is empty return false and ErrEmptyMemberID
// 2. if aliveMessage Id is not in MemberList then Create Member and Add MemberList
// 3. if aliveMessage Id is in MemberList and existingMember's Incarnation is bigger than AliveMessage's Incarnation Than return false, ErrIncarnation
// 4. if aliveMessage Id is in MemberList and AliveMessage's Incarnation is bigger than existingMember's Incarnation Than Update Member and Return true, nil
func (m *MemberMap) Alive(aliveMessage AliveMessage) (bool, error) {
m.lock.Lock()
defer m.lock.Unlock()
// Check whether Member Id empty
if aliveMessage.ID == "" {
return false, ErrEmptyMemberID
}
// Check whether it is already exist
existingMem, ok := m.members[MemberID{aliveMessage.ID}]
// if Member is not exist in MemberList
if !ok {
m.members[MemberID{aliveMessage.ID}] = createMember(aliveMessage.MemberMessage, Alive)
return true, nil
}
// Check incarnation
if aliveMessage.Incarnation <= existingMem.Incarnation {
return false, nil
}
// update Member
existingMem.Status = Alive
existingMem.Incarnation = aliveMessage.Incarnation
existingMem.LastStatusChange = time.Now()
return true, nil
}
func getSuspicionCallback(memberMap *MemberMap, member *Member) func() {
return func() {
memberMap.lock.Lock()
defer memberMap.lock.Unlock()
member, exist := memberMap.members[member.ID]
if !exist {
iLogger.Error(nil, "member is not found in callback suspicion")
return
}
member.Status = Dead
member.LastStatusChange = time.Now()
}
}
func createMember(message MemberMessage, status Status) *Member {
return &Member{
ID: MemberID{message.ID},
Addr: message.Addr,
Port: message.Port,
Status: status,
LastStatusChange: time.Now(),
Incarnation: message.Incarnation,
}
}
// Delete all dead node,
// Reset waiting list,
func (m *MemberMap) Reset() {
m.lock.Lock()
defer m.lock.Unlock()
// delete dead status node
for k, member := range m.members {
if member.Status == Dead {
delete(m.members, k)
}
}
}
func (memberMap *MemberMap) Merge(membership *pb.Message_Membership) {
}