This repository has been archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
neighborhood.go
177 lines (164 loc) · 3.99 KB
/
neighborhood.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
package wendy
import (
"errors"
"log"
"os"
"sync"
)
type neighborhoodSet struct {
self *Node
nodes [32]*Node
log *log.Logger
logLevel int
lock *sync.RWMutex
}
func newNeighborhoodSet(self *Node) *neighborhoodSet {
return &neighborhoodSet{
self: self,
nodes: [32]*Node{},
log: log.New(os.Stdout, "wendy#neighborhoodSet("+self.ID.String()+")", log.LstdFlags),
logLevel: LogLevelWarn,
lock: new(sync.RWMutex),
}
}
var nsDuplicateInsertError = errors.New("Node already exists in neighborhood set.")
func (n *neighborhoodSet) insertNode(node Node, proximity int64) (*Node, error) {
return n.insertValues(node.ID, node.LocalIP, node.GlobalIP, node.Region, node.Port, node.routingTableVersion, node.leafsetVersion, node.neighborhoodSetVersion, proximity)
}
func (n *neighborhoodSet) insertValues(id NodeID, localIP, globalIP, region string, port int, rTVersion, lSVersion, nSVersion uint64, proximity int64) (*Node, error) {
n.lock.Lock()
defer n.lock.Unlock()
if id.Equals(n.self.ID) {
return nil, throwIdentityError("insert", "into", "neighborhood set")
}
insertNode := NewNode(id, localIP, globalIP, region, port)
insertNode.updateVersions(rTVersion, lSVersion, nSVersion)
insertNode.setProximity(proximity)
newNS := [32]*Node{}
newNSpos := 0
score := n.self.Proximity(insertNode)
inserted := false
dup := false
for _, node := range n.nodes {
if newNSpos > 31 {
break
}
if node == nil && !inserted && !dup {
newNS[newNSpos] = insertNode
newNSpos++
inserted = true
continue
}
if node != nil && insertNode.ID.Equals(node.ID) {
insertNode.updateVersions(node.routingTableVersion, node.leafsetVersion, node.neighborhoodSetVersion)
newNS[newNSpos] = insertNode
newNSpos++
dup = true
continue
}
if node != nil && n.self.Proximity(node) > score && !inserted && !dup {
newNS[newNSpos] = insertNode
newNSpos++
inserted = true
continue
}
if newNSpos <= 31 {
newNS[newNSpos] = node
newNSpos++
}
}
n.nodes = newNS
if dup {
return nil, nsDuplicateInsertError
}
if inserted {
n.self.incrementNSVersion()
return insertNode, nil
}
return nil, nil
}
func (n *neighborhoodSet) getNode(id NodeID) (*Node, error) {
n.lock.RLock()
defer n.lock.RUnlock()
if id.Equals(n.self.ID) {
return nil, throwIdentityError("get", "from", "neighborhood set")
}
for _, node := range n.nodes {
if node == nil {
break
}
if id.Equals(node.ID) {
return node, nil
}
}
return nil, nodeNotFoundError
}
func (n *neighborhoodSet) export() [32]*Node {
n.lock.RLock()
defer n.lock.RUnlock()
return n.nodes
}
func (n *neighborhoodSet) list() []*Node {
n.lock.RLock()
defer n.lock.RUnlock()
nodes := []*Node{}
for _, node := range n.nodes {
if node != nil {
nodes = append(nodes, node)
}
}
return nodes
}
func (n *neighborhoodSet) removeNode(id NodeID) (*Node, error) {
n.lock.Lock()
defer n.lock.Unlock()
if id.Equals(n.self.ID) {
return nil, throwIdentityError("remove", "from", "neighborhood set")
}
pos := -1
var node *Node
for index, entry := range n.nodes {
if entry == nil || entry.ID.Equals(id) {
pos = index
node = entry
break
}
}
if pos == -1 || pos > len(n.nodes) {
return nil, nodeNotFoundError
}
var slice []*Node
if len(n.nodes) == 1 {
slice = []*Node{}
} else if pos+1 == len(n.nodes) {
slice = n.nodes[:pos]
} else if pos == 0 {
slice = n.nodes[1:]
} else {
slice = append(n.nodes[:pos], n.nodes[pos+1:]...)
}
for i, _ := range n.nodes {
if i < len(slice) {
n.nodes[i] = slice[i]
} else {
n.nodes[i] = nil
}
}
n.self.incrementNSVersion()
return node, nil
}
func (n *neighborhoodSet) debug(format string, v ...interface{}) {
if n.logLevel <= LogLevelDebug {
n.log.Printf(format, v...)
}
}
func (n *neighborhoodSet) warn(format string, v ...interface{}) {
if n.logLevel <= LogLevelWarn {
n.log.Printf(format, v...)
}
}
func (n *neighborhoodSet) err(format string, v ...interface{}) {
if n.logLevel <= LogLevelError {
n.log.Printf(format, v...)
}
}