-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (117 loc) · 3.42 KB
/
main.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
package main
import (
"DHT-implementation/util"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
var node Node
func getNextNode(node Node) Node {
nextNode, err := addrToNode(node.NextAddress)
if err != nil {
log.Fatal("Get next node failed")
}
return nextNode
}
func findNode(start Node, key string) Node {
current := start
keyHash := util.Hash(key, K)
for util.RingDistance(util.Hash(current.Address, K), keyHash, K) >
util.RingDistance(util.Hash(current.Next().Address, K), keyHash, K) {
current = getNextNode(current)
}
return current
}
func insertSelfAfter(oldNodeAddr string) {
prevNode, err := addrToNode(oldNodeAddr)
if err != nil {
log.Fatal("Insert self in network failed")
}
nextNode := prevNode.Next()
// Copy required data from prev node
allDataFromPrevNode := getAllDataFromNode(prevNode)
for _, item := range allDataFromPrevNode {
itemKeyHash := util.Hash(item.Key, K)
if util.RingDistance(node.ID, itemKeyHash, K) <
util.RingDistance(prevNode.ID, itemKeyHash, K) {
log.Printf("Copying %+v with hash %d from predecessor %d to self", item, itemKeyHash, prevNode.ID)
deleteDataFromSpecificNode(prevNode, item.Key)
keyValueStore[item.Key] = item.Value
}
}
updateRouting(prevNode.Address, RoutingInfo{Next: node.Address})
updateRouting(nextNode.Address, RoutingInfo{Prev: node.Address})
node.PrevAddress = prevNode.Address
node.NextAddress = nextNode.Address
log.Printf("Node info: %+v", node)
}
func setRoutes() {
http.HandleFunc("/", homePageHandler)
http.HandleFunc("/stats", statsHandler)
http.HandleFunc("/node", getNodeInfoHandler)
http.HandleFunc("/data", dataDisplayHandler)
http.HandleFunc("/update-routing", updateRoutingHandler)
http.HandleFunc("/server-data", directDataLevelCommunicationHandler)
}
func initNode() {
if util.CheckPortOpen(BasePort) {
log.Printf("Starting first node at port %d", BasePort)
node.ID = util.Hash(CompleteBaseAddress(), K)
node.Address = CompleteBaseAddress()
node.NextAddress = CompleteBaseAddress()
node.PrevAddress = CompleteBaseAddress()
log.Printf("Base node info: %+v", node)
err := http.ListenAndServe(CompleteBaseAddress(), nil)
if err != nil {
log.Fatal("Could not start root node")
}
} else {
// Finding next available port
availablePort := BasePort + 1
const MaxTCPPort = 65535
for !util.CheckPortOpen(availablePort) && availablePort < MaxTCPPort {
availablePort += 1
}
log.Printf("Starting node at port %d", availablePort)
completeAddress := fmt.Sprintf(":%d", availablePort)
node.ID = util.Hash(completeAddress, K)
node.Address = completeAddress
rootNode, _ := addrToNode(CompleteBaseAddress())
nodeToInsertAfter := findNode(rootNode, completeAddress)
insertSelfAfter(nodeToInsertAfter.Address)
err := http.ListenAndServe(completeAddress, nil)
if err != nil {
log.Fatal("Could not start node")
}
}
}
func handleRequests() {
setRoutes()
initNode()
}
func closeGracefully() {
prev := node.Prev()
next := node.Next()
updateRouting(prev.Address, RoutingInfo{Next: next.Address})
updateRouting(next.Address, RoutingInfo{Prev: prev.Address})
log.Println("Exiting... Transferring all data to predecessor")
for _, item := range getAllData() {
writeDataToSpecificNode(prev, item)
}
}
func setupCloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGINT)
go func() {
<-c
closeGracefully()
os.Exit(0)
}()
}
func main() {
setupCloseHandler()
handleRequests()
}