-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelection.go
232 lines (180 loc) · 5.16 KB
/
election.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
package election
import (
"strings"
"errors"
"net"
"log"
"golang.org/x/net/ipv4"
"time"
"sync"
"fmt"
"os"
)
func RegisterCallback(callback Callback, multicastNet string, networkInterface string) error{
participant, err := newParticipant(multicastNet,networkInterface)
if err != nil{
return err
}
participant.callback = callback
go participant.watcher()
return nil
}
func newParticipant(multicastNet string, networkInterface string) (*Participant, error) {
if !strings.Contains(multicastNet, ":"){
return nil, errors.New("Muilticast address is not in the format IP:PORT")
}
addrsTokens := strings.Split(multicastNet,":")
p := &Participant{state:FOLLOWER, ipAddr:addrsTokens[0], port:addrsTokens[1], heardFromLeader:false,
writeMutex:&sync.Mutex{}, electionTimer:nil, waitForAnotherLeader:false}
if "" == networkInterface {
return nil, errors.New("Network interface must be specified")
}
var err error
if p.multicastInterface, err = net.InterfaceByName(networkInterface); err != nil {
return nil, err
}
p.localIpAddr, p.localIpAddrNumeric = getLocalInterfaceIpAddress(p.multicastInterface)
groupIP := net.ParseIP(addrsTokens[0])
if !groupIP.IsMulticast(){
return nil, errors.New("Address supplied is not a multicast address")
}
groupIpOctets := strings.Split(addrsTokens[0],".")
conn, err := net.ListenPacket("udp4", groupIpOctets[0] + ".0.0.0:" + addrsTokens[1])
if err != nil{
return nil, err
}
p.conn = ipv4.NewPacketConn(conn)
p.conn.JoinGroup(p.multicastInterface,&net.UDPAddr{IP:groupIP})
p.conn.SetControlMessage(ipv4.FlagDst,true)
p.dst,_ = net.ResolveUDPAddr("udp4", multicastNet)
if err := p.conn.SetMulticastInterface(p.multicastInterface); err != nil {
log.Println(err)
}
p.conn.SetTTL(MULTICATE_TTL)
p.pid = os.Getpid()
return p, nil
}
func(this *Participant) cleanup() {
this.conn.Close()
}
func (this *Participant) leaderPeriodicAnnouncement() {
go this.callback(LEADER)
ticker := time.NewTicker(time.Millisecond * LEADER_PERIODIC_ANNOUNCEMENT_TIME)
for range ticker.C{
this.announce("LEADER")
}
}
func(this *Participant) monitorLeader() {
bchan := make(chan bool)
ticker := time.NewTicker(time.Millisecond * LEADER_NOTIFICATION_TIMEOUT)
exit := false
for !exit {
select {
case <-ticker.C:
this.Lock()
if !this.heardFromLeader {
if this.state == FOLLOWER && !this.waitForAnotherLeader {
this.state = CANDIDATE
this.announce("ELECTION")
this.electionTimer = time.NewTimer(time.Second * ELECTION_TIMEOUT)
go func() {
<-this.electionTimer.C
this.state = LEADER
ticker.Stop()
bchan <- true
go this.leaderPeriodicAnnouncement()
}()
go this.callback(CANDIDATE)
}
} else {
this.heardFromLeader = false
}
this.Unlock()
case <-bchan:
close(bchan)
exit = true
}
}
log.Println("exiting leader monitor")
}
func (this *Participant) watcher() {
go this.monitorLeader()
buffer := []byte{}
readBuf := make([]byte, 1500)
for{
num,cm,_,err := this.conn.ReadFrom(readBuf)
if err != nil{
log.Println(err)
}
if !cm.Dst.IsMulticast(){
continue
}
buffer = append(buffer,readBuf[:num]...)
for len(buffer) >= MSG_BLOCK_SIZE{
data := string(buffer[:MSG_BLOCK_SIZE])
go this.processData(data)
buffer = buffer[MSG_BLOCK_SIZE:]
}
}
}
func(this *Participant) processData(data string) {
log.Println(data)
dataToken := strings.Split(data,"|")
switch strings.ToUpper(dataToken[0]) {
case "LEADER" :
this.processLeaderRequest(dataToken)
case "ELECTION":
this.processElectionRequest(dataToken)
}
}
func (this *Participant) announce(data string) {
transmitData := fmt.Sprintf("%s|%d|%d|%s",data,this.localIpAddrNumeric,this.pid,this.localIpAddr)
transmitData = transmitData + strings.Repeat("#", MSG_BLOCK_SIZE - len(transmitData))
this.writeMutex.Lock()
log.Println("Announcing == ", transmitData)
if _, err := this.conn.WriteTo([]byte(transmitData),nil, this.dst); err != nil {
log.Println(err)
}
this.writeMutex.Unlock()
}
func (this *Participant) processLeaderRequest(dataToken []string) {
msg := parseMessage(dataToken)
if this.localIpAddrNumeric == msg.ipNumber && this.pid == msg.processId {
log.Println("Same network and process id")
return
}
this.Lock()
this.heardFromLeader = true
this.Unlock()
this.waitForAnotherLeader = false
if this.state == CANDIDATE {
this.electionTimer.Stop()
this.state = FOLLOWER
go this.callback(this.state)
}
}
func (this *Participant) processElectionRequest(dataToken []string) {
msg := parseMessage(dataToken)
if this.localIpAddrNumeric == msg.ipNumber && this.pid == msg.processId {
log.Println("Same network and process id")
return
}
if ((this.localIpAddrNumeric == msg.ipNumber) && this.pid < msg.processId) || (this.localIpAddrNumeric < msg.ipNumber) {
if this.state == CANDIDATE {
this.electionTimer.Stop()
this.waitForAnotherLeader = true;
this.state = FOLLOWER
go this.callback(this.state)
go func() {
tmpTimer := time.NewTimer(time.Second * 5)
<-tmpTimer.C
this.waitForAnotherLeader = false;
}()
}
return
}
//at this point we are eligible to become leader
if this.state == CANDIDATE {
return
}
}