-
Notifications
You must be signed in to change notification settings - Fork 0
/
introducer.go
153 lines (121 loc) · 3.4 KB
/
introducer.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
package main
import (
"encoding/json"
"fmt"
"log"
"net"
)
func JoinRing() {
if !isLeader {
members, err := IntroduceYourself()
if err != nil {
log.Fatalf("Unable to join the group: %s", err.Error())
}
NODE_ID = InitializeMembershipInfoAndList(members, LOCAL_IP)
helloMessage := Message{
Kind: HELLO,
Data: NODE_ID,
}
AddPiggybackMessage(helloMessage)
} else {
NODE_ID = ConstructNodeID(LEADER_SERVER_HOST)
RING_POSITION = GetRingPosition(NODE_ID)
}
fmt.Println("Joined the group as: ", NODE_ID)
}
func IntroduceYourself() (map[string]MemberInfo, error) {
conn, err := net.Dial("tcp", GetServerEndpoint(LEADER_SERVER_HOST))
if err != nil {
return nil, err
}
defer conn.Close()
joinMessageEnc, err := GetEncodedJoinMessage()
if err != nil {
return nil, err
}
var joinMessage Message
_ = json.Unmarshal(joinMessageEnc, &joinMessage)
localIP, _ := GetLocalIP()
PrintMessage("outgoing", joinMessage, localIP)
conn.Write(joinMessageEnc)
buffer := make([]byte, 1024)
mLen, err := conn.Read(buffer)
if err != nil {
return nil, err
}
members, err := parseMembersFromJoinResponse(buffer[:mLen])
if err != nil {
return nil, err
}
return members, nil
}
func parseMembersFromJoinResponse(buffer []byte) (map[string]MemberInfo, error) {
messages, err := DecodeAckMessage(buffer)
if err != nil {
return nil, err
}
if len(messages) == 0 {
return nil, err
}
membersEnc := []byte(messages[0].Data) // First message is a "JOIN" with membership info
if err != nil {
LogError("Unable to decode the initial suspect state")
}
var members map[string]MemberInfo
err = json.Unmarshal(membersEnc, &members)
if err != nil {
LogError("Unable to decode returned members list")
return nil, nil
}
return members, err
}
// Initalizes the Membership Information map for the newly joined node.
// Returns the NODE_ID for this node.
func InitializeMembershipInfoAndList(members map[string]MemberInfo, localIP string) string {
nodeId := ""
for id, memberInfo := range members {
ip := GetIPFromID(id)
fmt.Printf("ID: %s Position: %d\n", id, memberInfo.RingPosition)
if ip == LEADER_SERVER_HOST {
AddToMembershipInfo(id, &MemberInfo{
Host: ip,
failed: memberInfo.failed,
RingPosition: memberInfo.RingPosition,
})
} else if ip == localIP {
nodeId = id
RING_POSITION = memberInfo.RingPosition
} else {
AddToMembershipInfo(id, &MemberInfo{
Host: ip,
failed: memberInfo.failed,
RingPosition: memberInfo.RingPosition,
})
}
}
return nodeId
}
// Add nodes to membership list and returns a message containing all members.
func IntroduceNodeToGroup(request string, ipAddr string) (Message, error) {
nodeId := ConstructNodeID(ipAddr)
// fmt.Printf("IP: %s NodeID: %s", ipAddr, nodeId)
AddNewMemberToMembershipInfo(nodeId)
membershipListResponse := GetMembers()
// For the response, add yourself to the list as well.
// ! @kartikr2 This isn't using the lock.
membershipListResponse[NODE_ID] = MemberInfo{
Host: NODE_ID,
failed: false,
RingPosition: RING_POSITION,
}
membershipListEnc, err := json.Marshal(membershipListResponse)
if err != nil {
return Message{}, err
}
response := Message{
Kind: JOIN,
Data: string(membershipListEnc),
}
// Note, adding introducer dissemination here could have implications if new node isn't ready to receive messages just yet.
return response, err
}