-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
162 lines (126 loc) · 3.22 KB
/
cluster.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
package raft
import (
"fmt"
"log"
"net"
"time"
)
// First send ping to addr
// Wait for the response (have to define the timeout for that)
// if response then call RPC for getting the nodes and leader information
func (r *Raft) joinToClusterByAddr(addr net.Addr) error {
r.stateCh <- Unknown
joinTimeout := time.After(100 * time.Millisecond)
for {
select {
case <-joinTimeout:
if len(r.self.leaderID) > 0 {
fmt.Println("Got the leader, I am leaving the loop")
r.stateCh <- r.self.state
return nil
}
p := &joinPing{
Node: &Node{
Name: r.config.Name,
Addr: getUDPAddr(r.config.BindAddr, r.config.BindUDPPort),
},
}
err := r.encodeAndSendMsg(addr, joinPingMsg, p)
if err != nil {
log.Printf("Failed to send the ping message. Err %s", err)
return err
}
joinTimeout = time.After(1 * time.Second)
}
}
}
func (r *Raft) syncStateAfterJoin(dt *joinAckResp) error {
err := r.addNode(dt.Node)
if err != nil {
log.Printf("Failed to add node to node list. Err %s", err)
return err
}
for _, node := range dt.Nodes {
err := r.addNode(node)
if err != nil {
log.Printf("Failed to add node to node list. Err %s", err)
return err
}
}
if r.self.state == Unknown {
r.stateLock.Lock()
r.self.leaderID = dt.LeaderID
r.self.state = Follower
r.stateLock.Unlock()
}
return nil
}
func (r *Raft) addNode(p *Node) error {
if p.Name == r.config.Name {
return nil
}
node := r.nodeMap[p.Name]
if node != nil {
return nil
}
node = &Node{
Name: p.Name,
Addr: p.Addr,
VoteStatus: UnknownVote,
}
r.nodeMap[node.Name] = node
r.nodes = append(r.nodes, node)
return nil
}
func (r *Raft) invokeJoinAckResp(from net.Addr, p *joinPing) error {
if r.self.leaderID == p.Node.Name {
return fmt.Errorf("Failed to forward the packet, Leader has not been selected yet")
}
if r.self.state == Leader {
ack := &joinAckResp{}
node := &Node{
Name: r.config.Name,
Addr: getUDPAddr(r.config.BindAddr, r.config.BindUDPPort),
}
ack.LeaderID = r.self.leaderID
ack.Nodes = r.Nodes()
ack.Node = node
err := r.encodeAndSendMsg(p.Node.Addr, joinAckRespMsg, &ack)
if err != nil {
log.Printf("Failed to send the ack to the ping. Err %s", err)
return err
}
err = r.broadCastNewMember(p)
if err != nil {
log.Printf("Failed to broadcast about new node. Err %s", err)
}
} else {
// forward the ping to server
if len(r.self.leaderID) <= 0 || len(r.Nodes()) <= 0 {
return fmt.Errorf("Failed to forward the packet, Leader has not been selected yet")
}
node := r.nodeMap[r.self.leaderID]
if node == nil {
return fmt.Errorf("Failed to forward the packet, Leader has not been selected yet")
}
leaderAddr := node.Addr
if len(r.self.leaderID) > 0 {
if leaderAddr.String() == from.String() {
return nil
}
fmt.Println("Sent request to leader server: --> ", leaderAddr)
err := r.encodeAndSendMsg(leaderAddr, joinPingMsg, &p)
if err != nil {
log.Printf("Failed to forward the ping to the leader. Err %s", err)
return err
}
}
}
return nil
}
func (r *Raft) broadCastNewMember(p *joinPing) error {
for _, node := range r.nodes {
go r.encodeAndSendMsg(node.Addr, joinPingMsg, &p)
}
return nil
}