-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebalancer.go
124 lines (110 loc) · 2.77 KB
/
rebalancer.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
package main
import (
"bytes"
"errors"
"log"
)
type rebalanceRequest struct {
Key key
chResponse chan error
}
type rebalancer struct {
c *cluster
s site
chR chan rebalanceRequest
}
func newRebalancer(c *cluster, s site) *rebalancer {
ch := make(chan rebalanceRequest)
r := rebalancer{c, s, ch}
go r.run()
return &r
}
func (r *rebalancer) run() {
for req := range r.chR {
req.chResponse <- r.doRebalance(req.Key)
}
}
func (r *rebalancer) Rebalance(key key) error {
resp := make(chan error)
req := rebalanceRequest{key, resp}
r.chR <- req
return <-resp
}
// check that the file is stored in at least Replication nodes
// and, if at all possible, those should be the ones at the front
// of the list
func (r rebalancer) doRebalance(key key) error {
if r.c == nil {
log.Println("can't rebalance on a nil cluster")
return errors.New("nil cluster")
}
rebalances.Inc()
nodesToCheck := r.c.ReadOrder(key.String())
satisfied, deleteLocal, foundReplicas := r.checkNodesForRebalance(key, nodesToCheck)
if !satisfied {
rebalanceFailures.Inc()
log.Printf("could not replicate %s to %d nodes", key, r.s.Replication)
} else {
rebalanceNoops.Inc()
log.Printf("%s has full replica set (%d of %d)\n", key, foundReplicas, r.s.Replication)
}
if satisfied && deleteLocal {
rebalanceDeletes.Inc()
r.cleanUpExcessReplica(key)
}
return nil
}
func (r rebalancer) checkNodesForRebalance(key key, nodesToCheck []node) (bool, bool, int) {
var satisfied = false
var foundReplicas = 0
var deleteLocal = true
for _, n := range nodesToCheck {
if n.UUID == r.c.Myself.UUID {
deleteLocal = false
foundReplicas++
} else {
foundReplicas = foundReplicas + r.retrieveReplica(key, n, satisfied)
}
if foundReplicas >= r.s.Replication {
satisfied = true
}
if foundReplicas >= r.s.MaxReplication {
return satisfied, deleteLocal, foundReplicas
}
}
return satisfied, deleteLocal, foundReplicas
}
func (r rebalancer) retrieveReplica(key key, n node, satisfied bool) int {
local, err := n.RetrieveInfo(key, r.c.secret)
if err == nil && local {
return 1
}
if !n.Writeable {
return 0
}
if !satisfied {
b, err := r.s.Backend.Read(key)
buf := bytes.NewBuffer(b)
if err != nil {
log.Printf("error reading from backend")
return 0
}
if n.AddFile(key, buf, r.c.secret) {
log.Printf("replicated %s\n", key)
return 1
}
log.Println("write to the node failed, but what can we do?")
}
return 0
}
// our node is not at the front of the list, so
// we have an excess copy. clean that up and make room!
func (r rebalancer) cleanUpExcessReplica(key key) {
err := r.s.Backend.Delete(key)
if err != nil {
log.Printf("could not clear out excess replica: %s\n", key)
log.Println(err.Error())
} else {
log.Printf("cleared excess replica: %s\n", key)
}
}