-
Notifications
You must be signed in to change notification settings - Fork 0
/
rafttestutil.go
125 lines (113 loc) · 2.91 KB
/
rafttestutil.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
package rafttestutil
import (
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"
"github.com/cpssd-students/paranoid/pkg/raft"
)
// GenerateNewUUID creates a new UUID
func GenerateNewUUID() string {
uuidBytes, err := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
if err != nil {
log.Fatalln("Error generating new UUID:", err)
}
return strings.TrimSpace(string(uuidBytes))
}
// StartListener starts a new listener on a random port
func StartListener() (*net.Listener, string) {
lis, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatalf("Failed to start listening : %v.\n", err)
}
splits := strings.Split(lis.Addr().String(), ":")
port := splits[len(splits)-1]
return &lis, port
}
// SetUpNode sets up a new node
func SetUpNode(name, ip, port, commonName string) raft.Node {
return raft.Node{
NodeID: name,
IP: ip,
Port: port,
CommonName: commonName,
}
}
// CloseListener closes the given net listener
func CloseListener(lis *net.Listener) {
if lis != nil {
(*lis).Close()
file, _ := (*lis).(*net.TCPListener).File()
file.Close()
}
}
// StopRaftServer stops the given raft server
func StopRaftServer(raftServer *raft.NetworkServer) {
if !raftServer.QuitChannelClosed {
close(raftServer.Quit)
}
}
// CreateRaftDirectory clears the specified directory
func CreateRaftDirectory(raftDirectory string) string {
os.RemoveAll(raftDirectory)
err := os.MkdirAll(raftDirectory, 0700)
if err != nil {
log.Fatal("Error creating raft directory:", err)
}
return raftDirectory
}
// RemoveRaftDirectory removes the raft directory when the server is finished
func RemoveRaftDirectory(raftDirectory string, raftServer *raft.NetworkServer) {
if raftServer != nil {
//Need to wait for server to shut down or the file could be removed while in use
raftServer.Wait.Wait()
}
time.Sleep(time.Second)
os.RemoveAll(raftDirectory)
}
// IsLeader checks if the local raft server is the raft leader
func IsLeader(server *raft.NetworkServer) bool {
return server.State.GetCurrentState() == raft.LEADER
}
// GetLeader from a list of servers
func GetLeader(cluster []*raft.NetworkServer) *raft.NetworkServer {
highestTerm := uint64(0)
highestIndex := -1
for i := 0; i < len(cluster); i++ {
if IsLeader(cluster[i]) {
currentTerm := cluster[i].State.GetCurrentTerm()
if currentTerm > highestTerm {
highestTerm = currentTerm
highestIndex = i
}
}
}
if highestIndex >= 0 {
return cluster[highestIndex]
}
return nil
}
// GetLeaderTimeout gets a leader, if the leader is not found it tires again
// after the timeout
func GetLeaderTimeout(cluster []*raft.NetworkServer, timeoutSeconds int) *raft.NetworkServer {
var leader *raft.NetworkServer
leader = GetLeader(cluster)
if leader != nil {
return leader
}
count := 0
for {
count++
if count > timeoutSeconds {
break
}
time.Sleep(1 * time.Second)
leader = GetLeader(cluster)
if leader != nil {
break
}
}
return leader
}