-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchord.go
160 lines (141 loc) · 2.97 KB
/
chord.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
package main
import (
"crypto/sha1"
"math/big"
"sync"
"time"
)
const M = 160
type Node struct {
ID *big.Int
Address string
successor *Node
predecessor *Node
fingers [M]*Node
mutex sync.RWMutex
stopCh chan struct{}
}
func NewNode(address string) *Node {
return &Node{
ID: hashToBigInt(address),
Address: address,
stopCh: make(chan struct{}),
}
}
func (n *Node) Create() {
n.mutex.Lock()
defer n.mutex.Unlock()
n.successor = n
n.predecessor = nil
for i := 0; i < M; i++ {
n.fingers[i] = n
}
}
func (n *Node) Join(knownNode *Node) {
n.mutex.Lock()
defer n.mutex.Unlock()
n.predecessor = nil
n.successor = knownNode.findSuccessor(n.ID)
}
func (n *Node) findSuccessor(id *big.Int) *Node {
current := n
if n == n.successor {
return n
}
if betweenEq(id, current.ID, current.successor.ID) {
return current.successor
}
closest := current.closestPrecedingNode(id)
if closest == current {
return current.successor.findSuccessor(id)
}
return closest.findSuccessor(id)
}
func (n *Node) closestPrecedingNode(id *big.Int) *Node {
n.mutex.RLock()
defer n.mutex.RUnlock()
for i := M - 1; i >= 0; i-- {
f := n.fingers[i]
if f != nil && between(f.ID, n.ID, id) {
return f
}
}
return n
}
func (n *Node) Stabilize() {
for {
select {
case <-n.stopCh:
return
case <-time.After(time.Second * 3):
n.mutex.Lock()
succ := n.successor
if succ == nil {
n.mutex.Unlock()
continue
}
x := succ.predecessor
if x != nil && between(x.ID, n.ID, succ.ID) {
n.successor = x
}
n.successor.notify(n)
n.mutex.Unlock()
}
}
}
func (n *Node) notify(candidate *Node) {
n.mutex.Lock()
defer n.mutex.Unlock()
if n.predecessor == nil || between(candidate.ID, n.predecessor.ID, n.ID) {
n.predecessor = candidate
}
}
func (n *Node) FixFingers() {
nextIndex := 0
for {
select {
case <-n.stopCh:
return
case <-time.After(time.Second * 3):
n.mutex.Lock()
next := nextIndex
start := new(big.Int).Add(n.ID, twoToPower(next))
start.Mod(start, twoToPower(M))
n.fingers[next] = n.findSuccessor(start)
nextIndex = (nextIndex + 1) % M
n.mutex.Unlock()
}
}
}
func (n *Node) AddPeer(newAddr string, knownNode *Node) *Node {
newNode := NewNode(newAddr)
newNode.Join(knownNode)
return newNode
}
func (n *Node) Lookup(key string) *Node {
keyID := hashToBigInt(key)
return n.findSuccessor(keyID)
}
func (n *Node) Stop() {
close(n.stopCh)
}
func hashToBigInt(str string) *big.Int {
h := sha1.Sum([]byte(str))
return new(big.Int).SetBytes(h[:])
}
func twoToPower(exp int) *big.Int {
e := big.NewInt(1)
return e.Lsh(e, uint(exp))
}
func between(x, a, b *big.Int) bool {
if a.Cmp(b) < 0 {
return (x.Cmp(a) > 0) && (x.Cmp(b) < 0)
}
return (x.Cmp(a) > 0) || (x.Cmp(b) < 0)
}
func betweenEq(x, a, b *big.Int) bool {
if a.Cmp(b) < 0 {
return (x.Cmp(a) > 0 && x.Cmp(b) <= 0) || x.Cmp(a) == 0 || x.Cmp(b) == 0
}
return (x.Cmp(a) > 0 || x.Cmp(b) < 0) || x.Cmp(a) == 0 || x.Cmp(b) == 0
}