-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
187 lines (149 loc) · 3.92 KB
/
util.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
package raft
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"log"
"math/rand"
"net"
"time"
)
func decode(buf []byte, out interface{}) error {
// create the buffer from input buf []bytes
msgBuff := bytes.NewBuffer(buf)
// create the decoder, which will decode the r buf
dec := gob.NewDecoder(msgBuff)
// will write the decoded value to out. out can be any struct after the decode (if compatible)
return dec.Decode(out)
}
func encode(msgType int, msg interface{}) (*bytes.Buffer, error) {
// Create the buffer
buf := bytes.NewBuffer(nil)
// add the buffer type, do not encode it
binary.Write(buf, binary.BigEndian, uint16(msgType))
// create encoder. This enc will encode to buf
enc := gob.NewEncoder(buf)
// encode the message in itself to buf
err := enc.Encode(msg)
return buf, err
}
func getUDPAddr(addr string, port int) *net.UDPAddr {
res := &net.UDPAddr{IP: net.ParseIP(addr), Port: port}
return res
}
func (r *Raft) getTCPAddress() (addr string) {
addr = fmt.Sprintf("%s:%d", r.config.BindAddr, r.config.BindTCPPort)
return addr
}
func (r *Raft) getUDPAddress() (addr string) {
addr = fmt.Sprintf("%s:%d", r.config.BindAddr, r.config.BindUDPPort)
return addr
}
func (r *Raft) encodeAndSendMsg(dest net.Addr, msgType int, msg interface{}) error {
buf, err := encode(msgType, msg)
if err != nil {
return err
}
if err := r.sendMsg(dest, buf); err != nil {
return err
}
return nil
}
func (r *Raft) sendMsg(to net.Addr, msg *bytes.Buffer) error {
_, err := r.udpListener.WriteTo(msg.Bytes(), to)
if err != nil {
log.Printf("Failed to send UDP to %s, Err: %s", to, err)
return err
}
return nil
}
func (r *Raft) voteForSelf() {
vt := &Vote{
Voter: r.config.Name,
VoteStatus: Voted,
}
r.votes <- vt
}
func (r *Raft) addVote(vt *Vote) {
node := r.nodeMap[vt.Voter]
if node == nil {
log.Printf("Node was not found: %s", vt.Voter)
}
node.VoteStatus = vt.VoteStatus
}
func (r *Raft) releaseVotes() error {
for _, node := range r.Nodes() {
node.VoteStatus = UnknownVote
}
return nil
}
// calculateVotes return true if majority --> Leader
// or returns false if not majority --> Candidate
func (r *Raft) calculateVotes() bool {
online := 0
votes := 0
for _, node := range r.Nodes() {
fmt.Printf("Node %s ; VoteStatus %d\n", node.Name, node.VoteStatus)
if node.VoteStatus != UnknownVote {
online++
if node.VoteStatus == Voted {
votes++
}
}
}
fmt.Printf("Votes: %d, online: %d\n", (votes + 1), (online + 1))
if (votes + 1) >= (online+1+1)/2 {
return true
}
return false
}
func getElectionTicker(n int) *time.Ticker {
rnd := int(rand.Float32() * RndTime)
intrv := n + rnd
return time.NewTicker(time.Duration(intrv) * time.Millisecond)
}
func (r *Raft) resetTicker() {
r.electionTimeoutTickerLock.Lock()
r.electionTimeoutTicker = getElectionTicker(r.config.ElectionTime)
r.electionTimeoutTickerLock.Unlock()
}
func (r *Raft) stopTicker() {
if r.electionTimeoutTicker == nil {
return
}
r.electionTimeoutTickerLock.Lock()
r.electionTimeoutTicker.Stop()
// r.electionTimeoutTicker = nil
r.electionTimeoutTickerLock.Unlock()
}
func (r *Raft) resetVotingTicker() {
r.votingTimeoutTickerLock.Lock()
r.votingTimeoutTicker = getElectionTicker(r.config.VotingTime)
r.votingTimeoutTickerLock.Unlock()
}
func (r *Raft) stopVotingTicker() {
if r.votingTimeoutTicker == nil {
return
}
r.votingTimeoutTickerLock.Lock()
r.votingTimeoutTicker.Stop()
// r.votingTimeoutTicker = nil
r.votingTimeoutTickerLock.Unlock()
}
func (r *Raft) resetHeartBeat() {
r.heartBeatLock.Lock()
r.heartBeat = getElectionTicker(r.config.ElectionTime - RndTime)
r.heartBeatLock.Unlock()
}
func (r *Raft) stopHeartBeatTicker() {
if r.heartBeat == nil {
// r.heartBeat = getElectionTicker(r.config.ElectionTime - r.config.ElectionTime/2)
return
}
r.heartBeatLock.Lock()
r.heartBeat.Stop()
// r.stopHeartBeat <- true
// r.electionTimeoutHeartBeat = nil
r.heartBeatLock.Unlock()
}