-
Notifications
You must be signed in to change notification settings - Fork 0
/
membershipInfo.go
110 lines (80 loc) · 2.47 KB
/
membershipInfo.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
// Stores functions for the map that stores membership information.
package main
import (
"fmt"
"sync"
)
var membershipInfo map[string]MemberInfo = make(map[string]MemberInfo)
var membershipInfoMutex = sync.RWMutex{}
type RingMemberInfo struct {
Id string
RingPosition int
}
func AddNewMemberToMembershipInfo(nodeId string) error {
ipAddr := GetIPFromID(nodeId)
if nodeId == NODE_ID || ipAddr == LOCAL_IP {
LogError(fmt.Sprintf("Unexpected, attempt to add self. Don't do anything. %s %s %s %s\n",
nodeId, NODE_ID, ipAddr, LOCAL_IP))
return nil
}
LogMessage(fmt.Sprintf("Adding new member to info %s %s %s %s\n",
nodeId, NODE_ID, ipAddr, LOCAL_IP))
membershipInfoMutex.Lock()
defer membershipInfoMutex.Unlock()
membershipInfo[nodeId] = MemberInfo{
Host: ipAddr,
failed: false,
RingPosition: GetRingPosition(nodeId),
}
LogMessage(fmt.Sprintf("JOIN NODE: %s", nodeId))
return nil
}
// Returns the members in the group. Doesn't return failed members.
func GetMembers() map[string]MemberInfo {
members := make(map[string]MemberInfo)
membershipInfoMutex.RLock()
defer membershipInfoMutex.RUnlock()
for k, v := range membershipInfo {
members[k] = v
}
return members
}
func GetMemberAndRingPositions() []RingMemberInfo {
membershipInfoMutex.RLock()
defer membershipInfoMutex.RUnlock()
members := make([]RingMemberInfo, 0, len(membershipInfo))
for k, v := range membershipInfo {
members = append(members, RingMemberInfo{k, v.RingPosition})
}
return members
}
func PrintMembershipInfo() {
membershipInfoMutex.RLock()
defer membershipInfoMutex.RUnlock()
for k, v := range membershipInfo {
fmt.Printf("NODE ID: %s RING POSITION: %d\n", k, v.RingPosition)
}
}
func AddToMembershipInfo(nodeId string, member *MemberInfo) {
membershipInfoMutex.Lock()
defer membershipInfoMutex.Unlock()
membershipInfo[nodeId] = *member
LogMessage(fmt.Sprintf("JOIN NODE: %s", nodeId))
}
func GetMemberInfo(nodeId string) (MemberInfo, bool) {
membershipInfoMutex.RLock()
defer membershipInfoMutex.RUnlock()
member, ok := membershipInfo[nodeId]
return member, ok
}
func DeleteMember(nodeId string) {
membershipInfoMutex.Lock()
defer membershipInfoMutex.Unlock()
member := membershipInfo[nodeId]
member.failed = true
membershipInfo[nodeId] = member
// Deleting a non-existent entry is a no-op, so this operation is safe.
delete(membershipInfo, nodeId)
LogMessage(fmt.Sprintf("DELETE NODE: %s", nodeId))
fmt.Printf("DELETE NODE: %s\n", nodeId)
}