-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
142 lines (112 loc) · 2.95 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"net"
"strings"
"time"
)
func GetLocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", fmt.Errorf("no valid local IP address found")
}
func GetIPFromID(id string) string {
parts := strings.Split(id, "@")
if len(parts) > 0 {
return parts[0]
}
return ""
}
func ConstructNodeID(ip string) string {
// return fmt.Sprintf("%s@%s", ip, time.Now().Format(time.RFC3339))
return fmt.Sprintf("%s@%s", ip, time.Now().Format(time.TimeOnly))
}
func GetServerEndpoint(host string) string {
return fmt.Sprintf("%s:%d", host, SERVER_PORT)
}
// Take a list of messages and encapsulates them into a PING message.
// Returns the encoded message.
func EncodePingMessage(messages Messages) ([]byte, error) {
messagesEnc, err := json.Marshal(messages)
if err != nil {
return nil, err
}
pingMessage := Message{Kind: PING, Data: string(messagesEnc)}
pingMessageEnc, err := json.Marshal(pingMessage)
if err != nil {
return nil, err
}
return pingMessageEnc, nil
}
// Take a list of messages and encapsulates them into an ACK message.
// Returns the encoded message.
func EncodeAckMessage(messages Messages) ([]byte, error) {
messagesEnc, err := json.Marshal(messages)
if err != nil {
return nil, err
}
ackMessage := Message{Kind: ACK, Data: string(messagesEnc)}
ackMessageEnc, err := json.Marshal(ackMessage)
if err != nil {
return nil, err
}
return ackMessageEnc, nil
}
// For a given message, returns the sub-messages present inside it.
func DecodeAckMessage(messageEnc []byte) (Messages, error) {
var message Message
err := json.Unmarshal(messageEnc, &message)
if err != nil {
return nil, err
}
if message.Kind != ACK {
return nil, fmt.Errorf("Message kind is not ACK")
}
var messages Messages
err = json.Unmarshal([]byte(message.Data), &messages)
if err != nil {
return nil, err
}
return messages, nil
}
func Shuffle(slice []string) {
for i := range slice {
j := rand.Intn(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}
func DeleteMemberAndReReplicate(nodeID string) {
fmt.Println("Re-replicating")
DeleteMember(nodeID)
updatedPrimaryFiles := UpdatePrimaryReplicas()
// Invoke in a Go-routine because re-replication (with file checks could take a while)
go ReplicateFilesWrapper(updatedPrimaryFiles)
}
// Removes common names from A
func RemoveCommonElements(A, B []string) []string {
// Create a map to store elements from B for fast lookup
bMap := make(map[string]bool)
for _, item := range B {
bMap[item] = true
}
// Create a new slice to store unique elements from A
result := []string{}
// Iterate through A and only keep elements not in B
for _, item := range A {
if !bMap[item] {
result = append(result, item)
}
}
return result
}